Tree

Tree

The data structures that we have learn such as array, linked lists stacks, Queues were linear data structures. As against this trees are non ± linear data structures. Tree is a very flexible and powerful data structure that can be used for a wide variety of application. Tree is mainly used to represent data containing a hierarchical relationship between elements. Tree is known as Hierarchical data structure. Tree is a data structure which allows us to associate a parent child relationship between various data elements. Tree is a dynamic data structure. Elements of a Tree in known as nodes. each node has a info and link to another node.

Root Node Root of Left sub Tree Edge
D E B

A C

Root of Right sub Tree
F

H

I

Leaf Node or Leaves Or Terminal Node

Root Node: A single node in a tree which has no parent node ex:A Degree: The Number of Sub Trees of a node is called its Degree ex: Degree of root node A is 2/degree of node c is one and node f is 2 Leaf /Leaves/Terminal Node: A node with degree of Zero or A node which have no child node ex: Node D ,E,H,I Non-Terminal Node: A Node which have one or more child node ex: B,C,F Siblings: Children of same parent are called siblings. Ex-B,C Ancestor: Ancestor of a node are all the nodes along the path form the root to that node Ex: Ancestor of Node D is B and A Descendant: Descendant of a node are all the nodes along the path form node to terminal node. Ex: descendant of Node A is B and D. Level: Each node in a tree is assigned a level Number. The root node is at level no. 0 and any other node assign a level no one more than its father Ex: A root Node Level ->0 B/C-Level ->1 or F->2 Depth or Height: one more than its largest level No. Ex: Depth 4
Terms

Forest Tree: if we remove the root of a tree we get a forest .

this is indicated by the two branches emanating from A to B on the left and to C on the right. In Binary Tree there is no node with degree greater than two. The absence of a branch indicates an empty sub tree. The other two sub sets are themselves binary trees. A B D G E C F H I .Binary Tree A Tree is Binary tree if each node of it can have at the most two branches. its left sub tree is rooted at B and its right sub tree is rooted at C. Binary Tree A binary Tree is a finite set of elements that is either empty or is partitioned into three disjoint sub sets. The first sub set contains a single element called the root of the tree. The tree shown in figure consists of nine nodes with A as its root. called the left and right sub trees of the original tree A left or right sub tree can be empty. Each element of a binary tree is called a node of the tree.

Strictly Binary Tree: If every non-leaf node in a binary tree has non ± empty left and right sub trees the tree is termed a strictly binary tree that is every node have two child. Where as in previous fig one node C and E have only one child. Strictly BTree A B D E C F G .

Complete Binary Tree: A complete binary tree of any depth is a strictly Binary tree that is each node of tree have at most two children and all of whose leaves are at the same level. Complete BTree A B D G E F C g . complete Tree is also known as full Tree.

The height of node 3 4. The degree of node 3 8 9 10 12 Hight | 13 14 15 . The Depth of node 13 5. The subtree with root 5 3. The Siblings of node 6 2.1 2 3 4 5 6 11 7 Q 1 Define the following: 1.

List the terminal nodes non terminal nodes and the level of each node Sol:E & D is Terminal Nodes A.B.C are Non terminal Nodes A->0 B->1 C->2 D->2 E->3 B C D E .A Q2.

Draw a binary tree using given Expression: E=V+U/X * Y .Z Sol: ( (V+ ( ( U/X ) * Y ) ) ±Z ) z V + * / U X y .

One array arr stores the data fields of the trees. .Instead of one pointer it contains two pointer one for the left child and another for the right child Struct tnode { Struct tnode *left. } Array Representation of Binary trees:When a binary tree is represented by array three separate arrays are required .The other two arrays lc and rc represents the left child and right child of the nodes. Int data. Struct tnode *right.BINARY TREE The Structure of each node of a binary tree contains the data field and since a binary tree is recursive in nature it contains the pointer to the same structure.

A B D H E F C G .

if the node does not have any left child or right child then the element of the array lc or rc contains a value ±1. Then we need to find the value present at index 4 in array lc and rc since E is present at index 4 in the array arr.The array lc and rc contains the index of the array arr where the data is present .The value present at index 4 in the array lc is 9 which is the index position of node H so the left child of the node E is H .The right child of the node E is empty because the value present at index 4 in the array rc is -1 . Index: 0 B 3 4 1 2 1 C 5 6 2 3 D -1 -1 4 E 9 -1 5 F -1 -1 6 G -1 -1 7 \0 -1 -1 8 \0 -1 -1 H -1 -1 9 ARR A LC RC Suppose we want to find the left and right child of the node E.Some elements of the array arr contains¶\ 0¶ which represents an empty child. The 0th element of the array arr that contains the data is always the root node of the tree.

Some node also have NULL value in its left or right link field because the respective child of that particular node could be empty. .Linked Representation of Binary trees:.Binary trees can be represented by links where each node contains the address of the left child and the right child. The leaf node contains a NULL value in its link fields as there is no child to a leaf node.

A B C N D N E N N F N N G N N x N .

We first process the left sub tree terminal node D then root of D that is B and the last the right sub tree of B is E.C. Traverse the right sub tree In this technique first of all we process the left sub tree T1 of the root node R in the binary tree T.Then process the root R and at last we process the right sub tree T2 of R.F.L.K In order In -Order .M.G.J. Visit the root 3.H.O.A.E.We first process F and then right sub tree of E . Thus all the nodes of left sub tree is processed .There is no right sub tree hence we process E. Secondly we process root R and then process right sub tree T2 in the similar fashion as T1 and resulting list is as follows: D. Traverse the left sub tree 2.Traversal of a binary Tree:L Root R 1.B.The left sub tree of E is F that is terminal node.

Algo: step 1: [do through step 4] if node != NULL Step 2: call inorder(left child [node]) Step 3: output info[node] step 4: call inorder(Right child[node]) Step 5:exit .

L Root R A B D F L E G J M O C H K D.F.C.O.K .L.A.B.J.M.G.E.H.

H. E.left. F. L. D. J.right) A B D F L E G J M O C H K P R E O R D E R A . G. M. B.PRE-ORDER:-( root. C. K .O .

Visit the root 2. 1.Traverse the left sub tree 3. right) : in this method each node is visited before its children are visited the root is visited first. left.PRE-ORDER:-( root.Traverse the right sub tree Algo: step 1: [do through step 3] if node != NULL output info[node] Step 2: call preorder(left child [node]) Step 3: call preorder(Right child[node]) Step 4:exit .

The final list of elements after traversing binary is as follows: D. Traverse the right sub tree 3.B.J. that is B.We process D first Now we consider right sub tree of B that is E the left sub Tree of E is F so process F at the second Priority.Post -Order Post Order:-( left.F.C.A . Traverse the left sub tree 2.Then the right sub tree T2 and at last the root R The left sub tree of of R is T1 root of T1 is B and its left sub tree is terminal node. right.G.L.E. Visit the root In this technique first of all we process the left sub tree T1 of the root node R in the binary tree T. In the same fashion we process all the nodes of the right sub tree T2.K. root) Post order 1.H.As there is no right sub tree of E we process E and at last root of the sub tree T1.M.O.

root) A B D F L E G J M O C H K D.A .F.E.B.M. right.H.L.J.K.G.( left.C.O.

Algo: step 1: [do through step 4] if node != NULL Step 2: call postorder(left child [node]) Step 3: call postorder(Right child[node]) step 4: output info[node] Step 5 exit .

+ .left. right.In-Order: L Root R PRE-ORDER:-( root . root) + * / 4 2 7 3 1 In-Order: 4/2*7+3-1 Pre-Order:+ * / 4 2 7 ± 3 1 Post Order: 4 2 / 7 * 3 1 . right) Q Post Order:-( left.

left./ * / e + g h . right) Q Post Order:-( left.d e .In-Order: L Root R PRE-ORDER:-( root . right.+ d e ± f g + h . root) * + a b d c f In-Order: a + b ± c * d ± e / f + g ± h Pre-Order: * + a ± b c / .+ f g h Post Order: a b c .

BINARY SEARCH TREES Binary search Tree A Binary Search Tree (BST) is an ordered Binary Tree Such that either it is empty tree or following properties: 1. The advantage to its use is that it provides fast searching. Each data value in it¶s left sub tree is less than the root value 2. left and right sub tree are again binary tree. . Each data value in it¶s right sub tree is greater than the root value 3. A binary tree that has these properties is called a binary search tree.

20.75.88.89.20. 75.30.Draw binary search tree of the following elements and give the leaf nodes.30 q Level 0 75 35 23 30 Level 3 89 Level 1 88 98 Level 2 20 Leaf Node.23.98 Non Leaf Node.35.35.88.98.75.23. non leaf nodes and depth.89 Depth is 4 .

17.8.17.8.10.8.7.17.10.6.12.18.10.20 Pre:20.12.Draw binary search tree of the following elements and given 20.8.12.18.13.10.7.18 Post:5.13.13.7.5.6.18.6.6.12.13.5 17 6 8 7 20 18 5 10 13 12 In-Order:5.20 q .7.17.

25.17.10.8.18.23 q 14 10 8 11 22 23 12 18 17 20 25 .12.11.12.Draw 14.10.22.20.11.20.

num . void main( ) { struct btreenode *bt . . int req. }. bt = NULL . void inorder ( struct btreenode * ) .h> #include <conio.h> struct btreenode { struct btreenode *leftchild .h> #include <malloc. void postorder ( struct btreenode * ) . void insert ( struct btreenode **. void preorder ( struct btreenode * ) . int data .#include <stdio. /* empty tree */ printf ( "Specify the number of items to be inserted: " ) . i = 1. int ) . struct btreenode *rightchild .

inorder ( bt ) . printf ( "\nPre-order Traversal: " ) .scanf ( "%d". &num ) . } printf ( "\nIn-order Traversal: " ) . scanf ( "%d". while ( i <= req ) { printf ( "Enter the data: " ) . i++. getch( ). preorder ( bt ) . printf ( "\nPost-order Traversal: " ) . postorder ( bt ) . &req ) . num ) . } . insert ( &bt.

( *sr ) -> data = num . traverse to left */ if ( num < ( *sr ) -> data ) insert ( &( ( *sr ) -> leftchild ). return . num ) . } else /* search the node to which new node will be attached */ { /* if new data is less. } } . else /* else traverse to right */ insert ( &( ( *sr ) -> rightchild ). ( *sr ) -> rightchild = NULL . ( *sr ) -> leftchild = NULL . int num ) { if ( *sr == NULL ) { *sr = malloc( sizeof ( struct btreenode ) ) . num ) .void insert ( struct btreenode **sr.

printf ( "\t%d".void inorder ( struct btreenode *sr ) { if ( sr != NULL ) { inorder ( sr -> leftchild ) . inorder ( sr -> rightchild ) . } } . sr -> data ) .

preorder ( sr -> rightchild ) . sr -> data ) . preorder ( sr -> leftchild ) .void preorder ( struct btreenode *sr ) { if ( sr != NULL ) { printf ( "\t%d". } } .

printf ( "\t%d". sr -> data ) . postorder ( sr -> rightchild ) . } } .void postorder ( struct btreenode *sr ) { if ( sr != NULL ) { postorder ( sr -> leftchild ) .

6.8 Post: 6.17.17.8.17.8.20 Pre: 20.Input: 20.20 .6.17.8 17 6 8 20 Output: In-Order: 6.

But since this left sub tree is empty the new node is made the left child of the node that holds a value 8 .it is compared with the root node which holds a value 20 since 7 is less than 20 the searching of appropriate position of new node will be done in the left sub tree.Suppose the new node that is to be inserted holds a value 7 in its data field. Now the root node of the right or left sub tree is taken and its data is compared with the data that is to be inserted and the same procedure is repeated.initially the data that is to be inserted is compared with the data of the root node.if the data is found to greater than or equal to the data of the root node then the new node is inserted in the right sub-tree of the root node.otherwise the new node is inserted in the left sub tree of the root node.Operations on a Binary Search Tree Inserting of a node in bst Inserting a node in bst To insert any node into a BST. Ex. Next 7 is compared with 6 and since 7 is greater than 6 the searching of appropriate position of new node is done in the right sub tree of the node that holds a value 6 now 7 is compared with 8 and since 7 is less than 8 the searching of appropriate positon of the new node is done in the left sub tree.to find the appropriate position of this new node in the tree.

20 17 6 5 8 10 9 BST before Insertion 18 24 23 25 29 .

20 17 6 5 7 9 BST After Insertion 8 10 18 24 23 25 29 .

ITEM. LEFT[NEW]=NULL and RIGHT[NEW]=NULL 4.AVAIL.PAR) 2.LEFT.RIGHT.LEFT. Call FIND(INFO.LOC.RIGHT.ROOT.Algo: INSBST(INFO.LOC) 1.ITEM. AVIL = LEFT[AVIL] and INFO[NEW]=ITEM c) Set LOC=NEW.ROOT. [ add ITEM to tree] if PAR=NULL THEN Set ROOT=NEW else if ITEM<INFO[PAR] THEN Set LEFT[PAR]=NEW else Set RIGHT[PAR]=NEW 5. [ Copy the ITEM into new node in AVAIL list] a) if AVIL = NULL then write: OVERFLOW and Exit b) set NEW= AVIL . Exit . If LOC != NULL then Exit 3.

Which of these to set to NULL depends upon whether the node being deleted is a left child or a right child of its parent.Deletion a node from BST Deletion a node form Bst To delete a node into BST there are four case that we need to consider: Case (a) No Node in the tree contains the specified data In this case we print the message that the data item is not present in the tree. . So we have to adjust the pointer of the parent of the node to be deleted such that after deletion it points to the child of the node being deleted. Case (c) The node containing the data has exactly one child In this case node to be deleted has one child. Case (b) The node containing the data has no children In this case node to be deleted has no children the memory occupied by this should be freed and either the left link or the right link of the parent of this node should be set to NULL.

20 17 6 5 8 10 9 Before deletion 18 24 23 25 29 .

case C 20 17 6 5 9 After Deletion 10 18 24 23 25 29 .

This in order successor would always have one or zero child. This in order successor should then be deleted using the same procedure as for deleting a one child or a zero child node. Consider node 8 shown in fig before deletion. So the solution is more complex. .Case (d) The node containing the data has two children In this case node to be deleted has two children. The inorder successor of the node 8 is node 9 the data of this in order successor should now be copied into the node to be deleted and a pointer should be set up pointing to the in ± order successor (node 9).copy its data and reduce the problem to simple deletion of a node with one or zero child. Thus the whole logic of deleting a node with two children is to locate the in order successor.

20 17 6 5 7 9 Before deletion 8 10 18 24 23 25 29 .

case D 20 17 6 5 7 9 10 After Deletion 18 24 23 25 29 .

loc) call Dele(parent.parent.loc) Step 1: call search_node(info.parent.Algo: case D Delete_node(info.loc) Step 2: if loc=NULL output ³Information does exists in the tree´ and Exit else if left_child[loc]!=NULL and right_child[loc]!=NULL call Delete_node(parent.loc) Search Algo Case C :Delete_node Algo Delete_node(parent.loc) step 1: ( i ) Temp=Right_child[Loc] (i i) Parent_succ=Loc Step 2: Repeat while Left_child[Temp]!=NULL .

parent_succ) Step 4: (i) if parent!= NULL then if Loc = Left_child[Parent] then Left_child[parent]=Temp else Left_child[Temp]=Left_child[Loc] Right_child[Successor]=Right_child[Loc] Step 4: Return Dele Algo Dele(Loc.( i ) parent_succ=Temp (ii) Temp=Left_child[Temp] (iii) Successor=parent (iv) parent_Successor= save Step 3: call Dele(Temp.Parent) Step 1: [Initialization] if Left_child[Loc]=NULL then Dnode=Right_child[Loc] Else Dnode=Left_child[Loc] .

Step 2: if Parent !=NULL then if Loc=Left_child[Parent] then Left_child[Parent]=Dnode Else Right_child[Parent]=Dnode Else Root=Dnode Step 3: Return .

Same procedure is repeated for the left or right sub tree until the data is found Ex. .Search a node form BST To search any node in a binary Tree the data that is to be searched is compared with the data of the root node. Now 8 is compared 17 and since 8 is less than 17 the searching would proceed in the left sub tree of the node which holds a value 17 .otherwise Searching process proceeds in the left sub tree of the root node.Next 8 is compared with 6 and since 8 is greater than 6 the searching proceeds in the right sub tree of the node which holds a value 6 .now 8 is compared with the node which holds a value 8 and since 8 is found the searching process ends here.Suppose the data that is to be searched is 8 initially 8 is compared with root Search a node from Bst node which holds a value 20 and since 8 is less than 20 the searching would proceed in the left sub tree.If the data is found to be greater than the data of the root node then the searching process proceeds in the right sub tree of the root node .if the data is equal to the data of the root node then the searching is successful.

20 17 6 5 7 8 10 18 .

Alg o Search_node(node.info) Step 1: [initialize] Flag=0 Step 2: Repeat Through step 3 while node!= NULL Step 3: if info[node]=info flag=1 return(flag) else if info<info[node] node=Left_child[node] else node=Right_child[node] Step 4: Return(flag) .

11.11.Input: 9 8 7 10 11 13 12 14 15 In Order: 7 .8.10.15 after del: 7.13.13.14.12.14.12.15 .9.9.8.

void search ( struct btreenode **. 12. i = 0. void delete ( struct btreenode **. 10.h> #include <conio.h> #define TRUE 1 #define FALSE 0 struct btreenode { struct btreenode *leftchild . a[ ] = { 11. void main( ) { struct btreenode *bt . int ) . 9.h> #include <alloc. . num. }. 15. void insert ( struct btreenode **. int data . struct btreenode *rightchild . int * ) .struct btreenode **. int. 14. int ) . 7 } . 13. 8.#include <stdio. int req. void inorder ( struct btreenode * ) . struct btreenode **.

/* empty tree */ while ( i <= 8 ) { insert ( &bt. 14 ) . i++ . printf ( "\nBinary tree after deletion:\n" ) . delete ( &bt. } . inorder ( bt ) . delete ( &bt. } printf ( "Binary tree before deletion:\n" ) . delete ( &bt. a[i] ) . inorder ( bt ) . inorder ( bt ) . getch( ). printf ( "\nBinary tree after deletion:\n" ) . printf ( "\nBinary tree after deletion:\n" ) . 13 ) . 10 ) . inorder ( bt ) .bt = NULL .

( *sr ) -> rightchild = NULL . ( *sr ) -> leftchild = NULL . num ) . } else { if ( num < ( *sr ) -> data ) insert ( &( ( *sr ) -> leftchild ). . int num ) { if ( *sr == NULL ) { *sr = malloc ( sizeof ( struct btreenode ) ) . ( *sr ) -> data = num .void insert ( struct btreenode **sr.

int num ) { int found . } } void delete ( struct btreenode **root.else insert ( &( ( *sr ) -> rightchild ). } parent = x = NULL . return . *xsucc . . num ) . /* if tree is empty */ if ( *root == NULL ) { printf ( "\nTree is empty" ) . *x. struct btreenode *parent.

search ( root. num. } /* if the node to be deleted has two children */ if ( x -> leftchild != NULL && x -> rightchild != NULL ){ parent = x . &found ) . return . &x. not found" ) . xsucc = x -> rightchild . while ( xsucc -> leftchild != NULL ) { parent = xsucc . xsucc = xsucc -> leftchild . } . /* if the node to deleted is not found */ if ( found == FALSE ) { printf ( "\nData to be deleted. &parent.

x = xsucc . else parent -> leftchild = NULL . free ( x ) . .x -> data = xsucc -> data . return . } /* if the node to be deleted has only rightchild */ if ( x -> leftchild == NULL && x -> rightchild != NULL ) { if ( parent -> leftchild == x ) parent -> leftchild = x -> rightchild . } /* if the node to be deleted has no child */ if ( x -> leftchild == NULL && x -> rightchild == NULL ) { if ( parent -> rightchild == x ) parent -> rightchild = NULL .

else parent -> rightchild = x -> rightchild . } /* if the node to be deleted has only left child */ if ( x -> leftchild != NULL && x -> rightchild == NULL ) { if ( parent -> leftchild == x ) parent -> leftchild = x -> leftchild . free ( x ) . } } . else parent -> rightchild = x -> leftchild . return . free ( x ) . return .

return . } . struct btreenode **x. q = *root . *found = FALSE . *x = q . while ( q != NULL ) { /* if the node to be deleted is found */ if ( q -> data == num ) { *found = TRUE . *par = NULL . int *found ) { struct btreenode *q . int num.void search ( struct btreenode **root. struct btreenode **par.

} } . inorder ( sr -> rightchild ) . } } void inorder ( struct btreenode *sr ) { if ( sr != NULL ) { inorder ( sr -> leftchild ) . else q = q -> rightchild .*par = q . printf ( "%d\t". if ( q -> data > num ) q = q -> leftchild . sr -> data ) .

The efficiency of searching is ideal if the difference between the heights of left and right sub trees of all the nodes in a binary search tree is at the most one.frequent insertion and deletions in a BST is likely to make it unbalanced.AVL TREE Searching in a binary search tree is efficient if the heights of both left and right sub-trees of any node are equal. The structure of a node of an AVL tree is given below: .However. two for storing the address of the left and right child and an additional field is required to hold the balance factor.M.such a binary search tree is called as BALANCED BINARY TREE. Hence such tree are also known as AVL tree. To represent a node of an AVL tree four fields are requiredone for data. Landis. The balance factor of any node is calculated by subtracting the height right sub-tree of the node form the height of the left sub tree.M.Adelson-velskii and E.It was invented in the year 1962 by two Russian mathematicians-G.

3. 2. The value of balfact of any node is ±1. Struct AVL *right. 1.0 or 1. 4. ( balfact = left ± right ) . }. Int data.if it is other than these three values then the tree is not balanced or it is not an AVL tree. Int balfact. If the value of balfact of any node is 0 then the height of its left and right sub-tree is exactly same. If the value of balfact of any node is 1 then the height of the left sub-tree of that node is one more than the height of its right subtree.Struct AVL { Struct AVL *left. If the value of balfact of any node is ±1 then the height of the right sub-tree of that node is one more than the height of its left sub-tree.

Ex I -1 Ex II A 0 B 0 D AVL TREE C 1 2 A -1 B 0 D F 0 NOT A AVL TREE E 1 C 0 .

Insertion of Node in AVL Tree We can Insert a new node in an AVL tree by finding its appropriate position. 0 20 0 0 6 17 0 18 -1 23 25 0 Before Addition . But insertion of a new node involves cretain overheads since the tree may become unbalanced due to the increase in its height. as the height of the tree does not increase. If the new node is inserted as a child of any non-leaf node then there will be no effect on its balance.

The nodes are adjusted in such a way that the resultant tree becomes a balanced tree.0 20 0 0 6 17 0 18 0 23 0 0 25 21 after Addition Addition of a new node to the leaf node of a sub tree make it unbalance then to re-balance and make it an AVL tree the nodes need to be properly adjusted. . So after insertion of a new node the tree is traversed starting from the new node to the node whose balance factor is disturbed.

0 -1 6 5 0 10 20 1 0 12 15 0 27 -1 13 29 32 0 0 0 Before Addition 1 20 -2 1 6 0 -1 -1 29 0 5 12 32 13 0 1 10 15 0 27 0 13 after Addition .

LEFT ROTATION Case A: As seen form fig. on insertion of the new node the balance factor of the node containing the data 6 violates the condition of an AVL tree. To re-balance the tree we are required to make a left rotation makes the node containing the data 6 as the left child of the node containing the data 12 and the node containing the data 10 as a right child of the node containing the data 6. 0 0 0 5 6 0 10 12 20 1 15 0 0 1 -1 26 29 32 27 0 after Rotation 0 13 .

0 -1 6 5 0 10 20 1 0 12 15 0 27 -1 13 29 32 0 0 0 Before Addition .Case B: Now suppose instead of 13 we insert a node with value 11. this new node would get inserted as the right child of the node containing a value 10. After this the tree no longer remains a balanced tree.

.0 -2 6 20 1 1 12 15 0 -1 13 29 32 27 0 after Addition 0 0 5 -1 10 0 11 Right ROTATION To re-balance the tree we are required to make initially a right rotation of the tree along the node 12. Right rotation makes node 10 the right child of node 6 node 12 becomes the right child of node 10 and node 11 becomes the left child of node 12.

As a result node 10 becomes the left child of the node 20. since there is no left child for node 10 the right child of node 6 is empty. Thus finally the tree becomes a balanced binary tree of AVL Tree. .1 -2 6 5 20 1 -2 10 -1 13 29 32 after first Rotation 0 0 0 12 0 27 0 0 15 11 double ROTATION But now the tree is not balanced and hence the tree is rotated to left along with node 6. The node 6 becomes the left child of the node 10. this process first to right and then to the left rotation is known as Double Rotation.

0 0 10 6 0 5 0 11 20 1 0 12 -1 13 29 32 0 after second Rotation 0 1 0 27 15 .

95.26.45.80.Q1. Draw a AVL Tree 50.105 0 0 1 2 0 1 Critical Node 45 -1 0 26 0 43 Balance 0 26 0 43 0 45 50 1 -1 0 80 0 95 0 50 -1 80 0 95 .43.

-1 50 0 43 0 26 0 45 -2 80 -1 95 0 105 Critical Node 0 50 0 43 0 26 0 45 Balance 0 95 0 80 105 0 .

50.Q2.75 step 1: 25 critical node 45 50 step 4: 45 25 50 55 step 5: 45 25 critical 50 node 55 60 step 2: Balance 45 25 50 25 45 step 3: 25 . Draw a AVL Tree 25.55.60.65.45.

Balance: 45 25 50 step 6: 25 50 55 60 Balance: 55 55 45 60 65 25 50 65 60 45 critical node .

step 7: 55 Balance: 55 critical node 45 25 65 75 50 65 60 75 45 25 50 60 .

7.2.Q2.4.1. Draw a AVL Tree 5.0.3.8.9 step 1: step 3: 2 5 5 step 4: 7 2 0 step 5: 2 0 3 5 7 step 2: 2 5 7 5 .6.

step 6: 2 0 5 critical node 7 Balance: 2 0 3 5 4 7 3 4 step 7: 3 2 0 4 6 5 7 .

step 8: critical 2 0 1 6 step 9: 1 0 2 4 6 3 5 7 8 4 3 5 0 7 Balance: 1 2 4 6 3 5 7 .

critical node step 10: 1 0 2 4 6 balance 1 0 2 4 5 6 3 7 8 9 3 5 critical node 7 8 9 .

26 balance 43 23 15 -2 23 75 64 0 1 23 0 26 0 64 58 64 75 86 0 75 .58.43.Q 3 Draw AVL Tree 23.86.64.75.15.

-1 64 23 -1 75 0 86 -1 23 0 64 0 43 0 64 0 58 -1 75 0 86 1 2 23 64 1 43 0 58 -1 75 0 86 23 0 0 43 -1 75 0 86 .

1 43 23 1 15 0 1 64 0 58 -1 75 0 86 1 43 23 15 0 0 0 26 1 64 0 58 -1 75 0 86 .

#include <stdio.h> #include <conio.h> #include <malloc.h>

#define FALSE 0 #define TRUE 1 struct AVLNode { int data ; int balfact ; struct AVLNode *left ; struct AVLNode *right ; }; struct AVLNode * buildtree ( struct AVLNode *, int, int * ) ; struct AVLNode * deldata ( struct AVLNode *, int, int * ) ; struct AVLNode * del ( struct AVLNode *, struct AVLNode *,int*) ; struct AVLNode * balright ( struct AVLNode *, int * ) ; struct AVLNode * balleft ( struct AVLNode *, int * ) ; void display ( struct AVLNode * ) ; void deltree ( struct AVLNode * ) ;

void main( ) { struct AVLNode *avl = NULL ; int h ; clrscr( ) ; avl = buildtree ( avl, 20, &h ) ; avl = buildtree ( avl, 6, &h ) ; avl = buildtree ( avl, 29, &h ) ; avl = buildtree ( avl, 5, &h ) ; printf ( "\nAVL tree:\n" ) ; display ( avl ) ; avl = deldata ( avl, 20, &h ) ; avl = deldata ( avl, 29, &h ) ; printf ( "\nAVL tree after deletion of a node:\n" ) ; display ( avl ) ; deltree ( avl ) ; getch( ) ; }

/* inserts an element into tree */ struct AVLNode * buildtree (struct AVLNode *root,int data, int *h ) { struct AVLNode *node1, *node2 ; if ( !root ) { root = ( struct AVLNode * ) malloc ( sizeof ( struct AVLNode ) ) ; root -> data = data ; root -> left = NULL ; root -> right = NULL ; root -> balfact = 0 ; *h = TRUE ; return ( root ) ; }

if ( data < root -> data ) { root -> left = buildtree ( root -> left, data, h ) ; /* If left subtree is higher */ if ( *h ) { switch ( root -> balfact ) { case 1: node1 = root -> left ; if ( node1 -> balfact == 1 ) { printf ( "\nRight rotation along %d.", root -> data ) ; root -> left = node1 -> right ; node1 -> right = root ; root -> balfact = 0 ; root = node1 ; }

else node1 -> balfact = 0 . node1 -> right = node2 -> left printf ( " then right along %d. . node1 -> data ) . if ( node2 -> balfact == -1 ) node1 -> balfact = 1 . node2 = node1 -> right . root -> left = node2 -> right . left along %d". root = node2 . else root -> balfact = 0 . node2 -> right = root . if ( node2 -> balfact == 1 ) root -> balfact = -1 .\n". root -> data ) . node2 -> left = node1 .else { printf ( "\nDouble rotation.

break . break . *h = FALSE .} root -> balfact = 0 . } } } . case 0: root -> balfact = 1 . case -1: root -> balfact = 0 . *h = FALSE .

. h ) . /* If the right subtree is higher */ if ( *h ) { switch ( root -> balfact ) { case 1: root -> balfact = 0 . break . case -1: node1 = root -> right . *h = FALSE . data.if ( data > root -> data ) { root -> right = buildtree ( root -> right. break. case 0: root -> balfact = -1 .

node2 = node1 -> left . right along %d". root -> right = node2 -> left .if ( node1 -> balfact == -1 ) { printf ( "\nLeft rotation along %d.". node1 -> left = node2 -> right . root -> data ) .\n". node2 -> right = node1 . printf ( " then left along %d. root -> right = node1 -> left . } else { printf ( "\nDouble rotation. root = node1 . node2 -> left = root .node1 -> data ) . node1 -> left = root . . root -> balfact = 0 . root -> data ) .

} } } return ( root ) .if ( node2 -> balfact == -1 ) root -> balfact = 1 . } root -> balfact = 0 . *h = FALSE . root = node2 . else root -> balfact = 0 . if ( node2 -> balfact == 1 ) node1 -> balfact = -1 . else node1 -> balfact = 0 . } .

data. } . if ( *h ) root = balright ( root." ) . int data. int *h ) { struct AVLNode *node . h ) ./* deletes an item from the tree */ struct AVLNode * deldata ( struct AVLNode *root. return ( root ) . } else { if ( data < root -> data ) { root -> left = deldata ( root -> left. if ( !root ) { printf ( "\nNo such data. h ) .

h ) . if ( node -> right == NULL ) { root = node -> left . *h = TRUE . } else { node = root .else { if ( data > root -> data ) { root -> right = deldata ( root -> right. } . if ( *h ) root = balleft ( root. h ) . free ( node ) . data.

} else { node -> right = del ( node -> right. free ( node ) . if ( *h ) root = balleft ( root. h ) . *h = TRUE . h ) .else { if ( node -> left == NULL ) { root = node -> right . } } } } } return ( root ) . node. } .

node. *h = TRUE . int *h ) { struct AVLNode *temp = succ . node -> data = succ -> data . struct AVLNode*node. succ = succ -> right . if ( *h ) succ = balright ( succ. } return ( succ ) . } else { temp = succ . if ( succ -> left != NULL ) { succ -> left = del ( succ -> left. } . h ) . h ) . free ( temp ) .struct AVLNode * del ( struct AVLNode *succ.

/* balances the tree. break. int *h ) { struct AVLNode *node1. switch ( root -> balfact ) { case 1: root -> balfact = 0 . *h = FALSE . case -1: node1 = root -> right . . *node2 . break. case 0: root -> balfact = -1 . if right sub-tree is higher */ struct AVLNode * balright ( struct AVLNode *root.

if ( node1 -> balfact == 0 ) { root -> balfact = -1 . } else { root -> balfact = node1 -> balfact = 0 . root -> data ) . *h = FALSE .printf ( "\nLeft rotation along %d. root -> right = node1 -> left . .". node1 -> data ). node1 -> balfact = 1 . } root = node1 . right along %d". node1 -> left = root . } else { printf ( "\nDouble rotation.

root -> data ) . break . break .". switch ( root -> balfact ) { case -1: root -> balfact = 0 ./* balances the tree. case 1: node1 = root -> left . *node2 . if ( node1 -> balfact >= 0 ) { printf ( "\nRight rotation along %d. case 0: root -> balfact = 1 . int *h ) { struct AVLNode *node1. if left sub-tree is higher */ struct AVLNode * balleft ( struct AVLNode *root. *h = FALSE . .

*h = FALSE .root -> left = node1 -> right . } else { . node1 -> right = root . node1 -> balfact = -1 . } else { root -> balfact = node1 -> balfact = 0 . if ( node1 -> balfact == 0 ) { root -> balfact = 1 . } root = node1 .

} } return ( root ) . if ( node2-> balfact == -1 ) node1 -> balfact = 1 . node2 -> right = root . root = node2 . node2 = node1 -> right . root -> left = node2 -> right . } . node1 -> right = node2 -> left . left along %d". if ( node2 -> balfact == 1 ) root -> balfact = -1 . node2 -> balfact = 0 . else root -> balfact = 0 . node1 -> data ) .printf ( "\nDouble rotation.\n". node2 -> left = node1 . else node1 -> balfact = 0 . printf ( " then right along %d. root -> data ) .

node1 -> left = node2 -> right . else node1 -> balfact = 0 . node2 -> right = node1 . if ( node2 -> balfact == 1 ) node1 -> balfact = -1 . printf ( " then left along %d. else root -> balfact = 0 . node2 -> left = root . root -> right = node2 -> left .if ( node1 -> balfact <= 0 ) { node2 = node1 -> left .\n". if ( node2 -> balfact == -1 ) root -> balfact = 1 . . root -> data ).

root = node2 ; node2 -> balfact = 0 ; } } return ( root ) ; } /* displays the tree in-order */ void display ( struct AVLNode *root ) { if ( root != NULL ) { display ( root -> left ) ; printf ( "%d\t", root -> data ) ; display ( root -> right ) ; } }

/* deletes the tree */ void deltree ( struct AVLNode *root ) { if ( root != NULL ) { deltree ( root -> left ) ; deltree ( root -> right ) ; } free ( root ) ; }

Algo Insert In AVL
Note: That Nodes are inserted into an AVL tree is the same manner as an ordinary binary search Tree. However the insertion algo for an AVL tree Travels back along the path it took to find the point of insertion and checks the balance at each node on the path. If a node is found that is unbalanced if it has a balance factor of either -2 and +2 then rotation is performed based on the inserted nodes position relative to the node being examined.

Algo Delete Node in AVL Step 1: P has balance factor and it is equal :than it is changed according to left or right sub tree so it¶s value is FALSE Step 2: P has balance factor not equal and taller sub tree was short: than change balance factor p equal and leave it shorter value TRUE. we have 3 cases: Case 3 a: balance factor of q is equal : A single rotation restores the balance and shorter balance became FALSE . Step 3: P has balance factor not equal and short subtree was also short: apply rotation follows to restore the balance factor let be the root of taller subtree of p.

Case 3 C: Balance Factor of p and Q are opposite.set balance factor p and q to equal and leave shorter as TRUE. .Case 3 b: Balance factor of q is same as that of p: Apply single rotation . Step 4: Apply Double rotation set balance factor of new root to equal and other balance factor as appropriate and leave shorter as TRUE.

The non-leaf root node may have at the most n non-empty child and at least two child nodes. To improve the efficiency of tree operations Multi-way search trees can be used. 3. To improve the efficiency of operation performed on a tree we need to reduce the height of a tree and if a node contains more number of values then at a time more values can be accessed from the memory.e. A B-Tree can exist with only one node. 2.B-Tree The number of values that a particular node of a binary search tree or an AVL tree can hold is only one. . i. the root node containing no child. B-Tre is a Multi-way search tree of order n that satisfies the following conditions: B-Tree 1. On the other hand a 2-3 tree can hold only 2 value per node. All the non-leaf nodes( except the root node ) have at least n/2 children and at the most n children.

All the values that appear on the left most child of a root node are smaller that the first value of root node and All the values that appear on the right most child of a node are greater that the last value of that root node. All the values of a particular node are in increasing order. 5.4. 23 39 3 17 31 B-Tree Of order 3 43 65 . B-Tree of order n is a tree in which any node may maximum n1 values and maximum of n children. 7. all the leaf nodes should appear on the same level. 6.

40 B-Tree Of order 4 47. 27 42 2.23 39 3 17 31 43 65 B-Tree Of order 3 In a B Tree of order 4 any node can contain maximum three values and 4 children.13. 51 . 22 32.

51 Ex: Suppose we want to insert a value 37 to following B-Tree.Suppose a value k is to be inserted in a B-Tree of order 4. After searching for the appropriate leaf node to insert the value k the values present at that particular leaf node is searched.13. Since the node have only two values the third value can be added and hence the insertion process ends here. here the maximum number of values that any node may have is 3. 27 42 Insertion in B-Tree B- 2. 22 32. This leads to two possibilities: Case A:The leaf node is not full (does not contain 3 values): if the leaf node is not full then the value is inserted at its appropriate position and the process end.40 47. The value 37 is inserted between 72 & 40 because . So first we have to search the node where to insert this value. In our case it is the leaf node that contains the values 32 and 40.

If the parent node is full then same process is repeated. If n is n is the order of the tree then any node is always split after the value n/2-1 i.e. And n/2 i. 4/2=2 children. The second part of the node contains the last n-n/2 value i.13.e. 4/2-1 = 1 values. 5. 4/2th = 2nd value is moved up to the parent node and the new value is attached to one of the two split nodes.e. 51 Case B: The leaf node is full (contains 3 values) : If the leaf node is full . 3.e. 1.e.The value of any node of a B-Tree always appear in ascending order. then that node is split into two nodes . 22 32. 27 42 2.e. 4-4/2= 2 and n-n/2 +1 child i.40 47. . The n/2th value i.37. 4-4/2+1 = 3 children 4. 2. As a result the first part of the node contains the first n/2-1 values i. 4/2-1 = 1.

40 47. 22 .37.Ex: 27 42 2. 22 32. 51 Suppose the value that is to be inserted is 19 . In our case it is the node which contains values 2. .13. Since this node already contains three values no more values can be added to this node. To begin with the node in which 19 can be inserted is searched. First part hold only n/2-1=1 value in our case that is 2 and second part hold last n-n/2 that is 4-4/2=2 value in our case that is 19 and 22 and the value 13 is moved up to the parent node or new value 19 is attached to second node because it is greater than 13. Hence as per condition the node is split after the value 4/2-1=1 using this condition we split the node in two parts.13.

22 32.7.40 47.8.37.6.9.5.3.13 27 42 2 19. 51 Addition of new value in B-Tree of order 4 Q Draw a B-Tree of order 3 2.4.10 sol: we know that maximum no of value in one node is n-1 so 3-1=2 .1.

step 1 step 2 2 4 2 step 3 : Here Overflow problem is arise so node split in two parts 2 4 4 2 step 4 : 4 2 89 9 9 .

step 5 : 2 4 89 7 Overflow problem node split in two parts 4 8 2 7 9 step 6: 4 8 2 67 9 .

step 7: 4 8 23 67 9 step 8: 4 8 1 23 67 9 Overflow problem IN NODE 2 4 8 Overflow problem IN parent .

4 2 1 step 9 2 1 3 5 2 1 3 5 7 67 3 67 8 9 4 8 9 Overflow problem 4 68 9 .

step 10 4 2 1 3 5 7 68 9 10 .

e. suppose we want to delete the value 64 on deleting this value the number of values that are left in the node are 2. . After count the value there are two possibilities : Case A: The Number of values is greater than or equal to the minimum number of values required (i. Hence the deletion process ends hear. 2) for a b-tree of order 5.Deletion In B-Tree B53 14 32 3 7 79 96 19 27 37 42 59 64 71 83 92 97 99 A B-Tree with Order 5 To Delete a value first of all its node is searched the valued is deleted and the number of remaining values in the node is counted. which satisfy the condition of a B-Tree .

53 14 32 3 7 79 96 19 27 37 42 59 71 83 92 97 99 A B-Tree After Deletion .

.Suppose We want to delete 92 53 14 32 3 7 79 96 19 27 37 42 59 71 83 92 97 99 Case B: The number of values is less than the minimum number of values required (i.e. Here again there are two possibilities Case (a): The left or right sibling of the node form which the value is deleted contains more than the required minimum number of values. So the value of its (node form which the value is deleted ) parent is moved to the node and a value from its sibling ( Left or Right which contains more number of values than the required minimum values is moved to its parent. 2) for order 5.

53 14 32 3 7 71 96 19 27 37 42 59 64 79 83 97 99 A B-Tree After Deletion .After deleting the value 92. value 79 is moved form its parent to the node form where the value is deleted. Then the value 71 is moved form its left sibling to its parent the deletion process ends here as all the nodes satisfy the condition of B-Tree.

If the parent also contains the minimum number of values then the same procedure of merging the node with its sibling is applied. 53 14 32 3 7 71 96 19 27 37 42 59 64 79 83 97 99 . Suppose we want to delete the value 42. Here the value of its ( node form which the value is deleted ) parent is moved to the node and the node is merged with its sibling.Case (b): The left or right sibling of the node form which the value is deleted contains exactly the required minimum number of values.

37. As a result the node now contains 4 values 19. the node now contains 4 values 14.53 14 71 96 3 7 19 27 32 37 59 64 79 83 97 99 After Deletion of the value 42 the node want to delete the value 42. So value 32 is copied form its parent and it is merged with its left sibling that contains values 19 and 27. . 71 and 96. As a result . So the value 53 is copied form its parent and is merged with its right sibling that contains value 71 and 96.27. the deletion process ends here as all the nodes satisfy the condition of B-Tree. 53.32. After deletion of the value 42 the node contains only one value 37. Now the parent node contains only one value 14 .

14 53 71 96 3 7 19 27 32 37 59 64 79 83 97 99 .

Sign up to vote on this title
UsefulNot useful