Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 37

DS

UNIT-3 Notes
1. What is Tree and Tree terminology?
Tree-
Tree is a non-linear data structure which organizes data in a hierarchical structure and this is a recursive
definition.
OR
A tree is a connected graph without any circuits.
OR
If in a graph, there is one and only one path between every pair of vertices, then graph is called as a tree.
Tree Terminology-
The important terms related to tree data structure are-
 

1. Root-
 The first node from where the tree originates is called as a root node.
 In any tree, there must be only one root node.
 We can never have multiple root nodes in a tree data structure.

Example-

 
 
Here, node A is the only root node.
2. Edge-
 The connecting link between any two nodes is called as an edge.
 In a tree with n number of nodes, there are exactly (n-1) number of edges.
 
Example-
 

 
3. Parent-
 The node which has a branch from it to any other node is called as a parent node.
 In other words, the node which has one or more children is called as a parent node.
 In a tree, a parent node can have any number of child nodes.
Example-

 
Here,
 Node A is the parent of nodes B and C
 Node B is the parent of nodes D, E and F
 Node C is the parent of nodes G and H
 Node E is the parent of nodes I and J
 Node G is the parent of node K
 
4. Child- 
 The node which is a descendant of some node is called as a child node.
 All the nodes except root node are child nodes.
Example-

Here,
 Nodes B and C are the children of node A
 Nodes D, E and F are the children of node B
 Nodes G and H are the children of node C
 Nodes I and J are the children of node E
 Node K is the child of node G
 
5. Siblings-
 Nodes which belong to the same parent are called as siblings.
 In other words, nodes with the same parent are sibling nodes.
Example-
 

 Here,
 Nodes B and C are siblings
 Nodes D, E and F are siblings
 Nodes G and H are siblings
 Nodes I and J are siblings
 
6. Degree-
 Degree of a node is the total number of children of that node.
 Degree of a tree is the highest degree of a node among all the nodes in the tree.
Example-
 
 
Here,
 Degree of node A = 2, Degree of node B = 3, Degree of node C = 2, Degree of node D = 0
Degree of node E = 2, Degree of node F = 0, Degree of node G = 1, Degree of node H = 0
Degree of node I = 0, Degree of node J = 0, Degree of node K = 0
 
7. Internal Node-
 The node which has at least one child is called as an internal node.
 Internal nodes are also called as non-terminal nodes.
 Every non-leaf node is an internal node.
Example-
 

 
Here, nodes A, B, C, E and G are internal nodes. 
8. Leaf Node-
 The node which does not have any child is called as a leaf node.
 Leaf nodes are also called as external nodes or terminal nodes.
Example-
 
 
Here, nodes D, I, J, F, K and H are leaf nodes.
 9. Level-
 In a tree, each step from top to bottom is called as level of a tree.
 The level count starts with 0 and increments by 1 at each level or step.
Example-
 

 
10. Height-
 Total number of edges that lies on the longest (downward) path from any node to a leaf node is called
as height of that node.
 Height of a tree is the height of root node.
 Height of all leaf nodes = 0
Example-
 

 
Here, Height of node A = 3, Height of node B = 2, Height of node C = 2, Height of node D = 0 Height of
node E = 1, Height of node F = 0, Height of node G = 1, Height of node H = 0, Height of node I = 0, Height
of node J = 0, Height of node K = 0
 11. Depth-
 Total number of edges from root node to a particular node is called as depth of that node.
 Depth of a tree is the total number of edges from node to a root node in the longest (upward) path.
 Depth of the root node = 0
 The terms “level” and “depth” are used interchangeably.
Example-

 
 Here, Depth of node A = 0, Depth of node B = 1, Depth of node C = 1, Depth of node D = 2, Depth of node
E = 2, Depth of node F = 2, Depth of node G = 2, Depth of node H = 2, Depth of node I = 3, Depth of node J
= 3, Depth of node K = 3
 12. Subtree-
 In a tree, each child from a node forms a subtree recursively.
 Every child node forms a subtree on its parent node.
Example- 

 
13. Forest-
A forest is a set of disjoint trees.
Example-
 

Advantages of trees

 Trees reflect structural relationships in the data


 Trees are used to represent hierarchies
 Trees provide an efficient insertion and searching
 Trees are very flexible data, allowing to move sub trees around with minimum effort
 2. What is a Binary Tree and state its types?
Binary Tree is a special type of generic tree in which, each node can have at most two children. 
OR
A binary tree is a tree data structure in which each node has at most two children, which are referred to as
the left child and the right child.
 A binary tree is made of nodes, where each node contains a "left" reference, a "right" reference, and a data
element. 

Left_Child Data Right_Child

Here,
In the above tree each node contains Left_Child , Data and Right_Child .
Node 1 is the root and its Left_Child is pointing to node 2 and Right_Child to node 3.
Node 2 is the child of node 1 and its Left_Child is pointing to node 5 and Right_Child to node 4.
Node 4 is a leaf node because it has no children.
Node 5 is the child of node 2 and its Left_Child is pointing to node 6 and Right_Child to node 7.
Both Node 6 and 7 are leaf nodes.
Node 3 is the child of node 1 and its Left_Child and Right_Child are pointing to nothing thus it is a leaf
node.
Types of Binary Tree:

Full Binary Tree:


A Binary Tree is full if every node has 0 or 2 children. We can also say a full binary tree is a binary tree in
which all nodes except leaves have two children. It is also known as proper binary tree or 2-tree.
In a Full Binary, number of leaf nodes is number of internal nodes plus 1
       L = I + 1
Where L = Number of leaf nodes, I = Number of internal nodes
Complete Binary Tree:
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled,
and all nodes are as far left as possible.
Perfect Binary Tree:
A Binary tree is Perfect Binary Tree in which all internal nodes have two children and all leaves are at the
same level.A Perfect Binary Tree of height h (where height is the number of nodes on the path from the root
to leaf) has 2h – 1 node.Example of a Perfect binary tree is ancestors in the family. Keep a person at root,
parents as children, and parents of parents as their children.

Binary Tree representation

There are two types of representation of a binary tree:

1. Linked Representation: In this representation, the binary tree is stored in the memory, in the form of a
linked list where the number of nodes are stored at non-contiguous memory locations and linked together by
inheriting parent child relationship like a tree.

2. Sequential Representation: In this representation, an array is used to store the tree elements. Size of the
array will be equal to the number of nodes present in the tree. The root node of the tree will be present at the
1st index of the array.

3. What is Binary Search Tree?


Binary Search Tree is a node-based binary tree data structure which has the following properties:
 The left subtree of a node contains only nodes with keys lesser than the root’s key.
 The right subtree of a node contains only nodes with keys greater than the root’s key.
 The left and right subtree each must also be a binary search tree.

A Binary search tree is shown in the above figure. As the constraint applied on the BST, we can see that the
root node 8 doesn't contain any value greater than or equal to 8 in its left sub-tree and it also doesn't contain
any value less than 8 in its right sub-tree.
Binary Search Tree

Type Tree

Time complexity in big O notation

Average Worst case

Space O(n) O(n)

Search O(log n) O(n)

Insert O(log n) O(n)

Delete O(log n) O(n)

Advantages of using binary search tree

1. Searching become very efficient in a binary search tree since, we get a hint at each step, about which
sub-tree contains the desired element.
2. The binary search tree is considered as efficient data structure in compare to arrays and linked lists.
In searching process, it removes half sub-tree at every step. Searching for an element in a binary
search tree takes o(log2n) time. In worst case, the time it takes to search an element is 0(n).
3. It also speeds up the insertion and deletion operations as compare to that in array and linked list.

4. Explain Binary tree Traversals with an example?

A traversal is a process that visits all the nodes in the tree. Since a tree is a nonlinear data structure, there is
no unique traversal. We will consider several traversal algorithms with we group in the following two kinds

 depth-first traversal
 breadth-first traversal

There are three different types of depth-first traversals, :

 PreOrder traversal -
 InOrder traversal –
 PostOrder traversal -

There is only one kind of breadth-first traversal--the level order traversal. This traversal visits nodes by
levels from top to bottom and from left to right.

Breadth First Traversal

Breadth First Traversal is a traversing way where child at same levels are read first before visiting their
child. which is nothing but a LEVEL-ORDER Traversal. 

Level order traversal: To traverse a binary tree in Level order, following operations are carried-out
(a) Visit the nodes of tree Level by level (ie, from left to right, level by level). 
(b) Here every Node of same Level is visited first before going to a lower levels. 
     Therefore, the Level order traversal of the above sample tree will outputs: 
     45  25  75  15  35
For reading a Tree Level by Level, we require Queue to store the Nodes.

Algorithm :
1.  Put a Root Node in a Queue.
2.  Iterate till the Queue is not Empty.
3.  While iterating, take one element from Queue say 45 in our example, print it and put children's of
     45 in queue.
4.  Repeat steps till Queue is not empty.
Level Order - 8, 5, 4, 9, 7, 11, 1, 12, 3, 2
Depth First Traversal
Pre Order Traversal - visit the parent first and then left and right children;
Algorithm: Preorder(tree)
1. Chech if(root!=NULL) then
2. Visit the root node and print data of node. 
3. Traverse the left subtree, i.e., call Preorder(left-subtree)
4. Traverse the right subtree, i.e., call Preorder(right-subtree)
PreOrder - 8, 5, 9, 7, 1, 12, 2, 4, 11, 3

Inorder Traversal - visit the left child, then the parent and the right child;

Algorithm: Inorder(tree)
1. Chech if(root!=NULL) then
2. Traverse the left subtree, i.e., call Inorder(left-subtree)
3. Visit the root node and print data of node.
4. Traverse the right subtree, i.e., call Inorder(right-subtree)
InOrder - 9, 5, 1, 7, 2, 12, 8, 4, 3, 11
Postorder traversal - visit left child, then the right child and then the parent;
Algorithm: Postorder(tree)
1. 1. Chech if(root!=NULL) then
2. Traverse the left subtree, i.e., call Postorder(left-subtree)
3. Traverse the right subtree, i.e., call Postorder(right-subtree)
4. Visit the root and print data of node.
PostOrder - 9, 1, 2, 12, 7, 5, 3, 11, 4, 8

5. Explain the Implementation of a BST with an example.


A BST can be implemented using linked lists i.e. using linked representation of binary trees.
For a BST the link contains info, left and right fields, whereas a list node requires only info and next fields.
Create a self-referential structure or structure “node”
Structure of BST:
struct node
{
int data;
struct node *right_child;
struct node *left_child;
};

Create the binary search tree using the following data elements.

43, 10, 79, 90, 12, 54, 11, 9, 50

1. Insert 43 into the tree as the root of the tree.


2. Read the next element, if it is lesser than the root node element, insert it as the root of the left sub-
tree.
3. Otherwise, insert it as the root of the right of the right sub-tree.

The process of creating BST by using the given elements is shown in the image below.

Difference between Binary Tree and Binary Search Tree:


 Definition of Binary Tree and Binary Search Tree – Binary Tree is a hierarchical data structure in
which a child can have zero, one, or maximum two child nodes; each node contains a left pointer, a right
pointer and a data element. There’s no particular order to how the nodes should be organized in the tree.
Binary Search Tree, on the other hand, is an ordered binary tree in which there is a relative order to how the
nodes should be organized.
 Structure  of Binary Tree and Binary Search Tree– The topmost node in the tree represents the root
pointer in a binary tree, and the left and the right pointers represent the smaller trees on either side. It’s a
specialized form of tree which represents data in a tree structure. Binary search tree, on the other hand, is a
type of binary tree in which all the nodes in the left subtree are less than or equal to the value of the root
node and that of the right subtree are greater than or equal to the value of the root node.
 Operation of Binary Tree and Binary Search Tree– Binary tree can be anything that has two children
and one parent. Common operations that can be performed on a binary tree are insertion, deletion, and
traversal. Binary search trees are more of sorted binary trees that allows for fast and efficient lookup,
insertion, and deletion of items. Unlike binary trees, binary search trees keep their keys sorted, so lookup
usually implements binary search for operations.
 Types of Binary Tree and Binary Search Tree– There are different types of binary trees, the common
being the “Full Binary Tree”, “Complete Binary Tree”, “Perfect Binary Tree”, and “Extended Binary Tree”.
Some common types of binary search trees include T-trees, AVL trees, Splay trees, Tango trees, Red-Black
trees etc.

Searching operation in BST


Searching means finding or locating some specific element or node within a data structure. However,
searching for some specific node in binary search tree is pretty easy due to the fact that, element in BST are
stored in a particular order.
Searching a binary search tree for a specific value can be a recursive or iterative process. This explanation
covers a recursive method. We begin by examining the root node.
If the tree is null, the value we are searching for does not exist in the tree.
Otherwise, if the value equals the root, the search is successful. If the value is less than the root, search the
left subtree.
Similarly, if it is greater than the root, search the right subtree.
This process is repeated until the value is found or the indicated subtree is null. If the searched value is not
found before a null subtree is reached, then the item must not be present in the tree.
This operation requires O(log n) time in the average case, but needs O(n) time in the worst case, when the
unbalanced tree resembles a linked list (degenerate tree).

Here is the search algorithm:

Search (ROOT, ITEM)

Step 1: If root->data=key or root=NULL

Return root

Step 2: Else if key>root->data

Return search(root->right_child,key)
Step 3: Else

Return search(root->left_child,key)

For example consider the following binary tree and find 60 in the BST.

1. Compare the 60 with the root of the tree i.e 50.


2. If the key is matched then return the location of the node.
3. Otherwise check if 60 is less than the element present on root(50), if so then move to the left sub-
tree.
4. But 60 > 50 thus move to the right sub-tree.
5. Now root is 75. 60 < 75 thus move to the left sub-tree.
6. 60 = 60 , return root.

Insertion operation in BST

Insertion begins as a search would begin; if the root is not equal to the value, we search the left or right
subtrees as before. Eventually, we will reach an external node and add the value as its right or left child,
depending on the node's value. In other words, we examine the root and recursively insert the new node to
the left subtree if the new value is less than the root, or the right subtree if the new value is greater than or
equal to the root.

Insert function is used to add a new element in a binary search tree at appropriate location. Insert function is
to be designed in such a way that, it must not violate the property of binary search tree at each value.

The algorithm for insertion is similar to search.

Here, is the insertion algorithm:


Insert(root,key)
Step 1: if root = NULL then
Create a temporary node ; root=temp
Step 2: Else if key > root->data then root->right_child = insert(root->right_child,key)
Step 3: Else root->left_child = insert(root->left_child,key)
Step 4: Finally return root

For example consider the following binary tree and insert 95 in the BST.

1. Allocate the memory for node.


2. Set the data part to the value and set the left and right pointer of node, point to NULL.
3. As 95 > 50, move to right sub-tree.
4. Now root is 75; 95 > 75 move to right sub-tree.
5. Now root is 85; 95 >85 as right_child of 85 is NULL point it to temp node in which 95 is present.
6. Return root and 95 is inserted in BST.

Deletion operation in BST:

Delete function is used to delete the specified node from a binary search tree. However, we must delete a
node from a binary search tree in such a way, that the property of binary search tree doesn't violate. There
are three situations of deleting a node from binary search tree.

Ther are three possible cases to consider:

Deleting a leaf node

Deleting a node with one child.


Deleting a node with two children.

The deletion algorithm is similar to search.

The node to be deleted is a leaf node

It is the simplest case, in this case, replace the leaf node with the NULL and free the allocated space.

Algorithm:

Step 1: if root->left_child and right_child are both equal to NULL(&&)

Step 2: free the memory of the root and return NULL

In the following BST, we are deleting the node 85, since the node is a leaf node, therefore the node will
be replaced with NULL and allocated space will be freed.

The node to be deleted has only one child.

In this case, replace the node with its child and delete the child node, which now contains the value which is
to be deleted. Simply replace it with the NULL and free the allocated space.

Algorithm:

Step 1: check if root->right_child or root->left_child are equal to NULL

Step 2: Create a temporary node

Step 3: if root->left_child = NULL then temp <- root->right_child

Similarly, if root->right_child =NULL then temp <- root->left_child

Step 4: free root and return temp.

In the following BST, the node 12 is to be deleted. It has only one child. The node will be replaced with its
child node and the replaced node 12 (which is now leaf node) will simply be deleted.
The node to be deleted has two children.

It is a bit complex case compare to other two cases. However, the node which is to be deleted, is replaced
with its in-order successor or predecessor recursively until the node value (to be deleted) is placed on the
leaf of the tree. After the procedure, replace the node with NULL and free the allocated space.

Algorithm:

Step 1: create a temp node and store the inorder successor [the node] of the key to be deleted in temp

Step 2: root->data = temp->data

Step 3: root->right_child = delete(root->right_child,temp->data)

Step 4: return root.

In the following BST, the node 50 is to be deleted which is the root node of the tree. The in-order traversal
of the tree given below.

6, 25, 30, 50, 52, 60, 70, 75.

replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the tree, which will simply be
deleted.

AVL Tree
AVL tree is a binary search tree where the height of the left subtree differs from the height of the right
subtree by at most 1 level, if it exceeds 1 level then rebalancing occurs.

OR

AVL tree is a self-balancing Binary Search Tree (BST) where the difference between heights of left and
right subtrees cannot be more than one for all nodes

i.e balancing factor for each node is either 0 or 1 or -1.

Below is a balanced AVL tree.

Below is a imbalanced AVL tree.

Why AVL Trees?[Advantages of AVL tree]

Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height of
the BST. The cost of these operations may become O(n) for a skewed Binary tree. If we make sure that height
of the tree remains O(Logn) after every insertion and deletion, then we can guarantee an upper bound of
O(Logn) for all these operations. The height of an AVL tree is always O(Logn) where n is the number of
nodes in the tree.
A skewed tree is a tree where each node has only one child node or none. This type of BST is similar to a
linked list.
AVL Trees are self- balancing Binary Search Trees (BSTs). A normal BST may be skewed to either side
which will result in a much greater effort to search for a key (the order will be much more
than O(log2n)O(log2n)) and sometimes equal O(nO(n) ) at times giving a worst case result which is the same
effort spent in sequential searching. This makes BSTs useless as a practically efficient Data Structure.
Height of AVL tree[Balance Factor]
Balance Factor: It is defined as the difference between height of left subtree and height of right subtree.

Balance Factor (k) = height (left(k)) - height (right(k))

If balance factor of any node is 1, it means that the left sub-tree is one level higher than the right sub-tree.

If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain equal height.

If balance factor of any node is -1, it means that the left sub-tree is one level lower than the right sub-tree.

An AVL tree is given in the following figure. We can see that, balance factor associated with each node is in
between -1 and +1. Therefore, it is an example of AVL tree.

AVL tree
Type Tree
Invented 1962
Invented by G.M.Adelson-Velskiiand E.M.Landis
Time complexity in big O notation
Average Worst case
Space O(n) O(n)
Search O(log n) O(log n)
Insert O(log n) O(log n)
Delete O(log n) O(log n)

AVL Tree Operations-


Like BST Operations, commonly performed operations on AVL tree are-
1. Search Operation
2. Insertion Operation
3. Deletion Operation
 After performing any operation on AVL tree, the balance factor of each node is checked.
There are following two cases possible-
Case-01:
 After the operation, the balance factor of each node is either 0 or 1 or -1.
 In this case, the AVL tree is considered to be balanced.
 The operation is concluded.
Case-02:
 After the operation, the balance factor of at least one node is not 0 or 1 or -1.
 In this case, the AVL tree is considered to be imbalanced.
 Rotations are then performed to balance the tree.
 
AVL Tree Rotations-
Rotation is the process of moving the nodes to make tree balanced.
Kinds of Rotations-
 There are 4 kinds of rotations possible in AVL Trees-
Cases Of Imbalance And Their Balancing Using Rotation Operations-
Case-01:

Case-02:

Case-03:
Case-04:

AVL Tree insertion

Insertion in AVL tree is performed in the same way as it is performed in a binary search tree. The new node
is added into AVL tree as the leaf node. However, it may lead to violation in the AVL tree property and
therefore the tree may need balancing.

To insert an element in the AVL tree, follow the following steps-


 Insert the element in the AVL tree in the same way the insertion is performed in BST.
 After insertion, check the balance factor of each node of the resulting tree.
Case-01:
 After the insertion, the balance factor of each node is either 0 or 1 or -1.
 In this case, the tree is considered to be balanced.
 Conclude the operation.
 Insert the next element if any.
Case-02:
 After the insertion, the balance factor of at least one node is not 0 or 1 or -1.
 In this case, the tree is considered to be imbalanced.
 Perform the suitable rotation to balance the tree.
 After the tree is balanced, insert the next element if any.

Rules To Remember while inserting element in AVL tree-


Rule-01:
After inserting an element in the existing AVL tree,
 Balance factor of only those nodes will be affected that lies on the path from the newly inserted node to
the root node.
Rule-02:
To check whether the AVL tree is still balanced or not after the insertion,
 There is no need to check the balance factor of every node.
 Check the balance factor of only those nodes that lies on the path from the newly inserted node to the root
node.
Rule-03:
After inserting an element in the AVL tree,
 If tree becomes imbalanced, then there exists one particular node in the tree by balancing which the entire
tree becomes balanced automatically.
 To re balance the tree, balance that particular node.
To find that particular node,
 Traverse the path from the newly inserted node to the root node.
 Check the balance factor of each node that is encountered while traversing the path.
 The first encountered imbalanced node will be the node that needs to be balanced.
To balance that node,
 Count three nodes in the direction of leaf node.
 Then, use the concept of AVL tree rotations to re balance the tree.

Algorithm:

Step 1: First, insert a new element into the tree using BST's (Binary Search Tree) insertion logic.

Step 2: After inserting the elements you have to check the Balance Factor of each node.

Step 3: When the Balance Factor of every node will be found like 0 or 1 or -1 then the algorithm will
proceed for the next operation.

Step 4: When the balance factor of any node comes other than the above three values then the tree is said to
be imbalanced. Then perform the suitable Rotation to make it balanced and then the algorithm will proceed
for the next operation.

Problem-[example: 1]
Q. Construct an AVL tree by inserting the following elements in the given order.

63, 9, 19, 27, 18, 108, 99, 81

The process of constructing an AVL tree from the given set of elements is shown in the following figure.

At each step, we must calculate the balance factor for every node, if it is found to be more than 2 or less than
-2, then we need a rotation to rebalance the tree. The type of rotation will be estimated by the location of the
inserted element with respect to the critical node.

All the elements are inserted in order to maintain the order of binary search tree.

STEPS FOR INSERTION:

Step-01: Insert 63

Step-02: Insert 9

As 9 < 63, so insert 9 in 63’s left sub tree.

Step-03: Insert 19

As 19 < 63, so insert 19 in 63’s left sub tree.

As 9 > 9, so insert 19 in 9’s right sub tree.


Step-04: check balancing as tree is not balanced

To balance the tree,

As the tree is imblanced and by seeing the tree we can perform LR rotation to balanace the tree.

Now the tree is balanced.

Step-05: Insert 27

As 27 > 19, so insert 27 in 19's right sub tree.

As 27 < 63, so insert 27 in 63's left sub tree.

Step-06: Insert 18

As 18< 19, so insert 18 in 19's left sub tree

As 18 > 9, so insert 18 in 9's right sub tree.

Step-07: Insert 108

As 108 > 19, so insert 108 in 19's right sub tree

As 108 > 63, so insert 108 in 63's right sub tree

Step-08: Insert 99

As 99 > 19, so insert 99 in 19's right sub tree

As 99 > 63, so insert 99 in 63's right sub tree

As 99 < 108, so insert 99 in 108's left sub tree

Step-09: Insert 81

As 81 > 19, so insert 81 in 19's right sub tree

As 81 > 63, so insert 81 in 63's right sub tree

As 81 < 108, so insert 81 in 108's left sub tree

As 81 < 99, so insert 81 in 99's left sub tree

To balance the tree,

As the tree is imbalanced and by seeing the tree we can perform LL rotation to balance the tree.

Now the tree is balanced.

Step-10: Balanced

This is the final balanced AVL tree after inserting all the given elements.
Problem-[example: 2]
Q. Insert an element to the below AVL tree.
Insert 8
STEPS :
As 8<50, so insert 8 in 50's left sub tree
As 8<20, so insert 8 in 20's left sub tree
As 8<10, so insert 8 in 10's left sub tree
As the tree is imblanced and by seeing the tree we can perform RR rotation to balanace the tree.
Now the tree is balanced.
Problem-[example: 3]
Q. Insert an element to the below AVL tree.
Insert 15
As 15>5, so insert 15 in 5's right sub tree
As 15>7, so insert 15 in 7's right sub tree
As 15>9, so insert 15 in 9's right sub tree
As 15<16, so insert 15 in 16's left sub tree
As 1<20, so insert 8 in 20's left sub tree
As 8<10, so insert 8 in 10's left sub tree
As the tree is imblanced and by seeing the tree we can perform RL rotation to balanace the tree.
Now the tree is balanced.

Deletion
Deleting a node from an AVL tree is similar to that in a binary search tree. Deletion may disturb the balance
factor of an AVL tree and therefore the tree needs to be rebalanced in order to maintain the AVL tree
properties. For this purpose, we need to perform rotations.

Algorithm:
Step 1: Firstly, find that node where k is stored{k=key}

Step 2: Secondly delete those contents of the node (Suppose the node is x)

Step 3: Claim: Deleting a node in an AVL tree can be reduced by deleting a leaf. There are three possible
cases:

 When x has no children then, delete x


 When x has one child, let x' becomes the child of x.
 Notice: x' cannot have a child, since subtrees of T can differ in height by at most one :
o then replace the contents of x with the contents of x'
o then delete x' (a leaf)
 Step 4:  When x has two children,
o then find x's successor z (which has no left child)
o then replace x's contents with z's contents, and
o delete z
In all of the three cases, you will end up removing a leaf.

Example:
Delete the node 30 from the following AVL tree

Searching
Searching a node from an AVL tree is similar to that in a binary search tree.
Algorithm:
Step 1: First compare your key to be searched with the root
 If it is equal to root then key is found.
Step 2:If key < root , move to left subtree and again compare elements and repeat same.
Step 3:If key > root , move to right subtree and again compare elements and repeat same.
Step 4: END.

Red-Black Tree

Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows following rules.

1) Every node has a color either red or black.

2) Root of tree is always black.

3) There are no two adjacent red nodes (A red node cannot have a red parent or red child).

4) Every path from a node (including root) to any of its descendant NULL node has the same number of
black nodes.

Why Red-Black Trees?

Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h is the height
of the BST. The cost of these operations may become O(n) for a skewed Binary tree. If we make sure that
height of the tree remains O(Logn) after every insertion and deletion, then we can guarantee an upper bound
of O(Logn) for all these operations. The height of a Red-Black tree is always O(Logn) where n is the
number of nodes in the tree.

Comparison with AVL Tree

The AVL trees are more balanced compared to Red-Black Trees, but they may cause more rotations during
insertion and deletion. So if your application involves many frequent insertions and deletions, then Red
Black trees should be preferred. And if the insertions and deletions are less frequent and search is a more
frequent operation, then AVL tree should be preferred over Red-Black Tree.

How does a Red-Black Tree ensure balance?


A simple example to understand balancing is, a chain of 3 nodes is not possible in the Red-Black tree. We
can try any combination of colours and see all of them violate Red-Black tree property.

A chain of 3 nodes is nodes is not possible in Red-Black Trees.


Following are NOT Red-Black Trees
303030
/\ / \ / \
20 NIL 20 NIL20 NIL
/\ /\ / \
10 NIL 10 NIL10NIL
Violates Violates Violates
Property 4. Property 4 Property 3

Following are different possible Red-Black Trees with above 3 keys


2020
/ \ / \
10 3010 30
/ \ / \ / \ / \
NIL NIL NIL NIL NIL NIL NIL NIL

From the above examples, we get some idea how Red-Black trees ensure balance. Following is an important

fact about balancing in Red-Black Trees.

Black Height of a Red-Black Tree :

Black height is number of black nodes on a path from root to a leaf. Leaf nodes are also counted black
nodes. From above properties 3 and 4, we can derive, a Red-Black

Tree of height h has black-height >= h/2.

Insertion in red-black tree

In AVL tree insertion, we used rotation as a tool to do balancing after insertion caused imbalance. In Red-
Black tree, we use two tools to do balancing.
1)  Recoloring
2) Rotation

We try recoloring first, if recoloring doesn’t work, then we go for rotation. Following is detailed algorithm.
The algorithms has mainly two cases depending upon the color of uncle. If uncle is red, we do recoloring. If
uncle is black, we do rotations and/or recoloring.Color of a NULL node is considered as BLACK.

Let x be the newly inserted node.

1) Perform standard BST insertion and make the color of

newly inserted nodes as RED.


2)  If x is root, change color of x as BLACK (Black height of
3) complete tree increases by 1).
4)  Do following if color of x’s parent is not BLACK and x is

not root.

….a) If x’s uncle is RED (Grand parent must have been

black from property 4)

……..(i) Change color of parent and uncle as BLACK.

……..(ii) color of grand parent as RED.

……..(iii) Change x = x’s grandparent, repeat steps 2 and3 for new x.

b) If x’s uncle is BLACK, then there can be four

configurations for x, x’s parent (p) and x’s grandparent (g)

(This is similar to AVL Tree)

i)  Left Left Case (p is left child of g and x is left child of p)


ii)  Left Right Case (p is left child of g and x is right child ofp
iii)  Right Right Case (Mirror of case i)

iv) Right Left Case (Mirror of case ii)

Following are operations to be performed in four subcases

when uncle is BLACK.

All four cases when Uncle is BLACK

Left Left Case (See g, p and x)

Left Right Case (See g, p and x)

Right Right Case (See g, p and x)


Right Left Case (See g, p and x)

Examples of Insertion
Deletion in red black trees

Insertion Vs Deletion:

Like Insertion, recoloring and rotations are used to

maintain the Red-Black properties.

In insert operation, we check color of uncle to decide the appropriate case. In delete operation, we check
color of sibling to decide the appropriate case.

The main property that violates after insertion is two consecutive reds. In delete, the main violated property
is, change of black height in subtrees as deletion of a black node may cause reduced black height in one root
to leaf path.

Deletion is fairly complex process.  To understand deletion, 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.

Deletion Steps
Following are detailed steps for deletion.
1) Perform standard BST delete. When we perform standard delete operation in BST, we always end up
deleting a node which is 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 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 leaf, then u is NULL and color of NULL is considered as 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 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.

…………..(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.
In this case, if parent was red, then we didn’t need to recur for prent, 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.
…………..(iii) Right Case (s is right child of its parent). We left rotate the parent p.

You might also like