Tree & bst

  • View
    357

  • Download
    5

Embed Size (px)

DESCRIPTION

Tree & bst

Text of Tree & bst

  • 1. Tree & BSTMd. Shakil AhmedSoftware EngineerAstha it research & consultancy ltd.Dhaka, Bangladesh

2. Natural Tree 3. Tree structure 4. Unix / Windows file structure 5. Definition of TreeA tree is a finite set of one or more nodessuch that:There is a specially designated node calledthe root.The remaining nodes are partitioned inton>=0 disjoint sets T1, ..., Tn, where each ofthese sets is a tree.We call T1, ..., Tn the subtrees of the root. 6. Level and Depth Levelnode (13)degree of a node 1leaf (terminal) 3 A 1nonterminal2parentchildren 2 B 2 1 C 2 3 D 2 3siblingdegree of a tree (3) E 3 F 3 G 31 HI 3 J 4 20 0 3003ancestorlevel of a nodeheight of a tree (4) 0 K 4 L 0 M 404 7. Binary Tree Each Node can have at most 2 children. 8. Array Representation 1 With in a single array. If root position is i then, Left Child in 2*i+1 Right Child is 2*i+2 For N level tree it needs 2^N 1 memory space. If current node is i then itsparent is i/2.0 12 3 4 56 789 10 11 12 13 142 75 2 6 -1 9 -1 -1 5 11 -1 -1 4-1 9. Array Representation 1 Advantage ->1.Good in Full Or CompleteBinary tree Disadvantage1.If we use it in normal binarytree then it may be hugememory lose. 10. Array Representation 2 Use 3 Parallel Array0 1 2 345 67 8Root2 7 5 269 5114Left1 3 -1-1 68 -1 -1-1Right 2 4 5 -1 7-1-1 -1-1 If you need parent01 2 3 45 678Root27 5 2 69 511 4Left13 -1-168 -1 -1 -1Right 24 5 -17-1-1 -1 -1Parent -1 00 1 12 445 11. Linked Representationtypedef struct tnode *ptnode;typedef struct tnode { int data; ptnode left, right;}; dataleftdata rightleftright 12. Preorder Traversal (recursive version)Linked Representation Array Representation 2void preorder(ptnode ptr) void preorder(int nodeIndex)/* preorder tree traversal */ {{ printf(%d, root[nodeIndex]);if (ptr) {if(Left[nodeIndex]!=-1)printf(%d, ptr- preorder(Left[nodeIndex]); >data);if(Right[nodeIndex]!=-1)preorder(ptr->left);preorder(Right[nodeIndex]);preorder(ptr->right); }}} 13. Inorder Traversal (recursive version)Linked RepresentationArray Representation 2void inorder(ptnode ptr) void inorder(int nodeIndex)/* inorder tree traversal */ {{if(Left[nodeIndex]!=-1)if (ptr) { inorder(Left[nodeIndex]);inorder(ptr->left);printf(%d, root[nodeIndex]);printf(%d, ptr->data); if(Right[nodeIndex]!=-1)inorder(ptr->right); inorder(Right[nodeIndex]);}}} 14. Postorder Traversal (recursive version)Linked RepresentationArray Representation 2void postorder(ptnode ptr) void postorder(int nodeIndex)/* postorder tree traversal */ {{if(Left[nodeIndex]!=-1)if (ptr) { postorder(Left[nodeIndex]);postorder(ptr->left);if(Right[nodeIndex]!=-1)postorder(ptr->right); postorder(Right[nodeIndex]);printf(%d, ptr->data); printf(%d, root[nodeIndex]);}}} 15. Binary Search Tree All items in the left subtree are less than theroot. All items in the right subtree are greater orequal to the root. Each subtree is itself a binary search tree. 16. Binary Search Tree16 17. Binary Search TreeElements => 23 18 12 20 44 52 351st Element2nd Element3rd Element 18. Binary Search Tree4th Element5th Element 19. Binary Search Tree6th Element7th Element 20. Binary Search Tree20 21. Binary Search Tree//Array Representation 2root = 0;//Generate BST while(1){int N = 0;if(Root[root]==value)int Root[1000000],break; Left[1000000],else if(Root[root]>value) Right[1000000]; { if(Left[root]!=-1)void AddToBST(int value) root = Left[root];{else if(N==0){ {Root[N]=value;Root[N]=value;Left[N]=-1;Left[N]=-1; Right[N]=-1;Right[N]=-1;Left[root]=N; }N++; else break; { } } 22. Binary Search TreeelseClient Code =>{ scanf(%d,&n);if(Right[root]!=-1)root = Right[root]; for(i=0;ielse if(Root[root]>value)scanf(%d,&n);{for(i=0;i http://www.lightoj.com/volume_showproblem.php?problem=1087 http://www.lightoj.com/volume_showproblem.php?problem=1097 http://www.lightoj.com/volume_showproblem.php?problem=1293 25. Thanks!