Tree & BSTMd. Shakil AhmedSoftware EngineerAstha it research & consultancy ltd.Dhaka, Bangladesh
Natural Tree
Tree structure
Unix / Windows file structure
Definition of TreeA tree is a finite set of one or more nodessuch that:There is a specially designated node calledthe root...
Level and Depth                                                                                 Levelnode (13)degree of a ...
Binary Tree• Each Node can have at most 2 children.
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• F...
Array Representation 1• Advantage ->1.Good in Full Or Complete  Binary tree• Disadvantage1.If we use it in normal binary  ...
Array Representation 2 • Use 3 Parallel Array        0   1     2     3    4        5         6        7         8Root    2...
Linked Representationtypedef struct tnode *ptnode;typedef struct tnode { int data; ptnode left, right;};                  ...
Preorder Traversal (recursive version)Linked Representation           Array Representation 2void preorder(ptnode ptr)     ...
Inorder Traversal (recursive version)Linked Representation              Array Representation 2void inorder(ptnode ptr)    ...
Postorder Traversal (recursive version)Linked Representation              Array Representation 2void postorder(ptnode ptr)...
Binary Search Tree• All items in the left subtree are less than the  root.• All items in the right subtree are greater or ...
Binary Search Tree        16
Binary Search TreeElements => 23 18 12 20 44 52 351st Element2nd Element3rd Element
Binary Search Tree4th Element5th Element
Binary Search Tree6th Element7th Element
Binary Search Tree        20
Binary Search Tree//Array Representation 2      root = 0;//Generate BST                 while(1){int N = 0;               ...
Binary Search Tree            else                    Client Code =>            {                       scanf(“%d”,&n);   ...
Binary Search Tree//Array Representation 2                                    else//Search in BST                         ...
SampleLightOj =>• http://www.lightoj.com/volume_showproble  m.php?problem=1087• http://www.lightoj.com/volume_showproble  ...
Thanks!
Upcoming SlideShare
Loading in …5
×

Tree & bst

580 views

Published on

Tree & bst

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
580
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
27
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Tree & bst

  1. 1. Tree & BSTMd. Shakil AhmedSoftware EngineerAstha it research & consultancy ltd.Dhaka, Bangladesh
  2. 2. Natural Tree
  3. 3. Tree structure
  4. 4. Unix / Windows file structure
  5. 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. 6. Level and Depth Levelnode (13)degree of a node 1leaf (terminal) 3 A 1nonterminal 2parentchildren 2 B 2 1 C 2 3 D 2 3siblingdegree of a tree (3) E 3 F 3 G 31 H I 3 J 4 2 0 0 30 0 3ancestorlevel of a nodeheight of a tree (4) 0 K 4 L 0 M 4 0 4
  7. 7. Binary Tree• Each Node can have at most 2 children.
  8. 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 it’s parent is i/2. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 2 7 5 2 6 -1 9 -1 -1 5 11 -1 -1 4 -1
  9. 9. Array Representation 1• Advantage ->1.Good in Full Or Complete Binary tree• Disadvantage1.If we use it in normal binary tree then it may be huge memory lose.
  10. 10. Array Representation 2 • Use 3 Parallel Array 0 1 2 3 4 5 6 7 8Root 2 7 5 2 6 9 5 11 4Left 1 3 -1 -1 6 8 -1 -1 -1Right 2 4 5 -1 7 -1 -1 -1 -1• If you need parent 0 1 2 3 4 5 6 7 8 Root 2 7 5 2 6 9 5 11 4 Left 1 3 -1 -1 6 8 -1 -1 -1 Right 2 4 5 -1 7 -1 -1 -1 -1 Parent -1 0 0 1 1 2 4 4 5
  11. 11. Linked Representationtypedef struct tnode *ptnode;typedef struct tnode { int data; ptnode left, right;}; data left data right left right
  12. 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. 13. Inorder Traversal (recursive version)Linked Representation Array 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. 14. Postorder Traversal (recursive version)Linked Representation Array 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. 15. Binary Search Tree• All items in the left subtree are less than the root.• All items in the right subtree are greater or equal to the root.• Each subtree is itself a binary search tree.
  16. 16. Binary Search Tree 16
  17. 17. Binary Search TreeElements => 23 18 12 20 44 52 351st Element2nd Element3rd Element
  18. 18. Binary Search Tree4th Element5th Element
  19. 19. Binary Search Tree6th Element7th Element
  20. 20. Binary Search Tree 20
  21. 21. Binary Search Tree//Array Representation 2 root = 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. 22. Binary Search Tree else Client Code => { scanf(“%d”,&n); if(Right[root]!=-1) root = Right[root]; for(i=0;i<n;i++) else { { scanf(“%d”,&t); Root[N]=value; AddToBST(t); Left[N]=-1; } Right[N]=-1; Right[root]=N; N++; break; } } } }}
  23. 23. Binary Search Tree//Array Representation 2 else//Search in BST {int SearchInBST(int value) if(Right[root]!=-1){ root = Right[root]; if(N==0) else return -1; return -1; } root = 0; } while(1) return -1; { } if(Root[root]==value) return root; Client Code => else if(Root[root]>value) scanf(“%d”,&n); { for(i=0;i<n;i++) if(Left[root]!=-1) { root = Left[root]; scanf(“%d”,&t); else Printf(“%d”,SearchInBST(t)); return -1; } }
  24. 24. SampleLightOj =>• http://www.lightoj.com/volume_showproble m.php?problem=1087• http://www.lightoj.com/volume_showproble m.php?problem=1097• http://www.lightoj.com/volume_showproble m.php?problem=1293
  25. 25. Thanks!

×