Sunteți pe pagina 1din 59

TREES

Preeti S. Patil

Trees MPSTME, SHIRPUR

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

Trees MPSTME, SHIRPUR

Graph theoretic definition


It is finite set of one or more data items (nodes) such that
1. There is special data item called root of the tree 2. Remaining data items are partitioned into number of mutually exclusive (i.e, disjoint) subsets. Each of which is itself is tree called to be subtrees

Preeti S. Patil

Trees MPSTME, SHIRPUR

A Tree
Root A B E F C G H D I Level 0 Level 1 Level 2

Level 3

Trees in data structures grows from top to bottom.


Preeti S. Patil Trees MPSTME, SHIRPUR 5

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

Trees MPSTME, SHIRPUR

13

A binary tree
Root A B D E F C G

Preeti S. Patil

Trees MPSTME, SHIRPUR

14

Strictly Binary Tree


If every non terminal tree in a binary tree consist of non empty left sub tree an right sub tree.
Root A B D E C

F
Preeti S. Patil

G
Trees MPSTME, SHIRPUR 15

Complete Binary Tree


There is exactly one node at level 0,two nodes at level 1, four nodes at level 2 and so on. If there are m nodes at level L then a binary tree contains 2m nodes at level L+1.

Preeti S. Patil

Trees MPSTME, SHIRPUR

16

Properties of binary trees


1. The maximum number of nodes at level l of a binary tree is 2l, where l>=0 The maximum number of nodes in a binary tree of depth d is 2d-1 where d>=1. The total number of edges in a complete binary tree with n terminal nodes is 2(n-1). A binary tree with n nodes has exactly N+1 null branches.
Trees MPSTME, SHIRPUR 17

2.

3.

4.

Preeti S. Patil

Binary Tree Representation


Arrays representation Linked list representation

1. Basic component of a tree is a node 2. Node consist of three fields.



Preeti S. Patil

Data Left Child Right Child


Trees MPSTME, SHIRPUR 18

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

Trees MPSTME, SHIRPUR

19

Logical Representation
struct node { char data; struct node lchild; struct node rchild; }; typedef struct node BTNODE;
Preeti S. Patil Trees MPSTME, SHIRPUR 20

Consider the following Binary tree.


Root A C E

B D

Preeti S. Patil

Trees MPSTME, SHIRPUR

21

Linked List representation


Root A

Preeti S. Patil

Trees MPSTME, SHIRPUR

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

The array representation of the above tree


BT [0] BT [1] BT [2] BT A B C

Preeti S. Patil

Trees MPSTME, SHIRPUR

24

BT [0] A 0 1 B 3 D 5 E 4 F C 2 6 G BT [1] BT [2] BT [3] BT [4] BT [5] BT [6]

BT A B C D E F G

Preeti S. Patil

Trees MPSTME, SHIRPUR

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

Trees MPSTME, SHIRPUR

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

Skewed binary tree


0 0 1 B 3 C A 1 2 3 4 5 6 BT A B C -

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

Disadvantages of array representation


Not suitable for normal binary trees. Ideal for complete binary trees More memory is unnecessarily wasted. Insertion and deletion at the middle of the tree is cumbersome.

Preeti S. Patil

Trees MPSTME, SHIRPUR

29

Operations on binary trees


Operation
1. Create 2. Make BT 3. Empty BT 4. lchild 5. rchild 6. father 7. Brother 8. Data
Preeti S. Patil

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

Trees MPSTME, SHIRPUR

31

Traversal of a Binary Tree


It is a way in which each node in the tree is visited exactly once in systematic manner. Popular ways of Binary tree traversal
1. Preorder Traversal 2. Postorder Traversal 3. Inorder Traversal

Preeti S. Patil

Trees MPSTME, SHIRPUR

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

Trees MPSTME, SHIRPUR

33

Root A B D E F C G

Preorder traversal of the above binary tree


ABDEICFGJ
Root

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

Trees MPSTME, SHIRPUR

36

A B D E F C G

Inorder traversal of the above binary tree


DBIEAFCGJ
left Root Right
Preeti S. Patil Trees MPSTME, SHIRPUR 37

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

Trees MPSTME, SHIRPUR

39

A B D E F C G

Post order traversal of the above binary tree


DIEBFJGCA
left
Preeti S. Patil

Right

Root Trees MPSTME, SHIRPUR 40

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

Binary Search Tree (BST)


Tree in which each node has left and right child. If traversed in inorder, we get a file is ascending order.

Preeti S. Patil

Trees MPSTME, SHIRPUR

42

25 12 9 21 43

42

48

Traverse the tree in inorder, we get 9 12 21 25 42 43 48


Preeti S. Patil Trees MPSTME, SHIRPUR 43

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

Trees MPSTME, SHIRPUR

44

Advantages
Advantage over an array is that all the insertion and deletions and searching are performed efficiently.

Preeti S. Patil

Trees MPSTME, SHIRPUR

45

Height Balanced Tree


We attempt to place each terminal node at equal distance from the root node.
A B D E F C G

Preeti S. Patil

Trees MPSTME, SHIRPUR

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

Algorithm for Searching

Trees MPSTME, SHIRPUR

48

Algorithm for insertion


insert BST(int num) { int found; BTNODE *temp,*parent; search_node(num,found,parent); if(found==1) printf(Node already exists); else { temp=(BTNODE*)malloc(sizeof(BTNODE)); temp->data=num; temp->lchild=temp->rchild=NULL; if(parent==NULL) tree=temp; else { if(parent->data > num) parent->lchild=temp; else parent-> rchild=temp; }}}
Preeti S. Patil Trees MPSTME, SHIRPUR 49

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

Trees MPSTME, SHIRPUR

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

Trees MPSTME, SHIRPUR

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

After Deletion of the node 21

Before Deletion of the node 21


Preeti S. Patil Trees MPSTME, SHIRPUR 52

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

Before Deletion of the Root node 25


Preeti S. Patil

After Deletion of the Root node 25


Trees MPSTME, SHIRPUR 53

C code for deletion


delete_BST(int number, BTNODE *tree) { int found; BTNODE *temp, *parent, *temp_succ; if(tree==NULL) { printf( Tree is empty); return; } parent=temp=Null; search_BST(number,parent,temp,tree,found) if(found==0) { printf( node to be deleted is not found); return; }
Preeti S. Patil Trees MPSTME, SHIRPUR 54

/* first case : The node to be deleted has no children*/


If((temp->lchild == NULL) && (temp->rchild ==NULL)) { if(parent-.rchild == temp) { parent->rchild = NULL; } else { parent->lchild= NULL; } free(temp); return; }
Preeti S. Patil Trees MPSTME, SHIRPUR 55

/* second case: if node to be deleted has only left child */


If(temp->lchild != NULL)&&(temp->rchild == NULL) { if(parent->lchild ==temp) { parent->lchild =temp->lchild; } else { parent->rchild = temp->lchild; } free(temp); return; }
Preeti S. Patil Trees MPSTME, SHIRPUR 56

/* Third case: if node to be deleted has two children */


If((temp->lchild != NULL) && (temp->rchild != NULL)) { parent = temp; temp_succ = temp->rchild; while(temp_succ != NULL) { parent=temp_succ; temp_succ=temp_succ->lchild; } temp->data = temp_succ->data; temp=temp_succ; } }
Preeti S. Patil Trees MPSTME, SHIRPUR 57

Efficiency of Binary Search Tree Operations


The timing analysis of searching a binary search tree, for randomly inserted records is O(log n). Worst case behavior of a binary search tree would occur when records are inserted in sorted order. In this case the search time is O(n), as the resulting tree consists all NULL left children.
Preeti S. Patil Trees MPSTME, SHIRPUR 58

End of Trees

Preeti S. Patil

Trees MPSTME, SHIRPUR

59

S-ar putea să vă placă și