UNIT 4.1 (Tree, BST, HeapTree, Heapsort)
UNIT 4.1 (Tree, BST, HeapTree, Heapsort)
Topics to be covered
Trees: Definitions and Concepts
properties of Binary Trees
types of binary trees
Representation of binary tree
Tree Traversal.
Binary Search Trees: Representation and operations.
Tries- Definition and uses
Heap Tree: Definition
Heap Tree Representation
Heap Sort.
Balanced Search Trees: AVL Trees
leaves
nodes
branches
root
root
leaves
branches
nodes
T1 A T3
T2
B C D
E F G H I J
K L
S. Durga Devi , CSE, CBIT
A tree satisfies the following properties:
1. It has one designated node, called the root, that has no parent.
2. Every node, except the root, has exactly one parent.
3. A node may have zero or more children.
4. There is a unique directed path from the root to each node.
5 5
5
1
3 2 3 2
3 2
4 1 6 4 6
4 1 6
tree Not a tree Not a tree
D E F G
left Right
sub sub H I J
tree tree
K S. Durga Devi , CSE, CBIT
• Difference between tree and binary tree
Trees
1.Tree never be empty Binary tree
2. A node may have any no of 1. May be empty
children nodes. 2. A node may have
at most two children
0, 1, or 2
Three special situations of a binary tree are possible
1.Full binary tree
2.Complete binary tree.
3. Strict binary tree
1 Level 0- 1node
2 3 Level 1-2nodes
4 5 6 7
Level 2-4 nodes
8 9 10 11 12 13 14 15 Level 3-8nodes
Complete binary tree
A binary tree is said to be complete binary tree, if all its levels have maximum
number of nodes except possibly the last level, and In the last level, the nodes
are attached starting from the left-most position. All leaf nodes at height h or h-
1.
1 A
Level 0-1node
2 3 Level 1-2 nodes
B C Not a CBT
4 5 6 7 Level 2- 4 nodes D E F G
8 9 Level 3- 2 nodes H I J
K Devi
S. Durga Not, CSE,
a CBTCBIT
3. strict binary tree:
every node in binary tree has exactly two
children except the leaf nodes.
B C
D E F G
H I
The nodes are stored level by level, starting from the zero level
where root node is present.
the following rules can be used to decide the location of any node of a
tree in the array.
a. The root node is at location 0.
b. If a node is at a location ‘i’, then its left child is located at 2 * i + 1
and right child is located at 2 * i + 2
c. The space required by an n node binary tree is 2n+1.
2 3
B D
4 6 7
C E G
13
F
A B D C . E G . . . . . F
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Sequential representation
disadvantages
1. Other than full binary tree, if u store any type of tree most of the memory
locations are empty.
2. Allows only static representation. There is no possible way to enhance
the size of the tree.
3. Inserting a new node and deleting a node from it are inefficient with
this representation because these require considerable data movement up
and down the array which demand excessive processing time.
- 600 * 700
200 500
A B C D
600 700
300 400
root->rchild=createNode(40);
root->lchild->lchild=createNode(50);
root->rchild->lchild=creatNode(60); 30 40
return 0;
} 50 60
30 40
50 60
• Steps
.
1. Root node of the general tree will become
root node to Binary tree
2. Left most child in general tree will become left
child in binary tree
3. Left node siblings in general tree will become
right child nodes for the left node in binary tree
General Tree D
E
F G
6. for any non empty binary tree T, if n0 is the no. of leaf nodes and n2 is
the no. of internal nodes (degree-2), then
n0= n2+1
No of internal nodes n2 =3
No of leaf nodes n0 = 3+1=4
7. Minimum height of a binary tree with n number of nodes is
log2(n+1)
8. Total no of binary trees possible with n nodes is
(1/(n+1) )*2nCn
S. Durga Devi ,CSE,CBIT
• Total no of binary tree possible with 3 nodes is- 5- A,B,C
A A A A
A
B B B B
B C
C C C
1 C
2 3 5
4
Representation of binary tree
1. linear\sequential representation (using arrays).
2. linked representation (using linked list)
• This operation is used to visit each node in the tree exactly once.
• The tree can be traversed in various ways.
for a systematic traversal, it is better to visit each node and its sub trees in the
same fashion.
• Following are tree traversals
1. root, left, right- pre order
2. Left, root, right- In order
3. Left, right, root- post order
4. Level by level- level order
B C
D E F G
H I J
K S. Durga Devi ,CSE,CBIT
In order of binary tree
A Inorder- D,B,H,E,I,A,F,K,J,C,G
B C
D E F G
J
H I
K
S. Durga Devi ,CSE,CBIT
In order of binary tree
A Inorder- D,B,H,E,I,A,F,K,J,C,G
B C
D E F G
H J
I
K
S. Durga Devi ,CSE,CBIT
Post order of binary tree
B C
D E F G
J
H I
K
S. Durga Devi ,CSE,CBIT
Level order of binary tree
B C
D E F G
J
H I
K
S. Durga Devi ,CSE,CBIT
Write pre,in and post order of the following tree
• (A-B) + C* (D/E)
- *
A B C /
D E
+
Pre order- +,-,A,B,*,C,/,D,E.
- *
A B C /
D E
+
Pre order- +,-,A,B,*,C,/,D,E.
- *
A B C /
In order- A,-,B,+,C,*,D,/,E D E
post order of the following tree
+
Pre order- +,-,A,B,*,C,/,D,E.
- *
A B C /
In order- A,-,B,+,C,*,D,/,E D E
All<K All>K
35
18
45
15 20
12 17 25 40 50
20
15 25 Its not a binary search
tree because it fails to satisfy
12 the property of 3 and 4
18 22
22>20
20
Search node-22
15
25
12
18 22
1 4 1 4
3
3 5
a) Before insertion b) After inserting node 5
Root
19
8 55
6 15
44 98
10
23
82 99
• Another frequently used operation on the BST is to delete any node from it.
This is slightly complicated one.
• To delete a node from a binary search tree, there are three possible cases
when we delete a node.
case-1- if deleted node is a leaf node or node has no children, it can be deleted
by making its parent pointers ( left, right) to NULL.
30
30
25 40
25 40
20 20 45
35 45
1 7
4 7
4
45 24 45
20
16 42 16 29 42
29
24 27 33
33
x
27
S. Durga Devi
,CSE,CBIT
Merge or join BST
• Another interesting operations on BST are
recombine them (merge) and split BST in
interesting ways.
• Merge: Combine two Binary search trees into
single binary search tree.
• Split: break binary search tree into two binary
search trees.
There is no need to have prior knowledge of depth of the tree. Using dynamic
memory allocation concept one can create as much memory (node) as
required.
Insertion and deletion which are the most common operations can be done
without moving the other nodes.
It needs additional space in each node for storing the left and right subtrees.
• Applications
- Lower berths of a train during reservation are allocated first to the old age
persons. Here priority is age of a person.
- In operating system runnable processes might be stored in priority queue,
where certain system processes are given a higher priority than user
processes.
- In networks packets of data routed according to some assigned priorities.
Min Heap
Insert 10
10
6
6<10 swap with parent 10
10
10 20
S. Durga Devi
,CSE,CBIT
Insert 5
6
10 20
5 20
6 20
6 20
6 20
6 20
6 17
6 17
6 13
6 13
6 13
10
2 13
10
5 13
This process of moving the empty node towards the child is called percolate
down
2 20
4 5 4 5
7 6 10 8 7 6 10 8
11 9 12 14 20 11 9 12 14 20
4 5 20 5
7 6 10 8 7 6 10 8
11 9 12 14 11 9 12 14
4 4
6 5 6 5
7 20 10 8 7 12 10 8
11 9 12 14 11 9 20 14
S. Durga Devi ,CSE,CBIT
Heap sort
• There are two main applications of heap tree
1. sorting data ( heap sort)
2. implement priority queue.
Heap sort:
to sort the data in ascending(Descending)
order, build max heap (min heap) .
23 17
19 22 14 15
18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
25 23 17 19 22 14 15 18 14 21 3 9 11
• Notice:
– The left child of index i is at index 2*i+1
– The right child of index i is at index 2*i+2
– Example: the children of node 3 (19) are 7 (18) and 8 (14)
S. Durga Devi ,CSE,CBIT
25
23 17
19 22 14 15
18 14 21 3 9 11
23 17
19 22 14 15
18 14 21 3 9 25
11 17
19 22 14 15
18 14 21 3 9 25
22 17
19 11 14 15
18 14 21 3 9 25
22 17
19 21 14 15
18 14 11 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
23 22 17 19 21 14 15 18 14 11 3 9 25
22 17
19 21 14 15
18 14 11 3 9
22 17
19 21 14 15
18 14 11 3 23
9 17
19 21 14 15
18 14 11 3 23
21 17
19 9 14 15
18 14 11 3 23
21 17
19 11 14 15
18 14 9 3