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

dsa unit 4

The document discusses search trees, particularly focusing on symbol tables used by compilers to manage identifiers and their associated information. It explains the differences between static and dynamic symbol tables, their operations, and advantages, as well as the concept of optimal binary search trees (OBST) and their implementation in dynamic programming. Additionally, it provides examples and calculations related to tree costs and comparisons.

Uploaded by

itsfun206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

dsa unit 4

The document discusses search trees, particularly focusing on symbol tables used by compilers to manage identifiers and their associated information. It explains the differences between static and dynamic symbol tables, their operations, and advantages, as well as the concept of optimal binary search trees (OBST) and their implementation in dynamic programming. Additionally, it provides examples and calculations related to tree costs and comparisons.

Uploaded by

itsfun206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

1 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Unit – IV
Search Trees
This PDF is watermarked and traceable. Unauthorized sharing will result in
permanent access ban and legal action.

1. Search Trees
Search trees are a type of data structure used to
store data in a hierarchical form to allow for
efficient searching, inserting, deleting, and traversal of elements.

1.1 Symbol Table

A symbol table is a data structure used mainly by compilers. It keeps track of all identifiers (like
variable names, function names, etc.) used in a program, and stores related information such as:

 Name – The identifier’s name (e.g., x)

 Value – The value it holds (e.g., 10)

 Address – Its memory location

 Type – Data type (e.g., int, float)

Why is it Important?

When the compiler scans a program(like your C code), it needs to remember:


2 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

 What identifiers are declared

 Where they are stored in memory

 What type they are

 And what value they hold

The symbol table handles all of this — allowing the compiler to track and manage
identifiers efficiently.

Types of Symbol Tables

1. Static Symbol Table

 Stores a fixed amount of information.

 Built during compile time and does not change.

 Example structures:

o Optimal Binary Search Tree (OBST)

o Huffman Coding Tree

2. Dynamic Symbol Table

 Allows insertion/deletionof symbols at runtime.

 Used in dynamic programming languages or situations where the program changes


while running.

 Example structure:

o AVL Tree

Use of Symbol in Static and Dynamic Structures

Symbol tables are used to implement:

 Static tree tables → using static symbol tables

 Dynamic tree tables → using dynamic symbol tables

Advantages of Symbol Tables

1. Fast Lookup : Helps the compiler quickly find identifiers during compilation.
3 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

2. Memory Management : Assists in allocating memory for identifiers at runtime.

3. Scope Handling : Manages variable scopes and avoids errors like multiple or implicit
declarations.

Operations on Symbol Table

The main operations are:

1. Insertion – Add a new identifier and its info.

Steps:

1. Take the identifier (e.g., int a).

2. Check if it already exists in the current scope.

3. If not, store its:

o Name

o Type

o Scope

o Memory location (optional)

4. Add the entry to the table.

2. Deletion – Remove an identifier (if it goes out of scope).

Steps:

1. A block or function ends. 2. Identify all

variables declared in that scope. 3. Remove their

entries from the symbol table. Search – Look up

3. an identifier to get its details.

1. The compiler needs info about an identifier (e.g., a).

2. Search starts from the current scope.

3. If not found, move to outer scopes.

4. Return its data (type, memory, etc.), or give an error if not found.
4 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

1.2 Static Tree Table

A static tree table is a type of search tableimplemented using tree structures where:

 The data (identifiers) does not change after the table is created.

 It is constructed once at compile-time.

 It is used for fast and optimized searching.

So, once the tree is built, no insertion or deletion is allowed during execution — only
searching is performed.

From above two BSTs if we want to search a node A, then we compare key value A with 3
nodes, in case of Tree 1. But for Tree 2, to search node A will require 2 comparisons only. This
shows that arrangement of nodes is an important factor in searching. It is therefore necessary
to compute cost of a tree. The formula for computing the cost of a tree is:

∑(Number of Nodes ×Number of Comparisons)


Cost of a tree= Total Number of Nodes present in the tree

Let us now compute the cost of above 2 trees.

Tree 1 Cost Computation:

Node Number of Comparisons


ABC 321
5 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

∑(Number of Nodes ×Number of Comparisons)


Cost of a tree= Total Number of Nodes present in the tree

(1×3)+(1×2)+(1×1)
= =2
3

Cost of Tree 1 = 2.

Tree 2 Cost Computation:

Node Number of Comparisons


A, C 21
B

∑(Number of Nodes ×Number of Comparisons) (2×2)+(1×1)


Cost of a tree= Total Number of Nodes present in the tree = 3

=1.66

Cost of Tree 2 = 1.66.

Examples of Static Tree Table

Optimal Binary Search Tree (OBST): Tree built to minimize average search cost based on
frequency of use

Huffman Tree: Used for compression (e.g., zip files) based on symbol frequency

1.3 Dynamic Tree Table


A dynamic tree table is a data structure used to store and manage identifiers (like variables,
functions, etc.) in such a way that:

 New entries can be inserted

 Old entries can be deleted

 Existing entries can be searched or updated

 The structure adjusts itself to stay efficient

It’s called “dynamic” because the data can change at any time — unlike static tree tables
where data is fixed after construction.

Where is it Used?
6 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Dynamic tree tables are used:

 In dynamic compilers or interpreters (e.g., Python, JavaScript)

 In situations where the program keeps changing during execution

 In real-time systemsor interactive environments where data is constantly updated

How is it Implemented?

It’s implemented using self-balancing binary search trees, such as:

AVL Tree: Keeps height balanced after insert/delete

Red-Black Tree: Balances using coloring rules

Splay Tree: Moves recently accessed item to the root

Advantages of Dynamic Tree Table

 Supports real-time data updates

 Ensures fast operations even with large data

 Automatically balances itself

 Useful in modern languages and compilers

1.4 Weight Balanced Tree - Optimal Binary Search Tree (OBST)


A weight balanced tree is a binary search tree that is optimized based on the search
probabilities (or weights) of the keys.

In simple words: It arranges keys in such a way that the most frequently searched items
appear closer to the root, reducing the average search time.

An Optimal Binary Search Tree (OBST) is a special type of weight-balanced tree where:

 Each key has a search frequency (how often it’s searched).

 The tree is built in a way that minimizes the total search cost.

Why OBST?

In real-life applications like compilers or dictionaries:

 Some identifiers or words are accessed more frequently than others.


7 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

 If we place the frequently used keys nearer the root, searching becomes faster
overall.

So, OBST is used to achieve this optimal arrangement.

Let’s take an example for better understanding:

Let’s assume that we have the following keys in our tree: 15, 25, 35, 45, 55, 65, 75

The maximum time required to search a node in a binary search tree is equal to the minimum
height of the tree i.e. log n.

If we have keys: 15, 25, 35, then see how many BSTs can be formed. We will use the formula
given below:

1 2𝑛
Number of BSTs=𝐶௡= 𝑛 + 1 ൬ ൰
𝑛

For n = 3 (keys: 15, 25, 35), the number of BSTs is:

1 6 1
Number of BSTs=𝐶ଷ= ൬൰= ∙20=5
3 + 1 3 4

So, we can see that 5 trees will be formed. Now to find average number of comparisons:

Case 1:

1+2+3
Avg.number of comparisons= 3 =2

Case 2:

ଵ ାଶା ଷ
Avg.number of comparisons= =2

8 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Case 3:

1 + 2 + 2 5
Avg.number of comparisons= =
3 3

Case 4:

1+2+3
Avg.number of comparisons= 3 =2

Case 5:

1+2+3
Avg.number of comparisons= 3 =2

In the third case, the total number of comparisons is less as compared to


others because the height of the tree is less. So we can say that it’s a balanced binary search
tree.

1.5 OBST as an example of Dynamic Programming

Dynamic Programming is used when:

1. The problem can be broken into overlapping subproblems.

2. These subproblems have optimal substructure (optimal solution of full problem


depends on optimal solutions of subproblems).

OBST satisfies both — so we solve it using bottom-up DP.

DP Table Setup

Let:

 n = number of keys

 freq[i] = frequency of the i-th key

 co st [i] [j] = minimum cost of OBST containing keys from i to j


9 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

We’ll compute this using a DP table co st [0…n-1][0… n- 1]with these steps:

Algorithm Steps:

1. Initialize cost[i][i] = freq[i] → when the subtree has only 1 key.

2. For chain lengths from 2 to n:

o For each i and j = i + length - 1:

 Try making each key k from i to j the root.

 Calculate cost as:

cost[i][j] = min{(cost[i][k − 1] + cost[k][j])} + weight[i][j]

 Save the minimum in cost[i][j].

Example: Consider the below table, which contains the keys and frequencies.

First, we will calculate the values where j-i is equal to zero.

When i=0, j=0, then j-i = 0

When i = 1, j=1, then j-i = 0

When i = 2, j=2, then j-i = 0

When i = 3, j=3, then j-i = 0

When i = 4, j=4, then j-i = 0


10 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Therefore, c[0, 0] = 0, c[1, 1] = 0, c[2,2] = 0, c[3,3] = 0, c[4,4] = 0

Now we will calculate the values where j-i equal to 1.

When j=1, i=0 then j-i = 1

When j=2, i=1 then j-i = 1

When j=3, i=2 then j-i = 1

When j=4, i=3 then j-i = 1

Now to clculate the cost, we will consider only the jth value.

The cost of c[0,1] is 4 (The key is 10, and the cost corresponding to key 10 is 4).

The cost of c[1,2] is 2 (The key is 20, and the cost corresponding to key 20 is 2).

The cost of c[2,3] is 6 (The key is 30, and the cost corresponding to key 30 is 6)

The cost of c[3,4] is 3 (The key is 40, and the cost corresponding to key 40 is 3)

Now, we find the remaining values.

As i < k ≤ j

To find c[0,2], k = 1, 2

c[0,2] = min{(c[0][1 − 1] + c[1][2]), (c[0][2 − 1] + c[2][2])} + w[0][2]

= min{(c[0][0] + c[1][2]), (c[0][1] + c[2][2])} + w[0][2]

= min{0 + 2, 4 + 0} = min{2, 4} + 6

= 2 + 6 = 81

To find c[1,3], k = 2, 3
11 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

c[1,3] = min{(c[1][2 − 1] + c[2][3]), (c[1][3 − 1] + c[3][3])} + w[1][3]

= min{(c[1][1] + c[2][3]), (c[1][2] + c[3][3])} + w[0][2]

= min{0 + 6, 2 + 0} = min{6, 2} + 8

= 6 + 8 = 103

To find c[2,4], k = 3, 4

c[2,4] = min{(c[2][3 − 1] + c[3][4]), (c[2][4 − 1] + c[4][4])} + w[2][4]

= min{(c[2][2] + c[3][4]), (c[2][3] + c[4][4])} + w[2][4]

= min{0 + 3, 6 + 0} = min{3, 6} + 9

= 3 + 9 = 123

To find c[0,3], k = 1, 2, 3

c[0,3] = min{(c[0][1 − 1] + c[1][3]), (c[0][2 − 1] + c[2][3]), (c[0][3 − 1] + c[3][3])} + w[0][3]

= min{(c[0][0] + c[1][3]), (c[0][1] + c[2][3]), (c[0][2] + c[][3])} + w[0][3]

= min{0 + 10, 4 + 6, 8 + 0} = min{10, 10, 8} + 12

= 8 + 12 = 203

To find c[1,4], k = 2, 3, 4

c[1,4] = min{(c[1][2 − 1] + c[2][4]), (c[1][3 − 1] + c[3][4]), (c[1][4 − 1] + c[4][4])} + w[1][4]

= min{(c[1][1] + c[2][4]), (c[1][2] + c[3][4]), (c[1][3] + c[4][4])} + w[1][4]

= min{0 + 12, 2 + 3, 10 + 0} = min{12, 5, 10} + 11

= 5 + 11 = 163
12 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

To find c[0,4], k = 1, 2, 3, 4

c[0,4] = min{(c[0][1 − 1] + c[1][4]), (c[0][2 − 1] + c[2][4]), (c[0][3 − 1] + c[3][4]), (c[0][4 −


1] + c[4][4])} + w[0][4]

= min{(c[0][0] + c[1][4]), (c[0][1] + c[2][4]), (c[0][2] + c[3][4]) , (c[0][3] + c[4][4])}


+ w[0][4]

= min{0 + 16, 4 + 12, 8 + 3, 20 + 0} = min{16, 16, 11, 20} + 15

= 11 + 15 = 263

The optimal binary tree can be created as:


13 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

1.6 Height Balanced Tree

A Height Balanced Tree is a binary tree where the height difference (also called balance
factor) between the left and right subtrees ofevery node is at most 1.

Balance Factor = Height of left subtree − Height of right subtree

Allowed values: -1, 0, or +1

Why is Height Balance Important?

 In normal Binary Search Trees (BST), if elements are inserted in sorted order, the tree
becomes skewed (like a linked list), and searching takes O(n) time.

 Height-balanced trees keep the height as low as possible , ensuring searching,

inserting, and deleting take O(log n) time.

Example of Height Balanced Trees

The most common height-balanced trees are:

1. AVL Tree (named after Adelson-Velsky and Landis)

o Strict height balancing.

o Balance factor must be -1, 0, or +1.

o Rotations are used to restore balance after insert/delete.

2. Red-Black Tree

o Loosely height balanced.

o Allows slightly more imbalance but maintains O(log n) operations.

1.7 AVL tree


14 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

An AVL tree is defined as a self-balancing Binary Search Tree (BST) where the difference
between heights of left and right subtrees for any node cannot be more than one.

 The absolute difference between the heights of the left subtree and the right subtree for
any node is known as the balance factor of the node. The balance factor for all nodes
must be less than or equal to 1.

 Every AVL tree is also a Binary Search Tree (Left subtree values are smaller and right
subtree values are greater for every node), but every BST is not an AVL Tree.

 The main advantage of an AVL Tree is, the time complexities of all operations (search,
insert and delete, max, min, floor and ceiling) become O(log n). This happens because
height of an AVL tree is bounded by O(log n). In case of a normal BST, the height can
go up to O(n).

 An AVL tree maitains its height by doing some extra work during insert and delete
operations. It mainly uses rotations to maintain both BST properties and height balance.

 There exist other self-balancing BSTs also like Red Black Tree. Red Black tree is more
complex, but used more in practice as it is less restrictive in terms of left and right
subtree height differences.

Example of an AVL Tree

The balance factors for different nodes are: 12 :1, 8:1, 18:1, 5:1, 11:0, 17:0 and 4:0. Since all
differences are less than or equal to 1, the tree is an AVL tree.

Operations on an AVL Tree:

Searching
: It is the same as a normal Binary Search Tree (BST) as an AVL Tree is always a BST. So, we
can use the same implementation as BST. The advantage here is time complexity is O(log n).
15 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Insertion: It does rotations along with normal BST insertion to make sure that the balance
factor of the impacted nodes is less than or equal to 1 after insertion

Deletion: It also does rotations along with normal BST deletion to make sure that the balance
factor of the impacted nodes is less than or equal to 1 after deletion.

Example:

Replace 14 with its inorder succssor i.e. 18, and delete 14.

If we encounter unbalancing in subtree then we balance that subtree using appropriate single
or double rotations.

Rotating the subtrees (Used in Insertion and Deletion)

An AVL tree may rotate in one of the following four ways to keep itself balanced while making
sure that the BST properties are maintained.

Left Rotation:

When a node is added into the right subtree of the right subtree, if the tree gets out of balance,
we do a single left rotation.
16 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Algorithm/Pseudocode:

Node* leftRotate(Node* x) {
Node* y = x->right; // Step 1: y is x's right child
Node* T2 = y->left; // Step 2: T2 stores y's left subtree

y->left = x; // Step 3: Rotate - x becomes left child of y


x->right = T2; // Step 4: Move T2 to be x's right child

// Step 5: Update heights


x->height = max(getHeight(x->left), getHeight(x->right)) + 1; y-
>height = max(getHeight(y->left), getHeight(y->right)) + 1;

return y; // Step 6: Return new root


}

Right Rotation:

If a node is added to the left subtree of the left subtree, the AVL tree may get out of balance,
we do a single right rotation.

Algorithm/Pseudocode:
17 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Node* rightRotate(Node* y) {
Node* x = y->left; // Step 1: x is y's left child
Node* T2 = x->right; // Step 2: T2 stores x's right subtree

x->right = y; // Step 3: Rotate - y becomes right child of x


y->left = T2; // Step 4: Move T2 to be y's left child

// Step 5: Update heights (assuming a getHeight() function exists)


y->height = max(getHeight(y->left), getHeight(y->right)) + 1;
t = max(getHeight(x->left), getHeight(x->right)) + 1;
x->heig

return x; // Step 6: Return new root


}

Left-Right Rotation:

A left-right rotation is a combination in which first left rotation takes place after that right
rotation executes.

Algorithm/Pseudocode:

Node* leftRightRotate(Node* z) {
// Step 1: Store pointer to left child of z
Node* x = z->left;

// Step 2: Store pointer to right child of x


Node* y = x->right;
18 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

// Step 3: Perform rotation adjustments


x->right = y->left; z- // y's left subtree becomes x's right child // y's
>left = y->right; right subtree becomes z's left child

// Step 4: Make y the new root of this subtree


y->left = x;
y->right = z;

// Step 5: Update heights


x->heig t = max(getHeight(x->left), getHeight(x->right)) + 1;
z->height = max(getHeight(z->left), getHeight(z->right)) + 1;
y->height = m x(getHeight(y->left), getHeight(y->right)) + 1;

return y; // Step 6: Return new root


}

Right-Left Rotation:

A right-left rotation is a combination in which first right rotation takes place after that left
rotation executes.

Algorithm/Pseudocode:

Node* rightLeftRotate(Node* z) {
// Step 1: Store pointer to right child of z
Node* x = z->right;
19 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

// Step 2: Store pointer to left child of x


Node* y = x->left;

// Step 3: Perform rotation adjustments


x->left = y->right; // y's right subtree becomes x's left child
z->right = y->left; // y's left subtree becomes z's right child

// Step 4: Make y the new root of this subtree


y->right = x;
y->left = z;

// Step 5: Update heights


x->height = max(getHeight(x->left), getHeight(x->right)) + 1;
z->height = max(getH ight(z->left), getHeight(z->right)) + 1;
y->height = max(getHeight(y->left), getHeight(y->right)) + 1;

return y; // Step 6: Return new root


}

Example: Obtain AVL Tree for the following data: 30, 50, 110, 80, 40, 10, 120, 60, 20, 70,
100, 90.

Step 1: Insert 30. Step 2: Insert 50.

Step 3: Insert 110.


20 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Step 4: Insert 80. Step 5: Insert 40.

Step 6: Insert 10. Step 7: Insert 120.

Step 8: Insert 60. Step 9: Insert 20.

Step 10:
Insert 70.
21 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Step 11:Insert 100.

Step 12: Insert 90.

Example: Obtain an AVL Tree by accepting the following numbers (keys) one at a time. Show
balance factor of each node, the type of rotation and the transformation required for each
insertion in AVL Tree. 30, 31, 32, 23, 22, 28, 24, 29, 26, 27, 34, 36.

Final Answer for confirmation:

Advantages of AVL Tree:


22 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

1. AVL trees can self-balance themselves and provide time complexity of O(log n) for
search, insert and delete.

2. It is a BST (with balancing), so items can be traversed in sorted order.

3. Since the balancing rules are strict compared to Red Black Tree, AVL trees in general
have relatively less height and hence the search is faster.

4. AVL tree is relatively less complex to understand and implement compared to Red
Black Trees.

Disadvantages of AVL Tree:

1. It is difficult to implement compared to normal BST and easier compared to Red Black.

2. Less used compared to Red-Black trees. Due to its rather strict balance, AVL trees
provide complicated insertion and removal operations as more rotations are performed.

Applications of AVL Tree:

1. AVL Tree is used as a first example self-balancing BST in teaching DSA as it is easier
to understand and implement compared to Red Black.

2. Applications, where insertions and deletions are less common but frequent data lookups
along with other operations of BST like sorted traversal, floor, ceil, min and max.

3. Red Black tree is more commonly implemented in language libraries like map in
C++, set in C++, TreeMap in Java and TreeSet in Java.

4. AVL Trees can be used in a real time environment where predictable and consistent
performance is required.

1.8 Red-Black Tree

A Red-Black Tree is a self-


balancing Binary Search Tree
(BST) where each node follows
some color-based rules to keep
the tree approximately
balanced, ensuring O(log n)
time for insertion, deletion, and
search.
23 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Properties of a Red-Black Tree

Every node in the tree follows these 5 rules:

1. Each node is either RED or BLACK

2. The root is always BLACK

3. All leaves (NULL pointers) are considered BLACK

4. RED nodes cannot have RED children (No two reds in a row)

5. Every path from a node to its descendant NULL nodes must have the same number

of BLACK nodes (called black-height)

Why Red-Black Tree?

 Keeps tree height early O(log n)


 Balancing is less strict than AVL but faster for inserts and deletes
 Used in real-world applications like Java TreeMap, C++ STL map/set

Insertion in Red-Black Tree

In the Red-Black tree, we use two tools to do the balancing.

1. Recoloring

2. Rotation

Recoloring is the change in color of the node i.e. if it is red then change it to black and vice
versa. It must be noted that the color of the NULL node is always black. Moreover, we always
try recoloring first, if recoloring doesn’t work, then we go for rotation. Following is a detailed
algorithm. The algorithms have mainly two cases depending upon the color of the uncle. If the
uncle is red, we do recolor. If the uncle is black, we do rotations and/or recoloring.

The representation we will be working with is:


24 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

First, we have to insert the node similarly to that in a binary tree and assign a red color to it.
Now, if the node is a root node, then change its color to black, but if it is not, then check the
color of the parent node. If its color is black then don’t change the color but if it is not i.e. it is
red then check the color of the node’s uncle. If the node’s uncle has a red color, then change
the color of the node’s parent and uncle to black and that of grandfather to red color and
repeat the same process for him (i.e. grandfather). If grandfather is root, then don’t change
grandfather to red color.

But if the node’s uncle has black color, then there are 4 possible cases:

Left Left Case (LL rotation):

Left Right Case (LR rotation):


25 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Right Right Case (RR rotation):

Right Left Case (RL rotation):

Example: Insert 2, 1, 4, 5, 9, 3, 6 and 7 for Red Black Tree.

Step 1: Insert 2. Initially the red-black tree is empty. The newly inserted node should always
be red.
26 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Step 2: Insert 1. Make node for value 1 and it should be red, as it is a newly inserted node.
Since 1 < 2, attach this red node as left child of 2.

Step 3: Insert 4. Make node 4 as red node and attach it as right child of 2.

Step 4: Insert 5. Here, node 5 is a red node attached as a right child of red node 4. But there
should not be a red child of a red node. So, we make adjustments.

Step 5: Insert node 9.

Insert node 9 as a right child of 5. Naturally the node 9 is red initially.


27 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Step 6: Insert 3. Create a new node which is red in color and whose value is 3. Attach it as left
child of 4.

Step 7: Insert 6. Create a new node which is red in color and whose value is 6. Attach this node
as a left child of 9.
28 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Step 8: Insert 7. Create a new node which is red in color and whose value is 7. Attach this node
as right child of node 6.

Thus, we get the final Red-Black Tree:


29 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Deletion in Red-Black Tree

Deletion in a red-black tree is a bit more complicated than insertion. When a node is to be
deleted, it can either have no children, one child or two children.

Here are the steps involved in deleting a node in a red-black tree:

1. If the node to be deleted has no children, simply remove it and update the parent node.

2. If the node to be deleted has only one child, replace the node with its child.

3. If the node to be deleted has two children, then replace the node with its in-order
successor, which is the leftmost node in the right subtree. Then delete the in-order
successor node as if it has at most one child.

4. After the node is deleted, the red-black properties might be violated. To restore these
properties, some color changes and rotations are performed on the nodes in the tree.
The changes are similar to those performed during insertion, but with different
conditions.

5. The deletion operation in a red-black tree takes O(log n) time on average, making it a
good choice for searching and deleting elements in large data sets.

Deletion is a fairly complex process. To understand deletion, the notion of double black is
used. When a black node is deleted and replaced by a black child, the child is marked as double
black. The main task now becomes to convert this double black to single black. Following are
detailed steps for deletion.

1) Perform standard BST deletion. When we perform standard delete operation in BST, we
always end up deleting a node which is an either leaf or has only one child (For an internal
node, we copy the successor and then recursively call delete for successor, successor is always
30 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

a leaf node or a node with one child). So, we only need to handle cases where a node is leaf or
has one child. Let v be the node to be deleted and u be the child that replaces v (Note that u is
NULL when v is a leaf and color of NULL is considered as Black).

2) Simple Case: If either u or v is red, we mark the replaced child as black (No change in
black height). Note that both u and v cannot be red as v is parent of u and two consecutive reds
are not allowed in red-black tree.

3) If Both u and v are Black.

3.1) Color u as double black. Now our task reduces to convert this double black to single black.
Note that if v is a leaf node, then u is NULL and color of NULL is considered black. So, the
deletion of a black leaf also causes a double black.

3.2) Do following while the current node u is double black, and it is not the root. Let sibling of
node be s.

(a): If sibling s is black and at least one of sibling’s children is red, perform rotation(s). Let
the red child of s be r. This case can be divided in four subcases depending upon positions of
s and r.
31 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

(i) Left Left Case (s is left child of its parent and r is left child of s or both children of s are
red). This is mirror of right right case shown in below diagram.

(ii) Left Right Case (s is left child of its parent and r is right child). This is mirror of right left
case shown in below diagram.

(iii) Right Right Case (s is right child of its parent and r is right child of s or both children of
s are red)

(iv) Right Left Case (s is right child of its parent and r is left child of s)

(b): If sibling is black and its both children are black , perform recoloring, and recur for the
parent if parent is black.
32 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

In this case, if parent was red, then we didn’t need to recur for parent, we can simply make it
black (red + double black = single black)

(c): If sibling is red, perform a rotation to move old sibling up, recolor the old sibling and
parent. The new sibling is always black (See the below diagram). This mainly converts the tree
to black sibling case (by rotation) and leads to case (a) or (b). This case can be divided in two
subcases.
(i) Left Case (s is left child of its parent). This is mirror of right right case shown in below
diagram. We right rotate the parent p.

(ii) Right Case (s is right child of its parent). We left rotate the parent p.
33 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

1.9 AA tree
An AA Tree is a simplified version of a Red-Black Tree. It maintains balance using a single value
called "level" instead of using colors like red and black. It enforces strict rules to keep the tree
balanced and operations efficient.

It is named AA after Arne Andersson, the person who introduced it.

Key Concepts

 AA Tree is a Binary Search Tree (BST) with added balancing constraints.

 Each node stores:

o Key (data)

o Level ( number similar to height or black-height)

 All leaves have level 1

 Only right horizontal links are allowed (no left horizontal links)

AA Tree Properties

1. Level of every leaf node is 1

2. Level of every left child is strictly less than its parent

3. Level of every right child is equal to or less than the parent

4. No two consecutive right horizontal links (i.e., if node A → B → C are all right
children, it violates the rule)

5. The level of a right grandchild must be less than its grandparent

Advantages of AA Trees
34 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

1. Simpler than Red-Black Trees : Only uses one field (level) and two operations: skew
& split

2. Balanced BST : Maintains O(log n) for search, insert, and delete

3. Easier to implement & debug : No color rules or complex balancing cases

4. Fewer rotations: Only needs right (skew) and left (split) rotations

5. Good performance: Comparable to Red-Black Trees in practice

Insertion in AA Trees

The nodes are inserted similar to binary search algorithm.

If the parent and its child are on the same level then horizontal links are possible.

There are some restrictios on horizontal links. These restrictions are for avoiding unbalancing
in AA trees. Following figure represents the restrictions on horizontal links and remedy on
violation.

The nodes are inserted similar to binary search algorithm.

If the parent and its child are on the same level then horizontal links are possible.

There are some restrictions on horizontal links. These restrictions are for avoiding unbalancing
in AA trees. Following figure represents the restrictions on horizontal links and remedy on
violation.

1) Skew Operation: The skew operation is a single right rotation when an insertion creates
horizontal link.
35 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

 In this operation, the left horizontal link is removed.


 It may create consecutive right horizontal links in process.

2) Split Operation: The split operation is single left rotation when insertion creates two
horizontal links.

 In this operation, two consecutive right horizontal links are removed.


 It causes level of middle node to be increased by one.

Example: Insert the elements 8, 3, 10, 18, 12, 2 in AA Tree.

We insert the elements one by one. We apple skew and split operations when horizontal links
cause unbalancing in the tree.

Step 1: Insert 8.

Step 2: Insert 3.
36 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

But since the right left child is not allowed in AA tree, we apply skew operation to balance the
AA Tree.

Step 3: Insert 10.

Here two consecutive horizontal links occur. This is not allowed. Hence, we apply split
operation. The resultant tree will be
37 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Step 4: Insert 18.

Step 5: Insert 12.

Again, left link from 18 to 12 is not allowed. Hence, we apply skew operation. The balanced
AA tree will then be
38 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

But still two horizontal links are not allowed. Hence, we apply split operation.

Step 6: Insert 2.

The left horizontal link is not allowed. Hence, we apply skew operation.

This, the AA tree is created.

Deletion in AA Tree

The node is deleted similar to binary search tree.

 To delete a leaf node with no children, simply remove the node.


 To delete node with one child, replace node with its child.
 To delete an internal node, replace that node with either its successor or predecessor.

This deletion may cause unbalancing in AA tree which can be rebalanced with the help of skip
and skew operations.

Following are the rules that can be applied to rebalance the AA tree.

Rule 1: Decrease the level of the node when

 If the child node is more than one level down.


 A node is right horizontal child of another node whose level was decreased.

Rule 2: Skew the level of the node whose level was decreased.
39 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

 Skew the subtree from the root node, where the decremented node is root.
 Skew the root node's right child.
 Skew the root node's right-right child.

Rule 3: Split the level of the node whose level was decreased.

 Split the root node of the subtree.


 Split the root node's right child.

Example: Delete 4 from the following AA Tree.

Step 1: Delete node 4.

It can also be represented as follows:


40 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Step 2: Decrease the level of root, i.e. 8 and its right child, i.e. 16.

Step 3: Skew for right child of 8.

That means make 8 as parent of 12 and 12 as parent of 16. Attach 14 as left child of 16.

Step 4: Skew at root’s right right. That means, focus at node 16.

In this skew operation, 14 becomes parent of 16. And the right child of 14 which was actually
15 becomes left child of 16.

Step 5: Now we get consecutive horizontal red links. To balance such AA tree, we will perform
split operation.
41 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

First split will be at root.

Step 6: Now consider root's right 14, for split operation.

Thus, now we get balanced AA tree.

1.10 K-Dimensional tree


42 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

A K-Dimensional Tree (KD-Tree) is a binary tree used to store k-dimensional points (like
(x, y), (x, y, z), etc.) and perform efficient searches like:

 Range searches

 Nearest neighbor searches

It's like a Binary Search Tree (BST), but for multiple dimensions.

Example: Draw a K-Dimensional tree for following points. (5,8), (18,14), (12,14), (8,11),
(10,2), (3,6), (11,20).

Step 1: We will read the point (5,8) and make it the root node. The root is said to be x-aligned.

Step 2: Now, the next point is (18,14). As root is x aligned, we will compare x value of (5,8)
with x value of (18,14). That means compare 18 with 5. As 18 > 5, make (18,14) the right child
of (5,8).

The node is y-aligned.

Step 3: Now read (12,14). We start from root node. The x-value of (5,8) is compared with x-
value of (12,14).

As 12 > 5, move on to right branch.

Here (18,14) is y-aligned. Hence, the y-value of (18,14) is compared with y-value of (12,14).
As 14=14 move on to right branch and attach (12,14) as right child of (18,14).
43 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Step 4: The next point is (8,11). Starting from root, we will compare x and y values of (8,11)
alternatively and attach (8,11) as left child of (18,14). The partial construction is as follows:

Step 5: Next point is (10,2).

10 > 5 (from 5,8)), 10 < 14 (from(18,14)),

10 > 8 (from 8,11)). Hence attach (10,2) as a right child of (8,11).

Continuing, we get the following tree:

Step 6: These points can be plotted on X-Y plane.

i) For point (5,8), X=5. Hence draw a vertical line at x=5.

ii) For point (3,6), Y=6. Hence draw a horizontal line at Y=6. But as (3,6) is a left child of child
of (5,8), draw this line at the left of x=5 line.
44 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

In this way, the lines X and Y can be drawn to divide the space. The K-D space spotting is as
shown below:

Advantages of K-D Tree

1. Efficient Multi-Dimensional Search : Faster than brute force for searching in k-


dimensional space.

2. Good for Nearest Neighbor & Range Queries: Helps in quickly finding nearby points
or points in a region.

3. Simple & Structure: Easier to implement than other spatial data structures like R-
trees.

4. Works Well for Moderate Dimensions : Especially effective when k ≤ 10.

Applications of K-D Tree

1. Machine Learning : For nearest neighbor classification (e.g., k-NN algorithm).


45 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

2. Computer Graphics : For ray tracing and visibility problems.

3. Robotics & Pathfinding : To find nearest obstacles or destinations.

4. Databases & GIS : Efficient searching in spatial databases.

5. Image Retrieval : For comparing image features in multi-dimensional space.

1.11 Splay Tree

A Splay Tree is a type of self-adjusting binary search tree (BST). It reorganizes itself every
time you access a node — by moving the accessed node to the root .

This operation is called splaying.

What is Splaying?

Splaying is a series of rotations (like in AVL) that moves a node to the root of the tree.
This helps frequently accessed nodesstay near the top, making future accesses faster.

Why use Splay Trees?

 No need to store extra info (like balance factor or color).

 Faster access to frequently used data.

 Great for caching, text editors, and memory management.

Rotation Types in Splaying

TheZig
1) ZigRotation:
Rotation in splay trees operates in a manner similar to the single right rotation in AVL
Tree rotations. This rotation results in nodes moving one position to the right from their
current location. For example, consider the following scenario:
46 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

2) Zag Rotation: The Zag Rotation in splay trees operates in a similar fashion to the single left
rotation in AVL Tree rotations. During this rotation, nodes shift one position to the left from
their current location. For instance, consider the following illustration:

3) Zig-Zig Rotation: The Zig-Zig Rotation in splay trees is a double zig rotation. This rotation
results in nodes shifting two positions to the right from their current location. Take a look at
the following example for a better understanding:

4) Zag-Zag Rotation: In splay trees, the Zag-Zag Rotation is a double zag rotation. This
rotation causes nodes to move two positions to the left from their present position. For example:
47 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

5) Zig-Zag Rotation: The Zig-Zag Rotation in splay trees is a combination of a zig rotation
followed by a zag rotation. As a result of this rotation, nodes shift one position to the right and
then one position to the left from their current location. The following illustration provides a
visual representation of this concept:

6) Zag-Zig Rotation: The Zag-Zig Rotation in splay trees is a series of zag rotations followed
by a zig rotation. This results in nodes moving one position to the left, followed by a shift one
position to the right from their current location. The following illustration offers a visual
representation of this concept:

Example: Consider an empty splay tree and insert 0, 2, 4, 6, 8, 13, 11.

Step 1: Insert 0. No splaying is required.


48 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Step 2: Insert 2.

Step 3: Insert 4.

Step 4: Insert 6.

Step 5: Insert 8.

Step 6: Insert 13.


49 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Step 7: Insert 11.

Deletion in Splay Tree:

If we want to delete the node 8, we just copy the in-order successor of node 8. As there is no
in-order successor of 8, we delete 8. Then the parent node of the deleted node, i.e. 6 becomes
root node, by applying zig operation.
50 | Page © Haris Chaus | ALL RIGHTS ARE RESERVED as per copyright act.

Delete 12: Now to delete 12, we copy 15 at 12's place. And then allow 10 (i.e. parent of 12) to
be the root node. Then we get:

This is the required splay tree.

Advantages of Splay Tree

1. No Extra Storage Needed : Doesn’t require height or color info like AVL or Red-Black
trees.

2. Fast Access to Frequently Used Nodes: Recently accessed nodes are moved to the
root.

3. Amortized O(log n) Time : Over a sequence of operations, it stays efficient.

4. Simpler Implementation : Easier than AVL/Red-Black in terms of code.

Applications of Splay Tree

1. Caches: Frequently accessed data is quickly reachable.

2. Garbage Collection / Memory Management

3. Text Editors : For maintaining cursor positions or frequently edited lines.

4. Network Routing Tables : Access patterns tend to repeat, benefiting from splaying.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy