DS Unit 4
DS Unit 4
Root → The topmost node of the hierarchy is called the root of the tree.
Child → Nodes next in the hierarchy are the children of the previous node.
Parent → The node just previous to the current node is the parent of the current
node. Siblings → Nodes with the same parent are called siblings.
Ancestors → Nodes which are higher in the hierarchy are ancestors of a given node.
Descendents → Nodes which are lower in the hierarchy are descendants of a given
node. Internal Nodes → Nodes with at least one child are internal nodes.
External Nodes/Leaves → Nodes which don't have any child are called leaves of a
tree. Edge → The link
between two nodes is called an edge.
Level → The root of a tree is at level 0 and the nodes whose parent is root are at level 1 and so
on.
Height → The height of a node is the number of nodes (excluding the node) on the
longest pathfrom the node to a leaf.
Height of Tree → Height of a tree is the height of its root.
Depth → The depth of a node is the number of nodes (excluding the node) on the path from
the rootto the node.
A tree must have some properties so that we can differentiate from other data structures.
The numbers of nodes in a tree must be a finite and nonempty set.
There must exist a path to every node of a tree i.e., every node must be connected to some other node.
There must not be any cycles in the tree. It means that the number of edges is one less than the number
ofnodes.
Binary Trees
A binary tree is a tree in which every node has at most two children.
As you can see in the picture given above, a node can have less than 2 children but not more than
that.We can also classify a binary tree into different categories. Let's have a look at them:
Full Binary Tree → A binary tree in which every node has 2 children except the leaves is known as a
fullbinary tree.
Complete Binary Tree → A binary tree which is completely filled with a possible exception at the
bottomlevel i.e., the last level may not be completely filled and the bottom level is filled from left to
right.
∙
Perfect Binary Tree → In a perfect binary tree, each leaf is at the same level and all the interior nodes havetwo
children.
Thus, a perfect binary tree will have the maximum number of nodes for all alternative binary trees of thesame
We know that the maximum number of nodes will be in a perfect binary tree.
Number of nodes at level 0 = 2^0 =1
Number of nodes at level 1 = 2^1 =2
Similarly, the number of nodes at level h =
2^h
Array Representation of Binary Tree
We can also get the parent, the right child and the left child using the properties of a complete binary tree we have
discussed above i.e., for a node i, the left child is 2i and the right child is 2i+1.
So, we represented a complete binary tree using an array and saw how to get the parent and children of
anynode. Let's discuss about doing the same for an incomplete binary tree.
To represent an incomplete binary tree with an array, we first assume that all the nodes are present to
makeit a complete binary tree and then number the nodes as shown in the picture given below.
RIGHT_CHILD(index)
return (2*index + 1)
else
return null
LEFT_CHILD(index)
return (2*index)
else
return null
PARENT(index)
return floor(index/2)
else
return null
Linked List Representation of Binary Tree
We use a double linked list to represent a binary tree. In a double linked list, every node consists of three
fields. First field for storing left child address, second for storing actual data and third for storing right
child address.
In this linked list representation, a node has the following structure...
/
\
Applications of Binary tree
Binary trees are mainly used for searching and sorting as they provide a means to store data
hierarchically. ∙ Routing Table
∙ Decision Trees
∙ Expression Evaluation
▪ Preorder Traversal
▪ Postorder Traversal
Inorder Traversal:
To traverse a non empty tree in inorder the following steps are followed
recursively. ▪ Visit the Root
▪ Traverse the left subtree
▪ Traverse the right subtree
The inorder traversal of the tree shown below is as follows.
INORDER(n)
if(n != null)
INORDER(n.left)
print(n.data)
INORDER(n.right)
11
Preorder Traversal
Algorithm Pre-order(tree)
1. Visit the root.
2. Traverse the left sub-tree, i.e., call Pre-order(left-sub-tree) 3.
Traverse the right sub-tree, i.e., call Pre-order(right-sub-tree)
PREORDER(n)
if(n != null)
Postorder Traversal
Algorithm Post-order(tree)
1. Traverse the left sub-tree, i.e., call Post-order(left-sub-tree) 2.
Traverse the right sub-tree, i.e., call Post-order(right-sub-tree)
3. Visit the root.
POSTORDER(n)
if(n != null)
PREORDER(n.left) // visiting left subtree
In a binary tree, every node can have a maximum of two children but there is no need to maintain the
orderof nodes basing on their values. In a binary tree, the elements are arranged in the order they arrive at
the tree from top to bottom and left to right.
To enhance the performance of binary tree, we use a special type of binary tree known as Binary Search
Tree. Binary search tree mainly focuses on the search operation in a binary tree. Binary search tree can
be defined as follows...
Binary Search Tree is a binary tree in which every node contains only smaller values in its left subtree
and only larger values in its right subtree.
In a binary search tree, all the nodes in the left subtree of any node contains smaller values and all the
nodesin the right subtree of any node contains larger values as shown in the following figure...
Example
The following tree is a Binary Search Tree. In this tree, left subtree of every node contains nodes
withsmaller values and right subtree of every node contains larger values.
Every binary search tree is a binary tree but every binary tree need not to be binary search tree.
1. Search
2. Insertion
3. Deletion
Search Operation in BST
In a binary search tree, the search operation is performed with O(log n) time complexity.
def search(root,key):
# Base Cases: root is null or key is present at root
if root is None or root.val == key:
return root
# Key is greater than root's key
if root.val < key:
return search(root.right,key)
# Key is smaller than root's key
return search(root.left,key)
Insertion Operation in BST
In a binary search tree, the insertion operation is performed with O(log n) time complexity. In binary
searchtree, new node is always inserted as a leaf node. The insertion operation is performed as follows...
∙ Step 1 - Create a newNode with given value and set its left and right to NULL. ∙
∙ Step 4 - If the tree is Not Empty, then check whether the value of newNodeis smaller or
∙ Step 5 - If newNode is smaller than or equal to the node then move to its left child. If
newNodeis larger than the node then move to its right child.
∙ Step 6- Repeat the above steps until we reach to the leaf node (i.e., reaches to NULL). ∙ Step 7 -
After reaching the leaf node, insert the newNode as left child if the newNode is smaller orequal to
that leaf node or else insert it as right child.
Deletion Operation in BST
In a binary search tree, the deletion operation is performed with O(log n) time complexity. Deleting a
nodefrom Binary search tree includes following three cases...
∙ Step 2 - Delete the node using free function (If it is a leaf) and terminate the function.
50 50
/ \ delete(20) / \
30 70 ---------> 30 70
/\/\\/\
20 40 60 80 40 60 80
We use the following steps to delete a node with one child from BST...
∙ Step2 - If the node to be deleted has only one sub tree then the corresponding child is moved up to its
parent’s position
50 50
/ \ delete(30) / \
30 70 ---------> 40 70
\/\/\
40 60 80 60 80
Case 3: Deleting a node with two children
We use the following steps to delete a node with two children from BST...
Step 2 - If the node to be deleted has both sub tree then it is replaced with its in order successor.
50 60
/ \ delete(50) / \
40 70 ---------> 40 70
/\\
60 80 80
Example
AVL tree is a height-balanced binary search tree. That means, an AVL tree is also a binary search tree but
itis a balanced tree. A binary tree is said to be balanced if, the difference between the heights of left and
right subtrees of every node in the tree is either -1, 0 or +1. In other words, a binary tree is said to be
balanced if the height of left and right children of every node differ by either -1, 0 or +1. In an AVL tree,
every node maintains an extra information known as balance factor. The AVL tree was introduced in the
year 1962 by
An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of every node is either -
1, 0 or +1.
Balance factor of a node is the difference between the heights of the left and right subtrees of that node.
The balance factor of a node is calculated either height of left subtree - height of right subtree (OR)
height of right subtree - height of left subtree. In the following explanation, we calculate as follows...
Every AVL Tree is a binary search tree but every Binary Search Tree need not be AVL tree.
In AVL tree, after performing operations like insertion and deletion we need to check the balance factor
of every node in the tree. If every node satisfies the balance factor condition then we conclude the
operation otherwise we must make it balanced.
Whenever the tree becomes imbalanced due to any operation we use rotation operations to make the
tree balanced.
Rotation is the process of moving nodes either to left or to right to make the tree balanced.
In LL Rotation, every node moves one position to left from the current position. To understand LL Rotation,let us
consider the following insertion operation in AVL Tree...
Single Right Rotation (RR Rotation)
In RR Rotation, every node moves one position to right from the current position. To understand RR
Rotation, let us consider the following insertion operation in AVL Tree...
Left Right Rotation (LR Rotation)
The LR Rotation is a sequence of single left rotation followed by a single right rotation. In LR Rotation,
at first, every node moves one position to the left and one position to right from the current position. To
understand LR Rotation, let us consider the following insertion operation in AVL Tree...
The RL Rotation is sequence of single right rotation followed by single left rotation. In RL Rotation, at
first every node moves one position to right and one position to left from the current position. To
understand RL Rotation, let us consider the following insertion operation in AVL Tree...
Operations on an AVL Tree
1. Search
2. Insertion
3. Deletion
In an AVL tree, the search operation is performed with O(log n) time complexity. The search operation
inthe AVL tree is similar to the search operation in a Binary search tree. We use the following steps to
search an element in AVL tree...
∙ Step 2 - Compare the search element with the value of root node in the tree. ∙ Step 3 - If both
are matched, then display "Given node is found!!!" and terminate the function
∙ Step 4 - If both are not matched, then check whether search element is smaller or larger than that
node value.
∙ Step 5 - If search element is smaller, then continue the search process in left subtree.
∙ Step 6 - If search element is larger, then continue the search process in right subtree. ∙ Step
7 - Repeat the same until we find the exact element or until the search element is
comparedwith the leaf node.
∙ Step 8 - If we reach to the node having the value equal to the search value, then display
∙ Step 9 - If we reach to the leaf node and if it is also not matched with the search element,
In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL Tree, a
newnode is always inserted as a leaf node. The insertion operation is performed as follows...
∙ Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic. ∙
∙ Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next operation. ∙ Step 4 -
If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said to beimbalanced.
In this case, perform suitable Rotation to make it balanced and go for next operation.
The deletion operation in AVL Tree is similar to deletion operation in BST. But after every deletion
operation, we need to check with the Balance Factor condition. If the tree is balanced after deletion go for
next operation otherwise perform suitable rotation to make the tree Balanced.
∙ The keys in the first i children are smaller than the ith key
∙ The keys in the last m-i children are larger than the ith key
4-way search tree
M-way search trees give the same advantages to m-way trees that binary search trees gave tobinary trees - they
provide fast information retrieval and update. However, they also have the same problems that binary search trees
had - they can become unbalanced, which means that the construction of the tree becomes of vital importance.
Heap
A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Generally, Heaps can be
of two types:
1. Max-Heap: In a Max-Heap the key present at the root node must be greatest among the keys present at all of
it’s children. The same property must be recursively true for all sub-trees in that Binary Tree. 2. Min-Heap: In a
Min-Heap the key present at the root node must be minimum among the keys present at all of it’s children. The
same property must be recursively true for all sub-trees in that Binary Tree.
Applications of Heaps:
1) Heap Sort: Heap Sort uses Binary Heap to sort an array in O(nLogn) time.
2) Priority Queue: Priority queues can be efficiently implemented using Binary Heap because it supports insert(),
delete() and extractmax(), decreaseKey() operations in O(logn) time. Binomoial Heap and Fibonacci Heap are
variations of Binary Heap. These variations perform union also efficiently.
3) Graph Algorithms: The priority queues are especially used in Graph Algorithms like Dijkstra’s Shortest
Path and Prim’s Minimum Spanning Tree.
4) Many problems can be efficiently solved using Heaps. See following for example.
a) K’th Largest Element in an array.
b) Sort an almost sorted array/
c) Merge K Sorted Arrays.
Operations on Heap:
Delete operation
The standard deletion operation on Heap is to delete the element present at the root node of the Heap. That is if it is a
Max Heap, the standard deletion operation will delete the maximum element and if it is a Min heap, it will delete the
minimum element.
Process of Deletion:
Since deleting an element at any intermediary position in the heap can be costly, so we can simply replace the
element to be deleted by the last element and delete the last element of the Heap.
∙ Replace the root or element to be deleted by the last element.
∙ Since, the last element is now placed at the position of the root node. So, it may not follow the heap property.
Therefore, heapify the last node placed at the position of root.
Process:
The last element is 4.
Step 1: Replace the last element with root, and delete it.
4
/ \
5 3
/
2
Insert Operation
Process of Insertion: Elements can be inserted to the heap following a similar approach as discussed above for
deletion. The idea is to:
∙ First increase the heap size by 1, so that it can store the new element.
∙ This newly inserted element may distort the properties of Heap for its parents. So, in order to keep the properties of
Heap, heapify this newly inserted element following a bottom-up approach.