Académique Documents
Professionnel Documents
Culture Documents
Agenda
1) Introduction & Basic Terminologies 2) Complete & Extended Binary tree 3) Tree Traversal Algorithms 4) BST and primitive operations, 5) Threaded Binary tree and traversal 6) Huffman Algorithm 7) Heap sort 8) AVL tree 9) Introduction to m-way Search Trees, B Trees & B+ trees 10) Storage Management: Garbage Collection
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 2
Applications
1) Manipulate hierarchical data. 2) Make information easy to search (traversal). 3) Manipulate sorted lists of data. 4) As a workflow for composing digital images for visual effects. 5) Router algorithms.
Binary Trees
A binary tree is a tree in which no node can have more than two subtrees; the maximum outdegree for a node is two. In other words, a node can have zero, one, or two subtrees. These subtrees are designated as the left subtree and the right subtree.
10
11
12
H max N
A tree with a maximum height is rare. It occurs when all of the nodes in the entire tree have only one successor.
13
Some Properties of Binary Trees The minimum height of a binary tree is determined as follows:
Hmin log2 N 1
For instance, if there are three nodes to be stored in the binary tree (N=3) then Hmin=2.
14
N min H
15
N max 2 1
H
16
B HL HR
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 17
B=0
B=0
B=1
B=-1
B=0
B=1
B=-2
B=2
18
19
20
Representation of a Binary Tree There are two ways of representing tree in memory : 1) Linked representation 2) Sequential representation
21
Linked Representation : We use three arrays : 1) INFO[K] contains data at node N 2) LEFT[K] contains location of left child of N 3) RIGHT [K] contains location of right child of N ROOT will contain location of root R of T.
22
23
Sequential Representation : Uses single linear array called TREE : 1) ROOT node R is stored in TREE[1] 2) LEFT node is placed at TREE[2*K] where K position of its parent node 3) RIGHT node is placed at TREE[2*K+1] where K position of its parent node
24
25
Primitive operations
Traversing Insertion Deletion
26
27
Preorder Traversal
Step 1 : Process root T Step 2 : Traverse left subtree again in PREORDER Step 3 : Traverse right subtree again in PREORDER Also called NLR ( Node, Left, Right ) Traversal Ex : A B D E F C G H J L K
28
Function representation :
pre_trav(struct btree *tree) { if(root==NULL) printf(\n Tree is empty ); getch(); } if(tree!=NULL) { printf(\n %d ,treeinfo); pre_trav(treeleft); pre_trav(treeright); } else return;
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 29
Inorder Traversal
Step 1 : Traverse left subtree in INORDER Step 2 : Process root T Step 3 : Traverse right subtree again in INORDER Also called LNR (Left, Node, Right ) Traversal Ex : D B F E A G C L J H K
30
Function representation :
in_trav(struct btree *tree) { if(root==NULL) printf(\n Tree is empty ); getch(); } if(tree!=NULL) { in_trav(treeleft); printf(\n %d ,treeinfo); in_trav(treeright); } else return;
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 31
Postorder Traversal
Step 1 : Traverse left subtree in POSTORDER Step 2 : Traverse right subtree again in POSTORDER Step 3 : Process root T Also called LRN (Left, Right, Node ) Traversal Ex : D F E B G L J K H C A
32
Function representation :
post_trav(struct btree *tree) { if(root==NULL) printf(\n Tree is empty ); getch(); } if(tree!=NULL) { post_trav(treeleft); post_trav(treeright); printf(\n %d ,treeinfo); } else return;
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 33
the value at any node N is greater than every value in left subtree and less than every value in right subtree.
Also called BINARY SORTED TREE
34
14
56
23
45
82
18
70
35
36
37
38
39
40
Deleting in a BST There are three different types of deletion in a BST. 1) Nodes with no successor (leaf node ) 2) Nodes with one successor 3) Nodes with two successors Let us consider the tree T ( BST-2) and its linked representation
41
25
75
15
50
66
33
44
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 42
INFO 1 33
RIGHT 0
LEFT 9
Root
3
2
3 4 5
25
60 66
8
2 0 6 0
10
7 0
Avail
7
8 9 10
75
15 44 50
4
0 0 1
0
0 0 0
43
1) Nodes with no successor (leaf node ) Ex : 15, 44 & 66 Delete : 44 Draw the tree and linked representation.
44
60
25
75
15
50
66
33
45
INFO 1 33
RIGHT 0
LEFT 9
Root
3
2
3 4 5
25
60 66
8
2 0 6 0
10
7 0
Avail
7
8 9 10
75
15 50
4
0 5 1
0
0 0
46
2) Nodes with one successor Ex : 50, 33 & 75 Delete : 75 Draw the tree and linked representation.
47
60
25
66
15
50
33
44
Prof. (Dr) N Guruprasad EEC 012 - DATA STRUCTURES TREES 48
INFO 1 33
RIGHT 0
LEFT 9
Root
3
2
3 4 5
25
60 66
8
2 0 6 0
10
4 0
Avail
7
8 9 10 15 44 50
5
0 0 1
0
0 0 0
49
3) Nodes with two successor Ex : 25 & 60 Delete : 25 Draw the tree and linked representation.
50
60
33
75
15
50
66
44
51
INFO 1 33 60 66
RIGHT 8
LEFT 10 7 0
Root
3
2
3 4 5
5
1 0 6 0
Avail
7
8 9 10
75
15 44 50
4
0 0 9
0
0 0 0
52
Problem Suppose the following eight numbers are inserted in order into an empty BST T. 50 33 44 22 77 Draw the tree T 35 60
53
Problem Suppose the following list of letters is inserted in order into an empty BST T. J R D G T E M H P A F Q Draw the tree T and also find INORDER traversal.
54