Documente Academic
Documente Profesional
Documente Cultură
Preeti S. Patil
Introduction to Trees
Flexible, versatile, and powerful data structures. Used to represent hierarchical relationship among them. Example: Geneology
The relationship between the grandfather and his descendants and in turn their descendants and so on
Preeti S. Patil Trees MPSTME, SHIRPUR 2
Tree definition
Non-linear data structure in which items are arranged in a sorted sequence. Represents hierarchical relationship existing amongst several data items.
Preeti S. Patil
Preeti S. Patil
A Tree
Root A B E F C G H D I Level 0 Level 1 Level 2
Level 3
Tree Terminology
Term associated with trees
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. Root Node Degree of a node Degree of a tree Terminal node(s) Non-terminal node(s) Siblings Level Edge Path Depth Forest
Trees MPSTME, SHIRPUR 6
Preeti S. Patil
Root
1. Specially designated data item. 2. First in the hierarchical arrangements of data items.
Node
1. Each data item in a tree is called tree. 2. It specifies the data information and links (branches) to other data items.
Preeti S. Patil Trees MPSTME, SHIRPUR 7
Degree of a node
It is the number of sub trees of a node in a given tree.
a. b. c. d. e. The degree of node A is 3 The degree of node C is 1 The degree of node D is 2 The degree of node H is 3 The degree of node I is 0
Degree of tree
It is maximum degree of the nodes in a given tree.
In the tree shown , the node A has degree 3 and another node H is also having its degree 3. in all the value is the maximum. So, the degree of the tree is 3
Preeti S. Patil Trees MPSTME, SHIRPUR 8
Terminal Node(s)
A node with degree zero. Also called leaf node. Ex. Nodes E,G,I,J,K,L and M are Terminal nodes
Non-Terminal Node(s)
Any node (except the root node) whose degree is not zero. Intermediate nodes in traversing the given tree from its root to the terminal nodes. Ex nodes B, C, D, F & H are Non terminal nodes
Preeti S. Patil Trees MPSTME, SHIRPUR 9
Siblings
The children nodes of a given node. Also called brothers.
E and F are siblings of parent node B. K, L and M are siblings of parent node H.
Level
Entire tree structure is leveled as such that the root is always at level 0. Its intermediate at level 1. and their intermediate at level 2 and so on upto the terminal nodes. Tree shown has level 4.
Preeti S. Patil Trees MPSTME, SHIRPUR 10
Edge
Connecting line of two nodes. line drawn from one node to another node.
Path
Sequence of consecutive edges from source node to the destination node. Eg: Path between A and J is given by (A,B), (B,F), (F,J)
Preeti S. Patil Trees MPSTME, SHIRPUR 11
Depth/height
Maximum level of any node in a given tree. Number of levels one can descend the tree from its root to the terminal nodes In tree shown Root node has the maximum level.
Forest
Set of disjoint trees. If root is removed from tree it becomes forest. In tree shown there is forest with three trees
Preeti S. Patil Trees MPSTME, SHIRPUR 12
Binary Trees
Finite set of data items which is either empty or consists of a single item called root and two disjoint binary trees called left sub tree and right sub tree Maximum degree of any node is at most two.
Preeti S. Patil
13
A binary tree
Root A B D E F C G
Preeti S. Patil
14
F
Preeti S. Patil
G
Trees MPSTME, SHIRPUR 15
Preeti S. Patil
16
2.
3.
4.
Preeti S. Patil
Data field hold the value to be given. The left child is a link field which contains the address of its left node. The right child is a link field which contains the address of its right node. The Structure of a node
Lchild Data Rchild
Preeti S. Patil
19
Logical Representation
struct node { char data; struct node lchild; struct node rchild; }; typedef struct node BTNODE;
Preeti S. Patil Trees MPSTME, SHIRPUR 20
B D
Preeti S. Patil
21
Preeti S. Patil
22
Array Representation
The nodes stored in an array are accessible sequentially. Maximum nu ber of nodes is specified by MaxSize. Root node is always at index 0. Successive memory locations the left child and right child are stored.
Preeti S. Patil Trees MPSTME, SHIRPUR 23
A 0 B 1 C 2
Preeti S. Patil
24
BT A B C D E F G
Preeti S. Patil
25
Father(n):
The Father of node having index n is at floor((n-1)/2) If n=0, then it is the root node and has no father. Eg: father of node numbered 3(i.e, D) =floor ((3-1) / 2) =floor (2/2) =1(i.e B) The left child of node numbered n is at (2n+1). Eg: lchild(A)=lchild(0)
=2*0+1 =1(i.e B).
lchild(n):
Preeti S. Patil
26
rchild(n):
The right child of node numbered n is at index(2n+2) Eg: rchild(A)=rchild(0) =2*0+2 =2(i.e C).
Siblings:
If left child at index n is given then right sibling is at (n+1). If right child at index n is given then left sibling is at (n-1). Right node of node 4 is at index 5.
Preeti S. Patil Trees MPSTME, SHIRPUR 27
Since left sub tree is present, this type of tree is called left skewed binary tree. we can even have right skewed binary tree. This result in wastage of memory in case of array representation.
Preeti S. Patil Trees MPSTME, SHIRPUR 28
Preeti S. Patil
29
Description
It creates an empty binary tree. It creates a new Binary Tree having single node with data field set to some value. It returns true if the binary tree is empty. Otherwise it returns false. It returns a pointer to the left child of the node. if the node has no left child it returns the null pointer It returns a pointer to the right child of the node. if the node has no right child it returns the null pointer It returns the pointer to father of the node.otherwise returns null pointer It returns the pointer to brother of the node.otherwise returns null pointer It returns the content of the node.
Trees MPSTME, SHIRPUR 30
Operations
Other than primitive operation other operations are
1. 2. 3. 4. 5. Tree traversal Insertion of nodes Deletion of nodes Searching for the node Copying the binary tree.
Preeti S. Patil
31
Preeti S. Patil
32
Preorder Traversal
The preorder of a non-empty binary tree is defined as
1. Visit the root node. 2. Traverse the left sub tree in preorder. 3. Traverse the right sub tree in preorder.
Preeti S. Patil
33
Root A B D E F C G
left
Right
Trees MPSTME, SHIRPUR 34
Preeti S. Patil
Algorithm
preorder(BTNODE *tree) { if(tree != NULL) { printf(%c,tree->data); /* step 1*/ preorder(tree->lchild); /*step 2*/ preorder(tree->rchild); /*step 3*/ } return; }
Preeti S. Patil Trees MPSTME, SHIRPUR 35
Inorder Traversal
The Inorder of a non-empty binary tree is defined as
1. Traverse the left sub tree in inorder. 2. Visit the root node. 3. Traverse the right sub tree in inorder.
Preeti S. Patil
36
A B D E F C G
Algorithm
inorder(BTNODE *tree) { if(tree != NULL) { inorder(tree->lchild); /*step 1*/ printf(%c,tree->data); /* step 2*/ inorder(tree->rchild); /*step 3*/ } return; }
Preeti S. Patil Trees MPSTME, SHIRPUR 38
Postorder Traversal
The postorder of a non-empty binary tree is defined as
1. Traverse the left sub tree in postorder. 2. Traverse the right sub tree in postorder. 3. Visit the root node.
Preeti S. Patil
39
A B D E F C G
Right
Algorithm
postorder(BTNODE *tree) { if(tree != NULL) { postorder(tree->lchild); /*step 1*/ postorder(tree->rchild); /*step 2*/ printf(%c,tree->data); /* step 3*/ } return; }
Preeti S. Patil Trees MPSTME, SHIRPUR 41
Preeti S. Patil
42
25 12 9 21 43
42
48
SEARCHING
Start with the root. Eg; search 21 in the given tree
Compare 21<25, true. Take left branch. At left child second comparison is made. Here 21>12. so take right branch. On third comparison, since 21=21, the search is announced successful.
Preeti S. Patil
44
Advantages
Advantage over an array is that all the insertion and deletions and searching are performed efficiently.
Preeti S. Patil
45
Preeti S. Patil
46
Insertion in a BST
To insert an element 23 in an tree shown below Search for element 23 in that tree. Search causes to reach at the node 21 with no where to go. Since 23>21, we simply insert node 23 as right child to node 21.
25 12 43 12 25 43
21
42
48
21
42
48
23>21
23
Preeti S. Patil Trees MPSTME, SHIRPUR 47
Search(int key_val, int &found, BTNODE *(&parent)) { BTNODE *p; found=0; parent=NULL; if(tree==NULL) { return; } p=tree; while(p!=null) { if(p->data==key_val) {found=1; return; } if(p->data > key_val) {parent=p; p=p->lchild; } else if(p->data < key_val) {parent=p; p=p->rchild; } }} Preeti S. Patil
48
Deletion in a BST
Three cases: 1. Node to be deleted has no children. 2. Node to be deleted has exactly one subtree. 3. Node to be deleted has two subtrees.
Preeti S. Patil
50
In first situation ,
As there is no children for the node, so that node is deleted with out any further adjustments to the tree.
Preeti S. Patil
51
In second case,
The node to be deleted has exactly one child, so this child is moved up the tree to occupy its place.
25 12 43 12 25 43
21
42
48
23
42
48
Node to be deleted
23
Third case, The node to be deleted has two sub trees. The in order successor of the node should be moved up to occupy its place.
Node to be deleted 12 25 43 12 42 43
21
42
48
21
48
23
23
End of Trees
Preeti S. Patil
59