Professional Documents
Culture Documents
Unit-3 Part - 1
Unit-3 Part - 1
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
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:
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.
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
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.
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
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 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
Create the binary search tree using the following data elements.
The process of creating BST by using the given elements is shown in the image below.
Return root
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.
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.
For example consider the following binary tree and insert 95 in the 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.
It is the simplest case, in this case, replace the leaf node with the NULL and free the allocated space.
Algorithm:
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.
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:
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
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.
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
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.
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)
Case-02:
Case-03:
Case-04:
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.
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.
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.
Step-01: Insert 63
Step-02: Insert 9
Step-03: Insert 19
As the tree is imblanced and by seeing the tree we can perform LR rotation to balanace the tree.
Step-05: Insert 27
Step-06: Insert 18
Step-08: Insert 99
Step-09: Insert 81
As the tree is imbalanced and by seeing the tree we can perform LL rotation to balance the tree.
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:
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.
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.
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.
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.
From the above examples, we get some idea how Red-Black trees ensure balance. Following is an important
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
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.
not root.
black from property 4)
Examples of Insertion
Deletion in red black trees
Insertion Vs Deletion:
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.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.