0% found this document useful (0 votes)
14 views

DSA-Module 1_ Notes on Search Trees and Their Operations

The document discusses two models of search trees: Leaf Trees, where data is stored in the leaves and nodes serve for comparison, and Node Trees, where each node can contain an object and has a ternary structure. It covers the general properties of search trees, transformations like left and right rotations, the height of search trees, and basic operations such as find, insert, and delete. Additionally, it addresses methods for returning from leaf to root and handling nonunique keys.

Uploaded by

armar abdul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

DSA-Module 1_ Notes on Search Trees and Their Operations

The document discusses two models of search trees: Leaf Trees, where data is stored in the leaves and nodes serve for comparison, and Node Trees, where each node can contain an object and has a ternary structure. It covers the general properties of search trees, transformations like left and right rotations, the height of search trees, and basic operations such as find, insert, and delete. Additionally, it addresses methods for returning from leaf to root and handling nonunique keys.

Uploaded by

armar abdul
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

lOMoARcPSD|45529801

Advance Data Structures – Notes

Module – 1

Two Models of Search Trees:

Model 1: Leaf Trees


1. Structure:
• In this model, the search tree is a binary tree where each
interior node serves only for comparison.
• All actual objects (data) are stored in the leaves of the tree.
• The tree nodes contain keys for comparison, and the leaves contain
the objects associated with those keys.
2. Traversal:
• When searching for a key:
• If the query key is smaller than the node key, traverse the
left subtree.
• If the query key is larger than the node key, traverse the
right subtree.

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

• If the query key is equal to the node key, continue


searching in the left or right subtree until a leaf is reached.
3. Characteristics:
• Each interior node has two children (left and right), making it
a binary tree.
• Traversing an interior node requires only one comparison.
• The maximum number of objects in a tree of height (h) is (2^h).
4. Example Structure:
typedef struct tr_n_t {
key_t key;
struct tr_n_t *left; // Points to the left subtree
struct tr_n_t *right; // Points to the right subtree
/* possibly additional information */
} tree_node_t;

Model 2: Node Trees


1. Structure:
• In this model, each node can be considered a ternary node with
three possible outcomes:
• Traverse left if the query key is smaller.
• Traverse right if the query key is larger.
• If the query key is equal to the node key, the
object contained in the node is returned.
2. Traversal:
• The traversal logic is similar, but it includes an additional case
for equality:
• If the query key is equal to the node key, the object
is returned immediately.

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

3. Characteristics:
• Each node can have one object (the key) and two children (left and
right), leading to a ternary-like structure.
• Traversing an interior node requires two comparisons (one for
the left/right decision and one for equality).
• The maximum number of objects in a tree of height ( h ) is
( 2^{h+1} - 1 ).
4. Example Structure:
• The structure is similar to Model 1, but with the understanding
that each node can also contain an object:
typedef struct tr_n_t {
key_tkey; // The key for comparison
object_t *object; // The object associated with the
key struct tr_n_t *left; // Points to the left
subtree
struct tr_n_t *right; // Points to the right subtree
} tree_node_t;

General Properties of Search Trees:


1. Intervals Associated with Nodes:
• Each node in a search tree can be associated with an interval of
possible key values.
• The interval for the root node is (]-\infty, \infty[).
• For an interior node (n) with an interval ([a, b[):
• The key (n->key) is within ([a, b[).
• The left child has an interval ([a, n->key[).
• The right child has an interval ([n->key, b[).

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

• These intervals help in understanding the operations on the tree


and maintaining the search tree properties.

2. Structure of Search Trees:


• The leaves of the tree contain the actual (key, object) pairs
in increasing order of keys.
• Different search trees can represent the same set of (key,
object) pairs, but their structures can vary significantly.

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

Transformations: Left and Right Rotations


1. Left Rotation:
• A left rotation around a node (n) rearranges three nodes: ( n->left
), ( n->right->left ), and ( n->right->right ).
• The left rotation is a local change that can be performed in
constant time and does not affect the content of the nodes or the
structure above or below the rotation centre.
• Code for Left Rotation:
void left_rotation(tree_node_t *n)
{ tree_node_t *tmp_node;
key_ttmp_key;
tmp_node = n->left;
tmp_key = n->key;
n->left = n->right;
n->key = n->right->key;
n->right = n->left->right;
n->left->right = n->left-
>left; n->left->left =
tmp_node;
n->left->key = tmp_key;
}
2. Right Rotation:
• A right rotation is the inverse operation of a left rotation and
rearranges the same three nodes in the opposite manner.
• Code for Right Rotation:
void right_rotation(tree_node_t *n)
{ tree_node_t *tmp_node;
key_ttmp_key;

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

tmp_node = n->right;
tmp_key = n->key;
n->right = n->left;
n->key = n->left->key;
n->left = n->right-
>left;
n->right->left = n->right-
>right; n->right->right =
tmp_node;
n->right->key = tmp_key;
}

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

Height of a Search Tree


1. Definition:
• The height of a search tree is defined as the maximum length of a
path from the root to any leaf. It is the longest distance from the
root node to the furthest leaf node in the tree.
• The depth of a specific node is the distance from the root to that
node.
2. Properties:
• The maximum number of leaves in a search tree of height ( h ) is
( 2^h ).
• The minimum number of leaves in a search tree of height ( h ) is
( h + 1 ) because a tree of height ( h ) must have at least one
interior node at each depth from ( 0 ) to ( h - 1 ).
Theorems on Height

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

Theorem:
A search tree for n objects has height at least [log n] and at most n −
1. It is easy to see that both bounds can be reached.
The height is the worst-case distance we have to traverse to reach
a specific object in the search tree. Another related measure of quality of
a search tree is the average depth of the leaves, that is, the average over
all objects of the distance we have to go to reach that object. Here the
bounds are:

Theorem:
A search tree for n objects has average depth at least log n and at most
(n−1)(n+2) / 2n ≈ 1/2n.

To prove these bounds,


it is easier to take the sum of the depths instead of the average depth.
Because the sum of depths can be divided in the depth of the a leaves to
the left of the root and the depth of the b leaves to the right of the root,
these sums satisfy the following recursions:

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

In the first case, one uses that the function x log x is convex, so
a log a + b log b ≥ (a + b) log (a + b)/2.

Basic Operations on Search Trees

{ find( tree, query key): Returns the object associated with query key, if there is
one;
{ insert( tree, key, object ): Inserts the (key, object) pair in the tree; and

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

{ delete( tree, key): Deletes the object associated with key from the tree

1. Find Operation
Definition: The find operation searches for a specific key in the search tree and
returns the associated object if the key exists.
Algorithm:
• Start at the root of the tree.
• Traverse the tree based on comparisons:
• If the query key is less than the current node's key, move to the
left child.
• If the query key is greater than the current node's key, move to
the right child.
• If a leaf node is reached and the key does not match, return NULL.
• If the key matches the current node's key, return the associated object.
Code Implementation: Here are two versions of the find function: an iterative
version and a recursive version.

Recursive Version:
object_t *find(tree_node_t *tree, key_tquery_key) {
if (tree->left == NULL || (tree->right == NULL && tree->key != query_key))
{
return NULL; // Tree is empty or key not found
} else if (tree->right == NULL && tree->key == query_key)
{ return (object_t *) tree->left; // Return the associated object
} else {
if (query_key< tree->key) {
return find(tree->left, query_key); // Search left subtree
} else {
return find(tree->right, query_key); // Search right subtree
}
}

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

}
2. Insert Operation
Definition: The insert operation adds a new (key, object) pair to the search tree.

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

Algorithm:
• Start at the root of the tree.
• Traverse the tree to find the correct position for the new key:
• If the new key is less than the current node's key, move to the
left child.
• If the new key is greater than the current node's key, move to
the right child.
• If a leaf node is reached and the key already exists, return an error.
• Create a new interior node and a new leaf node for the new key and
object.
• Adjust the pointers to maintain the binary search tree properties.
Code Implementation:

Delete Operation:
Definition: The delete operation removes a (key, object) pair from the search
tree based on the specified key. If the key exists, the associated object is
returned, and the tree is restructured to maintain its properties.
Algorithm
1. Check for Empty Tree: If the tree is empty (i.e., the left child of the
root is NULL), return NULL since there is nothing to delete.
2. Leaf Node Deletion: If the current node is a leaf (i.e., it has no right
child):
• If the key of the current node matches the delete_key, store the
object, set the left child to NULL, and return the object.
Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

• If the key does not match, return NULL.


3. Interior Node Deletion: If the current node is not a leaf:

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

• Traverse the tree to find the node containing the key to be


deleted, keeping track of the current node (tmp_node) and its
parent (upper_node).
• If the key is found, replace the key and associated object of
the upper_node with the key and object of the other_node (the child
node that will take its place).
• Return the object associated with the deleted node.
4. Memory Management: After deleting the node, ensure that the object is
not lost. If the object is the only reference, it should be deleted to prevent
memory leaks.

Methods for Returning from Leaf to Root


1. Using a Stack:
• Description: During the descent to the leaf, pointers to all
traversed nodes are pushed onto a stack. When returning, nodes
can be popped from the stack in reverse order.
• Advantages:

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

• Clean and efficient; does not modify the tree structure.


• The maximum size of the stack is limited to the height of
the tree, which is logarithmic for balanced trees.
• Implementation: This method is implicitly used in recursive
implementations of search trees, where the call stack serves as
the path tracker.
2. Back Pointers:
• Description: Each node contains an additional pointer to its parent
node, allowing traversal back to the root.
• Advantages:
• Provides a direct path back to the root without needing a
stack.
• Disadvantages:
• Requires additional memory for the back pointer in
each node.
• The back pointers must be updated correctly during all
tree operations, which can introduce programming errors.
3. Back Pointers with Lazy Update:
• Description: Similar to back pointers, but the parent pointer is
only updated during the descent to the leaf. This means that the
back pointers are only guaranteed to be correct for the nodes on the
path just traversed.
• Advantages:
• Reduces the overhead of maintaining back pointers during
all operations.
• Disadvantages:
• The back pointers may not be accurate for nodes not on the
most recent path, which can lead to issues if the tree
structure changes.
4. Reversing the Path:

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

• Description: Instead of maintaining back pointers, the forward


pointers are temporarily reversed as the descent occurs. If moving
left, the left pointer is used as a back pointer; if moving right, the
right pointer is used.
• Advantages:
• No additional space is required for back pointers or a stack.
• Disadvantages:
• This method temporarily destroys the search tree structure,
which can lead to complications and errors.
• It is generally not recommended due to the potential for
introducing bugs and the complexity of restoring the original
pointers.
Handling Nonunique Keys in Search Trees:
1. Find Operation:
• Objective: Return all objects associated with a given key.
• Time Complexity: (O(h + k)), where ( h ) is the height of the
tree and ( k ) is the number of objects found.
• Implementation:
• When searching for a key, traverse the tree to the
appropriate leaf node.
• At the leaf node, access a linked list that contains all objects
with that key.
• Return all elements in the linked list.
2. Insert Operation:
• Objective: Insert a new object with a given key into the tree.
• Time Complexity: (O(h)), where ( h ) is the height of the tree.

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

• Implementation:
• Traverse the tree to find the appropriate leaf node for the
key.
• Insert the new object at the beginning of the linked list
associated with that key.
3. Delete Operation:
• Objective: Delete all objects associated with a given key.
• Time Complexity: ( O(h) ), where ( h ) is the height of the tree.
• Implementation:
• Traverse the tree to find the leaf node for the key.
• Remove the linked list of objects associated with that key.
• To efficiently manage memory, maintain an additional node
between the leaf and the linked list that contains pointers to
the beginning and end of the list. This allows for quick
deletion of the entire list in ( O(1) ) time.

Queries for the Keys in an Interval:


1. Summarize Queries for the Keys in an Interval.

This operation involves finding all the keys within a specified range [a,b][a,
b] in a search tree, such as a Binary Search Tree (BST). The goal is to
efficiently retrieve all the keys that fall within the given interval.

How to Perform Interval Queries in a BST:

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

1. In-Order Traversal: Perform an in-order traversal of the BST, which


visits nodes in sorted order. While traversing, only process nodes with
keys within the specified interval [a,b][a, b].
2. Left Subtree: If the current node's key is greater than or equal to the
lower bound aa, recursively visit the left subtree. This ensures that all
keys less than or equal to the current node's key are considered.
3. Current Node: If the current node's key is within the interval [a,b][a,
b], include it in the result.
4. Right Subtree: If the current node's key is less than or equal to the
upper bound bb, recursively visit the right subtree. This ensures that
all keys greater than or equal to the current node's key are
considered.
Example:
Consider the following BST:
15
/ \
10 20
/ \ / \
5 12 17 25
If we want to find all keys in the interval [10,20][10, 20]:
 Start at the root (15).
o 15 is within the interval, so include it.
o Traverse the left subtree.
 10 is within the interval, so include it.
 Traverse the left subtree (5 is not within the interval, so ignore it).
 Traverse the right subtree (12 is within the interval, so include it).
o Traverse the right subtree.
 20 is within the interval, so include it.
 Traverse the left subtree (17 is within the interval, so include it).
 Traverse the right subtree (25 is not within the interval,
so ignore it).

Building Optimal Search Trees:

An optimal search tree, also known as an optimal binary search tree (OBST), is a binary
search tree that minimizes the expected search cost, given the probabilities of searching for
each key. The process of building an OBST involves dynamic programming.
Here's a simplified explanation:
1. Identify Frequencies: Determine the probability of searching for each
key, as well as the probability of searches that result in no match (i.e.,
between keys).
2. Create Cost and Root Matrices: Use dynamic programming to
create two matrices: one for the cost and one for the root of the
subtree.
3. Fill Matrices: Calculate the optimal cost for subtrees of increasing
sizes, using the probabilities and previously computed values.
4. Construct Tree: Use the root matrix to construct the tree.

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

Diagram:
Keys: 10 20 30
Probabilities: p1 p2 p3
[10]
/ \
NULL [20]
\ [30]
The diagram shows an example where 10, 20, and 30 are the keys, and the tree
is constructed to minimize the expected search cost.

Converting Trees into Lists


Occasionally one also needs the other direction, converting a tree into an ordered list. This is very simple, using a
stack for a trivial depth-first search enumeration of the leaves in decreasing order, which we insert in front of the list.
This converts in O(n) time a search tree with n leaves into a list of n elements in increasing order. Again, we write the
generic stack functions, which in the specific implementation should be replaced by the correct method. If one knows
in advance that the height of the tree is not too large, an array is the preferred method; the size of the array needs to be
at least as large as the height of the tree.

2.10 Removing a Tree


We also need to provide a method to remove the tree when we no longer need it. As we
already remarked for the stacks, it is important to free all nodes in such a dynamically
allocated structure correctly, so that we avoid a memory leak. We cannot expect to remove a
structure of potentially large size in constant time, but time linear in the size of the structure,
that is, constant time per returned node, is easily reached. An obvious way to do this is using
a stack, analogous to the previous method to covert a tree into a sorted list. A more elegant
method is the following:
Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

This essentially performs rotations in the root till the left-lower neighbour is a leaf; then it
returns that leaf, moves the root down to the right, and returns the previous root.

Height-Balanced Trees:

A tree is height-balanced if, in each interior node, the height of the right
subtree and the height of the left subtree differ by at most 1. This is the
oldest balance criterion for trees, introduced and analyzed by G.M.
Adel’son-Vel’ski˘ı and E.M. Landis (1962), and still the most popular
variant of balanced search trees (AVL trees). A height-balanced tree has
necessarily small height.

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

Weight-Balanced Trees:

When Adel’son-Vel’ski˘ı and Landis invented the height-balanced search trees in 1962,
computers were extremely memory limited, so the applicability of the structure at that time
was small and only very few other papers on balanced search trees1 appeared in the 1960s.
But by 1970, technological development made it a feasible and useful structure, generating
much interest in the topic, and several alternative ways to maintain search trees at O(log n)
height were proposed. One natural alternative balance criterion is to balance the weight, that
is, the number of leaves, instead of the height of the subtrees.
The weight of a tree is the number of its leaves, so in a weight-balanced tree, the weight of
the left and right subtrees in each node should be “balanced”.An α-weight-balanced tree has
necessarily small height.

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

(a, b)- and B-Trees:

A different method to keep the height of the trees small is to allow tree nodes of higher
degree. This idea was introduced as B-trees by Bayer and McCreight (1972) and turned out
to be very fruitful. It was originally intended as external memory data structure. The
characteristic of external memory is that access to it is very slow, compared to main memory,
and is done in blocks, units much larger than single main memory locations, which are
simultaneously transferred into main memory. In the 1970s, computers were still very
memory limited but usually already had a large external memory, so that it was a necessary
consideration how a structure operates when a large part of it is not in main memory, but on
external memory. This situation is now less important, but it is still relevant for database
applications, where B-tree variants are still much used as index structures.
The problem with normal binary search trees as external memory structure is that each tree
node could be in a different external memory block, which becomes known only when the
previous block has been retrieved from the external memory. So we might need as many
external memory block accesses as the height of the tree, which is more than log2(n), and
would be interested in each of these blocks, which are large enough to hold many nodes, in
just a single node. The idea of B-trees is to take each block as a single node of high degree.
In the original version, each node has degree between a and 2a − 1, where a is chosen as
large as possible under the condition that a block must have room for 2a − 1 pointers and
keys. Then balance was maintained by the criterion that all leaves should be at the same
Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

depth.

Red-Black Trees and Trees of Almost Optimal Height


Introduction
Red-Black Trees are a type of self-balancing binary search tree that ensures the tree remains
approximately balanced, providing efficient performance for dynamic set operations. They
are particularly useful in applications where frequent insertions and deletions occur. This
answer discusses the properties of Red-Black Trees, their height characteristics, and the
concept of top-down rebalancing.

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

1. Red-Black Trees
Definition: A Red-Black Tree is a binary search tree with an additional color property for
each node, which can be either red or black. This coloring helps maintain balance during
insertions and deletions.
Properties: A Red-Black Tree must satisfy the following properties:
1. Node Color: Each node is either red or black.
2. Root Property: The root node is always black.
3. Red Property: Red nodes cannot have red children (no two reds in a row).
4. Black Property: Every path from a node to its descendant leaves must have the
same number of black nodes (black height).
5. Leaf Property: All leaves (NIL nodes) are considered black.
These properties ensure that the tree remains approximately balanced, leading to a height of
(O(\log n)), where (n) is the number of nodes.
Operations:
1. Insertion:
• Insert the new node as in a regular binary search tree.
• Color the new node red.
• If the parent node is red, perform rotations and recoloring to restore the
Red- Black properties.
2. Deletion:
• Remove the node as in a regular binary search tree.
• If the node is black, additional adjustments are needed to maintain
the properties, which may involve rotations and recoloring.
Complexity:
• Time Complexity: (O(\log n)) for search, insertion, and deletion.
• Space Complexity: (O(n)) for storing the nodes.
2. Trees of Almost Optimal Height
Definition: Trees of almost optimal height refer to trees that maintain a height close to the
theoretical minimum height for a given number of nodes. For binary search trees, the optimal
height is (O(\log n)). Red-Black Trees achieve this by ensuring that the longest path from the
root to a leaf is no more than twice the length of the shortest path.
Height Characteristics:

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

• The height of a Red-Black Tree is guaranteed to be at most (2 \log(n + 1)).


• This ensures that operations remain efficient, as the height is kept logarithmic
relative to the number of nodes.

3. Top-Down Rebalancing for Red-Black Trees


Definition: Top-Down Rebalancing is a technique used during the insertion of nodes in Red-
Black Trees to maintain the tree's properties. Instead of performing rebalancing after the
insertion (as in bottom-up approaches), the rebalancing is done as we traverse down the tree.
Algorithm:
1. Insertion:
• Start at the root and traverse down the tree to find the appropriate position
for the new node.
• Color the new node red.
• As you traverse, check the properties of the tree:
• If a violation occurs (i.e., a red parent with a red child),
perform rotations and recoloring immediately.
• This may involve rotating the tree to the left or right and changing
the colors of the nodes involved.
• Continue this process until the new node is inserted and the tree properties
are restored.
Advantages:
• Top-Down Rebalancing simplifies the insertion process by handling
violations immediately, reducing the need for multiple passes up the tree.
• It can lead to fewer rotations and recoloring operations compared to bottom-up
approaches.
Complexity:
• Time Complexity: (O(\log n)) for insertion with top-down rebalancing.
• Space Complexity: (O(n)) for storing the nodes.

Conclusion
Red-Black Trees are a powerful data structure that maintains balance through color
properties, ensuring efficient operations for dynamic datasets. Their height characteristics
guarantee that they remain close to optimal height, providing logarithmic time complexity
for

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

search, insertion, and deletion operations. Top-Down Rebalancing is an effective technique


for maintaining the properties of Red-Black Trees during insertions, enhancing performance
and simplifying the rebalancing process. Understanding these concepts is essential for
effective data structure management and algorithm design in various computational
problems.
Expected Questions Asked with Answers.
Question 1: Explain the two models of search trees and their general properties.
Answer: Search trees can be broadly classified into two models:
1. Binary Search Trees (BST):
• Each node has at most two children.
• For any node, all keys in the left subtree are less than the node's key, and
all keys in the right subtree are greater.
• Operations such as search, insert, and delete can be performed in (O(h))
time, where (h) is the height of the tree.
2. Balanced Search Trees:
• These trees maintain a balanced height to ensure efficient operations.
• Examples include AVL trees, Red-Black trees, and B-trees.
• They guarantee (O(\log n)) time complexity for search, insert, and delete
operations by keeping the tree height logarithmic relative to the number of
nodes.

Topic Voice Questions and Answers

Search Trees: Two Models of Search Trees, General Properties, and Transformations
Introduction
Search trees are a fundamental data structure used to maintain a dynamic set of items,
allowing for efficient search, insertion, and deletion operations. They are particularly useful
for applications that require frequent access to sorted data.
Two Models of Search Trees

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

1. Binary Search Trees (BST):


• A binary search tree is a tree data structure where each node has at most
two children, referred to as the left and right child.
• Properties:
• For any given node, all keys in the left subtree are less than the
node's key, and all keys in the right subtree are greater.
• This property allows for efficient searching, as one can eliminate
half of the remaining nodes at each step.
• Complexity:
• Average case for search, insert, and delete operations is (O(\log n)),
but in the worst case (e.g., when the tree becomes unbalanced), it can
degrade to (O(n)).
2. Balanced Search Trees:
• Balanced search trees are designed to maintain a balanced height,
ensuring that operations remain efficient.
• Examples include:
• AVL Trees: A type of height-balanced tree where the difference in
heights between the left and right subtrees of any node is at most
one.
• Red-Black Trees: A self-balancing binary search tree with an
additional color property (red or black) that ensures the tree
remains approximately balanced.
• B-Trees: A generalization of binary search trees that can have
more than two children, commonly used in databases and file
systems for efficient disk access.

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS
lOMoARcPSD|45529801

Transformations in Search Trees


Transformations in search trees refer to the operations that can be performed to maintain or
improve the structure of the tree. These include:
1. Rotations:
• Used in balanced trees (like AVL and Red-Black trees) to maintain
balance after insertions and deletions.
• Types of Rotations:
• Single Rotation: A single left or right rotation is performed to
restore balance.
• Double Rotation: A combination of two rotations (left-right or
right- left) is used when a single rotation is insufficient.
2. Rebalancing:
• After insertions or deletions, the tree may need to be rebalanced to ensure
that it maintains its properties.
• This is particularly important in height-balanced trees like AVL trees, where
the balance factor (the difference in heights of left and right subtrees) must
be monitored and adjusted.
3. Tree Transformations:
• Converting a binary search tree into a sorted linked list or vice versa can
be done through in-order traversal.
• Merging two search trees into a single tree while maintaining the search
tree properties.

Downloaded
Downloaded by armar
Kinjal abdul
Algud([email protected])
NIS

You might also like