unit 3&5 trees
DESCRIPTION
wefqwefjhbjk whlkhjqefjkl whegh jkwhfbjkb fffffffffffff wefhkjhjk wnmbgjkhTRANSCRIPT
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 1Prof. (Dr) N Guruprasad
UNIT 3 & 5 – Trees & Search UNIT 3 & 5 – Trees & Search TreesTrees
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 2Prof. (Dr) N Guruprasad
Agenda
1) Introduction & Basic Terminologies2) Complete & Extended Binary tree3) Tree Traversal Algorithms4) BST and primitive operations,5) Threaded Binary tree and traversal6) Huffman Algorithm7) Heap sort8) AVL tree9) Introduction to m-way Search Trees, B Trees & B+ trees10) Storage Management: Garbage Collection
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 3Prof. (Dr) N Guruprasad
Introduction & definition
• One of the most important data structures in CS.• A tree consists of finite set of elements, called nodes,
and a finite set of directed lines called branches, that connect the nodes.
• Non linear data structure capable of expressing more complex relationship than that of physical adjacency.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 4Prof. (Dr) N Guruprasad
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 5Prof. (Dr) N Guruprasad
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.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 6Prof. (Dr) N Guruprasad
Basic Tree Terminologies• Degree The number of branches associated with a node
is the degree of the node.• Edge • Path • Branch • Terminal node • Level number • Height / depth
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 7Prof. (Dr) N Guruprasad
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 8Prof. (Dr) N Guruprasad
Basic Tree Terminologies• Internal node node that is not a root or a leaf is known
as an internal node.• When the branch is directed toward the node, it is indegree
branch.• When the branch is directed away from the node, it is an
outdegree branch.• The sum of the indegree and outdegree branches is the
degree of the node.• If the tree is not empty, the first node is called the root.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 9Prof. (Dr) N Guruprasad
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 10Prof. (Dr) N Guruprasad
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.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 11Prof. (Dr) N Guruprasad
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 12Prof. (Dr) N Guruprasad
A null tree is a tree with no nodes
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 13Prof. (Dr) N Guruprasad
Some Properties of Binary Trees
• The height of binary trees can be mathematically predicted
• Given that we need to store N nodes in a binary tree, the maximum height is
maxH NA tree with a maximum height is rare. It occurs when all of the nodes in the entire tree have only one successor.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 14Prof. (Dr) N Guruprasad
Some Properties of Binary Trees
• The minimum height of a binary tree is determined as follows:
min 2log 1H N
For instance, if there are three nodes to be stored in the binary tree (N=3) then Hmin=2.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 15Prof. (Dr) N Guruprasad
Some Properties of Binary Trees
• Given a height of the binary tree, H, the minimum number of nodes in the tree is given as follows:
minN H
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 16Prof. (Dr) N Guruprasad
Some Properties of Binary Trees
• The formula for the maximum number of nodes is derived from the fact that each node can have only two descendents. Given a height of the binary tree, H, the maximum number of nodes in the tree is given as follows:
max 2 1HN
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 17Prof. (Dr) N Guruprasad
Some Properties of Binary Trees
• The children of any node in a tree can be accessed by following only one branch path, the one that leads to the desired node.
• The nodes at level 1, which are children of the root, can be accessed by following only one branch; the nodes of level 2 of a tree can be accessed by following only two branches from the root, etc.
• The balance factor of a binary tree is the difference in height between its left and right subtrees:
L RB H H
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 18Prof. (Dr) N Guruprasad
B=0 B=0 B=1 B=-1
B=0 B=1
B=-2 B=2
Balance of the tree
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 19Prof. (Dr) N Guruprasad
Complete and nearly complete binary trees
• A complete tree has the maximum number of entries for its height. The maximum number is reached when the last level is full.
• A tree is considered nearly complete if it has the minimum height for its nodes and all nodes in the last level are found on the left
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 20Prof. (Dr) N Guruprasad
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 21Prof. (Dr) N Guruprasad
Representation of a Binary Tree
• There are two ways of representing tree in memory :1) Linked representation2) Sequential representation
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 22Prof. (Dr) N Guruprasad
Linked Representation :
• We use three arrays :1) INFO[K] contains data at node N2) LEFT[K] contains location of left child of N3) RIGHT [K] contains location of right child of N
ROOT will contain location of root R of T.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 23Prof. (Dr) N Guruprasad
• Consider the binary tree and represent it in linked representation format.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 24Prof. (Dr) N Guruprasad
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 node3) RIGHT node is placed at TREE[2*K+1] where K position of its parent node
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 25Prof. (Dr) N Guruprasad
• Consider the binary tree and represent it in sequential representation format.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 26Prof. (Dr) N Guruprasad
Primitive operations
• Traversing• Insertion• Deletion
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 27Prof. (Dr) N Guruprasad
Binary Tree Traversal
• A binary tree traversal requires that each node of the tree be processed once and only once in a predetermined sequence.
• Three types :• Pre order traversal algorithm• In order traversal algorithm• Post order traversal algorithm
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 28Prof. (Dr) N Guruprasad
Preorder Traversal
Step 1 : Process root TStep 2 : Traverse left subtree again in PREORDERStep 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
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 29Prof. (Dr) N Guruprasad
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;
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 30Prof. (Dr) N Guruprasad
Inorder Traversal
Step 1 : Traverse left subtree in INORDERStep 2 : Process root TStep 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
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 31Prof. (Dr) N Guruprasad
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;
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 32Prof. (Dr) N Guruprasad
Postorder Traversal
Step 1 : Traverse left subtree in POSTORDERStep 2 : Traverse right subtree again in POSTORDERStep 3 : Process root T
Also called LRN (Left, Right, Node ) Traversal
Ex : D F E B G L J K H C A
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 33Prof. (Dr) N Guruprasad
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;
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 34Prof. (Dr) N Guruprasad
Binary Search TreesThis data structure enables us to search an element. Suppose T is a binary tree ,then T is called BINARY SEARCH TREE if each node N of T has the following properties :-
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
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 35Prof. (Dr) N Guruprasad
Example : ( Fig BST – 1 )
38
14 56
8 23 45 82
18 70
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 36Prof. (Dr) N Guruprasad
Primitive operations on BST
• Traversing• Insertion• Deletion
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 37Prof. (Dr) N Guruprasad
w.r.t fig BST-1
Pre order traversal :38 14 8 23 18 56 45 82 70
In order traversal :8 14 18 23 38 45 56 70 82
Post order traversal :8 18 23 14 45 70 82 56 38
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 38Prof. (Dr) N Guruprasad
Searching & Inserting in a BST
Suppose an item of information is given, the following algorithm finds the location of ITEM in binary search tree T or inserts ITEM as a new node in its appropriate place.
a) Compare ITEM with root node N
1) If ITEM<N, proceed to left subtree. 2) If ITEM>N, proceed to right subtree.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 39Prof. (Dr) N Guruprasad
Searching & Inserting in a BST
b) Repeat step (a) until one of the following occurs, 1) We meet a node N such that ITEM=N. In this case,
search is successful and no insertion takes place.
2) We meet an empty subtree which indicates search is unsuccessful and ITEM is inserted in place of an empty subtree.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 40Prof. (Dr) N Guruprasad
Example :
Consider fig BST – 1
Suppose ITEM = 20 is to be inserted.
Answer : We insert as right successor of 18
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 41Prof. (Dr) N Guruprasad
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
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 42Prof. (Dr) N Guruprasad
Example : ( Fig BST – 2 ) 60
2575
15 5066
33
44
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 43Prof. (Dr) N Guruprasad
INFO RIGHT LEFT
1 33 0 9
Root 2 25 8 10
3 3 60 2 7
4 66 0 0
5 6
Avail 6 0
5 7 75 4 0
8 15 0 0
9 44 0 0
10 50 1 0
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 44Prof. (Dr) N Guruprasad
1) Nodes with no successor (leaf node )
Ex : 15, 44 & 66Delete : 44Draw the tree and linked representation.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 45Prof. (Dr) N Guruprasad
60
2575
15 5066
33
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 46Prof. (Dr) N Guruprasad
INFO RIGHT LEFT
1 33 0 9
Root 2 25 8 10
3 3 60 2 7
4 66 0 0
5 6
Avail 6 0
9 7 75 4 0
8 15 0 0
9 5
10 50 1 0
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 47Prof. (Dr) N Guruprasad
2) Nodes with one successor
Ex : 50, 33 & 75Delete : 75Draw the tree and linked representation.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 48Prof. (Dr) N Guruprasad
60
2566
15 50
33
44
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 49Prof. (Dr) N Guruprasad
INFO RIGHT LEFT
1 33 0 9
Root 2 25 8 10
3 3 60 2 4
4 66 0 0
5 6
Avail 6 0
7 7 5 0
8 15 0 0
9 44 0 0
10 50 1 0
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 50Prof. (Dr) N Guruprasad
3) Nodes with two successor
Ex : 25 & 60Delete : 25Draw the tree and linked representation.
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 51Prof. (Dr) N Guruprasad
60
3375
15 5066
44
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 52Prof. (Dr) N Guruprasad
INFO RIGHT LEFT
1 33 8 10
Root 2 5
3 3 60 1 7
4 66 0 0
5 6
Avail 6 0
2 7 75 4 0
8 15 0 0
9 44 0 0
10 50 9 0
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 53Prof. (Dr) N Guruprasad
Problem Suppose the following eight numbers are inserted in order into an empty BST T.
50 33 44 22 77 35 60
Draw the tree T
Session: Jan – June 2013
EEC 012 - DATA STRUCTURES – TREES 54Prof. (Dr) N Guruprasad
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.