UNIT 3b Tree
UNIT 3b Tree
• Trees:
• Basic Tree terminologies,
• Types of Trees:
• Binary Tree, Binary Search Tree (BST), AVL Tree, B-Tree, and Heap.
The Binary tree means that the node can have maximum two
children. Here, binary name itself suggests that 'two'; therefore,
each node can have either 0, 1 or 2 children.
In the above tree, node 1 contains two pointers, i.e., left and a
right pointer pointing to the left and right node respectively. The
node 2 contains both the nodes (left and right node); therefore, it
has two pointers (left and right). The nodes 3, 5 and 6 are the
leaf nodes, so all these nodes contain NULL pointer on both left
and right parts.
Binary Tree
Properties of Binary Tree
At each level of i, the maximum number of nodes is 2^i.
The height of the tree is defined as the longest path from the root node to
the leaf node.
The tree which is shown has a height equal to 2. Therefore, the
maximum number of nodes at height 2 is equal to (1+2+4) = 7.
In general, the maximum number of nodes possible at height h is (2^0 +
2^1 + 2^2+….+2^h) = 2^(h+1) - 1.
The minimum number of nodes possible at height h is equal to (h+1).
If the number of nodes is minimum, then the height of the tree would be
maximum. Conversely, if the number of nodes is maximum, then the
height of the tree would be minimum.
Binary Tree
Properties of Binary Tree
The minimum height can be computed as:
o As we know that, for minimum height all level have to full.
o n = 2^(h+1) -1
o n+1 = 2^(h+1)
o Taking log on both the sides,
o = (h+1)
o h= -1
The maximum height can be computed as:
o As we know that,
o n = h+1
o h= n-1
Binary Tree
Types of Binary Tree
Uses of Inorder
In case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing
order. To get nodes of BST in non-increasing order, a variation of Inorder traversal where
Inorder traversal s reversed can be used.
Example: Inorder traversal for the above-given figure is 4 2 5 1 3.
Tree Traversal
Pre-order traversal
Algorithm Preorder(tree)
1. Visit the node.
2. Traverse the left subtree, i.e., call Preorder(left-subtree)
3. Traverse the right subtree, i.e., call Preorder(right-subtree)
Uses of Preorder
Preorder traversal is used to create a copy of the tree.
Preorder traversal is also used to get prefix expression on of an expression tree.
Uses of Postorder
Sequenc O(1) -
e insertion
Priority
Queue
O(1) – find
min & del
Sorted min
Sequenc
e
O(n) -
insertion
08-Jul-18 28
Binary Heap
■ It is a binary tree that stores the priority of element at the node. It has
two properties.
– Structural Property: All levels except last are full. Last level is filled from left.
– Heap Property: Priority of a node is less than or equal to its children.
■ Height of Heap:
– Let heap has n nodes and its height is h.
11
– As for Complete Binary tree of height h
contains 2 ℎ + 1 − 1 nodes.
17 13
– Hence number of nodes in heap can be:
• 2 ℎ − 1 < 𝑛≤ 2 ℎ + 1 − 1
18 21 19 17
ℎ = log 2 𝑛
43 23 26 29 31
Implementation of Heap
11
Parent
(i)
retur (𝑖−1) /
2
n 17 13
Left (i)
return
2i+1 18 21 19 17
Right (i)
return
A[PARENT(i)]
2i+2 <= A[i] 43 23 26 29 31
■ Finding Minimum
–Element
The minimum element always be at the
root of the Heap. Arra
– if it is somewhere else, then it violates 0y 1 2 3 4 5 6 7 8 9 10 11 12
17 13
18 21 19 17
43 23 26 29 31 8
08-Jul-18 31
Insertion in Heap
■ Insert 8 11
17 13
18 21 8 17
43 23 26 29 31 19
08-Jul-18 32
Insertion in Heap
■ Insert 8 11
17 8
18 21 13 17
43 23 26 29 31 19
08-Jul-18 33
Insertion in Heap
■ Insert 8 8
08-Jul-18 34
Delete-min in Heap
■ The minimum element is always be 8
on the root of heap.
■ We can delete this & move one of its 10 11
children up to fill the space. But we
might end up at any position on last
level, so resulting tree would not be 18 21 13 17
left filled that violet the structural
property of heap. 43 23 26 29 31 19
■ So we follow the following procedure.
– Exchange the root element
with last element of the heap.
– Then delete the element.
08-Jul-18 35
Delete-min in Heap
■ After deleting the element, it 19
violates the heap property.
■ So we apply heapify procedure: 10 11
– The method heapify makes binary
tree rooted at 𝑖 a heap by moving
18 21 13 17
A[]
𝑖 down the heap.
– Heap property violated at root node
while the left sub-tree and right 43 23 26 29 31
sub-tree of root node are heap.
– So apply heapify(root-
node)
08-Jul-18 36
Delete-min in Heap
19
10 11
18 21 13 17
43 23 26 29 31
08-Jul-18 37
Delete-min in Heap
10
19 11
18 21 13 17
43 23 26 29 31
08-Jul-18 38
Delete-min in Heap
■ At worst case after deleting an 10
element, we might have to traverse
height of the tree to do heapify.
18 11
■ So, it takes O(h) i.e. O( log2 𝑛) time to
delete minimum element.
19 21 13 17
43 23 26 29 31
08-Jul-18 39
Heap Tree
Build Heap
Build Heap is a process of building a Heap from a given element generally in an array format. The
resulting heap will be either max-heap or a min-heap.
The most important function in converting an array into Binary heap is Heapify function.
Build-heap(arr, n)
{
//you can start with the last element of array but as discussed
// the node floor(size/2) is the last non-leaf node
last_parent = floor(n/2);
for(i= last_parent; i>=0; i--)
max_heapify(arr, i);
}
Heapify
Heapify is used to convert a Binary tree to a heap data structure.
Heapify procedure when applied to a node arranges the node and its subtree(children) in such a
way that they follow Heap property.
Heap Tree
Heapify Algorithm
Let the current node be i, start by finding the left and
right child of the current node. left-child = heap[2*i]
and right-child = heap[2*i + 1] and assign a variable
largest with the index i, of current node.
If left-child exists and is less than the current node, take
assign the variable largest the index of left-child.
Use Heapify to convert Step 1: 9 < 27, hence swap 9 & 27, also since
Else if the right-child exists and is less than the current
above tree into a Max-Heap swapping has happend call heapify again
node, take assign the variable largest the index of right-
child.
If largest != i swap the values at the index i and the
index largest i.e. swap(heap[i], swap[largest]), and
again call the Max-Heapify procedure with largest as the
new index and repeat the step 1.
Else Stop. The tree is now following Max-Heap property.
The time complexity of performing Max/Min Heapify is Step 2: 9 < 18, hence swap 9 & 18, also
Step 1: 9 doesn't have any left since swapping has happend call heapify
O(log N) child so stop again
Complexity Analysis
O(1) – find
min
Binary
Heap O( log 2 𝑛) – del min
& insertion
O(1) -
insertion
Priority Queue
O(n) -
insertion
08-Jul-18 42
Application of Heap
■ To quickly find the smallest and largest element from a collection of items or
array.
■ In the implementation of Priority queue in graph algorithms like Dijkstra's
algorithm (shortest path), Prim's algorithm (minimum spanning tree) and Huffman
encoding (data compression).
■ In order to overcome the Worst Case Complexity of Quick Sort algorithm from
O(𝑛2) to O(𝑛log 𝑛) in Heap Sort.
■ Systems concerned with security and embedded system such as Linux Kernel
uses Heap Sort because of the O(𝑛log 𝑛).
08-Jul-18 43
AVL Tree
• 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.
• An Example Tree that is an AVL Tree
• The above tree is AVL because
differences between heights of left and
right subtrees for every node is less than
or equal to 1.
• An Example Tree that is NOT an AVL Tree
• The above tree is not AVL because
differences between heights of left and
right subtrees for 8 and 12 is greater
than 1..
AVL Tree
• Why AVL 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 an AVL tree is always O(Logn) where n is the number of nodes in the tree.
AVL Tree
• Insertion in AVL
• To make sure that the given tree remains AVL
after every insertion, we must augment the
standard BST insert operation to perform some
re-balancing. Following are two basic operations
that can be performed to re-balance a BST
without violating the BST property (keys(left) <
key(root) < keys(right)).
• Left Rotation
• Right Rotation
AVL Tree
• Steps for Insertion
Let the newly inserted node be w
1) Perform standard BST insert for w.
2) Starting from w, travel up and find the first unbalanced node. Let z be the first unbalanced node, y be the
child of z that comes on the path from w to z and w be the grandchild of z that comes on the path from w to z.
3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z. There can be 4
possible cases that needs to be handled as x, y and z can be arranged in 4 ways. Following are the possible 4
arrangements:
a) y is left child of z and x is left child of y (Left Left Case)
b) y is left child of z and x is right child of y (Left Right Case)
c) y is right child of z and x is right child of y (Right Right Case)
d) y is right child of z and x is left child of y (Right Left Case)
In all of the cases, we only need to re-balance the subtree rooted with z and the complete tree becomes
balanced as the height of subtree (After appropriate rotations) rooted with z becomes same as it was before
insertion.
AVL Tree
a) y is left child of z and x is left b) y is left child of z and x is right
child of y (Left Left Case) child of y (Left Right Case)
AVL Tree
c) y is right child of z and x is right d) y is right child of z and x is left
child of y (Right Right Case) child of y (Right Left Case)
AVL Tree
Insertion Examples:
AVL Tree
Insertion Examples:
AVL Tree
Insertion Examples:
AVL Tree
Insertion Examples:
AVL Tree
Insertion Examples:
AVL Tree
• Deletion in AVL Tree
Steps to follow for deletion.
To make sure that the given tree remains AVL after every deletion, we must augment the standard BST delete
operation to perform some re-balancing. Following are two basic operations that can be performed to re-
balance a BST without violating the BST property (keys(left) < key(root) < keys(right)).
1) Left Rotation
2) Right Rotation
AVL Tree
• Deletion in AVL Tree
Steps to follow for deletion.
Let w be the node to be deleted
1) Perform standard BST delete for w.
2) Starting from w, travel up and find the first unbalanced node. Let z be the first unbalanced node, y be the larger
height child of z, and x be the larger height child of y. Note that the definitions of x and y are different from
insertion here.
3) Re-balance the tree by performing appropriate rotations on the subtree rooted with z. There can be 4 possible
cases that needs to be handled as x, y and z can be arranged in 4 ways. Following are the possible 4 arrangements:
a) y is left child of z and x is left child of y (Left Left Case)
b) y is left child of z and x is right child of y (Left Right Case)
c) y is right child of z and x is right child of y (Right Right Case)
d) y is right child of z and x is left child of y (Right Left Case)
Like insertion, following are the operations to be performed in above mentioned 4 cases. Note that, unlike insertion,
fixing the node z won’t fix the complete AVL tree. After fixing z, we may have to fix ancestors of z as well
AVL Tree
a) y is left child of z and x is left b) y is left child of z and x is right
child of y (Left Left Case) child of y (Left Right Case)
AVL Tree
c) y is right child of z and x is right d) y is right child of z and x is left
child of y (Right Right Case) child of y (Right Left Case)
• Unlike insertion, in deletion, after we perform a rotation at z, we may have to perform a rotation
at ancestors of z. Thus, we must continue to trace the path until we reach the root.
AVL Tree
Deletion Examples:
• A node with value 32 is being deleted. After deleting 32, we travel up and find the first unbalanaced node which is 44. We
mark it as z, its higher height child as y which is 62, and y’s higher height child as x which could be either 78 or 50 as both
are of same height. We have considered 78. Now the case is Right Right, so we perform left rotation.
B – Tree
B - Tree
• Introduction
• B-Tree is a self-balancing search tree.
• In other self-balancing search trees (like AVL), it is assumed that everything is in main memory.
• To understand the use of B-Trees, we must think of the huge amount of data that cannot fit in
main memory.
• When the number of keys is high, the data is read from disk in the form of blocks.
• Disk access time is very high compared to the main memory access time.
• The main idea of using B-Trees is to reduce the number of disk accesses.
• Most of the tree operations (search, insert, delete, max, min, etc ) require O(h) disk accesses
where h is the height of the tree.
• The height of B-Trees is kept low by putting maximum possible keys in a B-Tree node.
• Generally, the B-Tree node size is kept equal to the disk block size.
• Since the height of the B-tree is low so total disk accesses for most of the operations are reduced
significantly compared to balanced Binary Search Trees like AVL Tree, Red-Black Tree, ..etc.
B - Tree
• Time Complexity
Sr. No. Algorithm Time Complexity
1. Search O(log n)
2. Insert O(log n)
3. Delete O(log n)