(Binary tree)

5,862 views

Published on

Data Structure - Binary Tree

Published in: Technology, Business
4 Comments
10 Likes
Statistics
Notes
  • Trading is a good thing. I lost a lot before I got to were I am today. if you need assistance on how to trade and recover the money you have lost email me get new amazing strategy? If you are having problems withdrawing your fund from your Forex/binary trade broker even when you were given a bonus,just contact me, i have worked with a binary broker for 7years, i have helped a lot of people and i wont stop until i have helped as many as possible, here is my email address if you have a bonus that was given to and you wish to withdraw it juanitajonathan44@gmail.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • comprehensive info
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • ya usefull!!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • it was very nice
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
5,862
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
488
Comments
4
Likes
10
Embeds 0
No embeds

No notes for slide

(Binary tree)

  1. 1. Binary Trees
  2. 2. Parts of a binary tree <ul><li>A binary tree is composed of zero or more nodes </li></ul><ul><li>Each node contains: </li></ul><ul><ul><li>A value (some sort of data item) </li></ul></ul><ul><ul><li>A reference or pointer to a left child (may be null ), and </li></ul></ul><ul><ul><li>A reference or pointer to a right child (may be null ) </li></ul></ul><ul><li>A binary tree may be empty (contain no nodes) </li></ul><ul><li>If not empty, a binary tree has a root node </li></ul><ul><ul><li>Every node in the binary tree is reachable from the root node by a unique path </li></ul></ul><ul><li>A node with neither a left child nor a right child is called a leaf </li></ul><ul><ul><li>In some binary trees, only the leaves contain a value </li></ul></ul>
  3. 3. Picture of a binary tree a b c d e g h i l f j k
  4. 4. Size and depth <ul><li>The size of a binary tree is the number of nodes in it </li></ul><ul><ul><li>This tree has size 12 </li></ul></ul><ul><li>The depth of a node is its distance from the root </li></ul><ul><ul><li>a is at depth zero </li></ul></ul><ul><ul><li>e is at depth 2 </li></ul></ul><ul><li>The depth of a binary tree is the depth of its deepest node </li></ul><ul><ul><li>This tree has depth 4 </li></ul></ul>a b c d e f g h i j k l
  5. 5. Balance <ul><li>A binary tree is balanced if every level above the lowest is “full” (contains 2 n nodes) </li></ul><ul><li>In most applications, a reasonably balanced binary tree is desirable </li></ul>a b c d e f g h i j A balanced binary tree a b c d e f g h i j An unbalanced binary tree
  6. 6. Binary search in an array <ul><li>Look at array location (lo + hi)/2 </li></ul>2 3 5 7 11 13 17 0 1 2 3 4 5 6 Searching for 5: (0+6)/2 = 3 hi = 2; (0 + 2)/2 = 1 lo = 2; (2+2)/2=2 7 3 13 2 5 11 17 Using a binary search tree
  7. 7. Tree traversals <ul><li>A binary tree is defined recursively: it consists of a root , a left subtree , and a right subtree </li></ul><ul><li>To traverse (or walk ) the binary tree is to visit each node in the binary tree exactly once </li></ul><ul><li>Tree traversals are naturally recursive </li></ul><ul><li>Since a binary tree has three “parts,” there are six possible ways to traverse the binary tree: </li></ul><ul><ul><li>root, left, right </li></ul></ul><ul><ul><li>left, root, right </li></ul></ul><ul><ul><li>left, right, root </li></ul></ul><ul><ul><li>root, right, left </li></ul></ul><ul><ul><li>right, root, left </li></ul></ul><ul><ul><li>right, left, root </li></ul></ul>
  8. 8. Preorder traversal <ul><li>In preorder , the root is visited first </li></ul><ul><li>Here’s a preorder traversal to print out all the elements in the binary tree: </li></ul><ul><li>public void preorderPrint(BinaryTree bt) { if (bt == null) return; System.out.println(bt.value); preorderPrint(bt.leftChild); preorderPrint(bt.rightChild); } </li></ul>
  9. 9. Inorder traversal <ul><li>In inorder , the root is visited in the middle </li></ul><ul><li>Here’s an inorder traversal to print out all the elements in the binary tree: </li></ul><ul><li>public void inorderPrint(BinaryTree bt) { if (bt == null) return; inorderPrint(bt.leftChild); System.out.println(bt.value); inorderPrint(bt.rightChild); } </li></ul>
  10. 10. Postorder traversal <ul><li>In postorder , the root is visited last </li></ul><ul><li>Here’s a postorder traversal to print out all the elements in the binary tree: </li></ul><ul><li>public void postorderPrint(BinaryTree bt) { if (bt == null) return; postorderPrint(bt.leftChild); postorderPrint(bt.rightChild); System.out.println(bt.value); } </li></ul>
  11. 11. Tree traversals using “flags” <ul><li>The order in which the nodes are visited during a tree traversal can be easily determined by imagining there is a “flag” attached to each node, as follows: </li></ul><ul><li>To traverse the tree, collect the flags: </li></ul>preorder inorder postorder A B C D E F G A B C D E F G A B C D E F G A B D E C F G D B E A F C G D E B F G C A
  12. 12. Copying a binary tree <ul><li>In postorder , the root is visited last </li></ul><ul><li>Here’s a postorder traversal to make a complete copy of a given binary tree: </li></ul><ul><li>public BinaryTree copyTree(BinaryTree bt) { if (bt == null) return null; BinaryTree left = copyTree(bt.leftChild); BinaryTree right = copyTree(bt.rightChild); return new BinaryTree(bt.value, left, right); } </li></ul>
  13. 13. Other traversals <ul><li>The other traversals are the reverse of these three standard ones </li></ul><ul><ul><li>That is, the right subtree is traversed before the left subtree is traversed </li></ul></ul><ul><li>Reverse preorder: root, right subtree, left subtree </li></ul><ul><li>Reverse inorder: right subtree, root, left subtree </li></ul><ul><li>Reverse postorder: right subtree, left subtree, root </li></ul>
  14. 14. Tree searches <ul><li>A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions, depending on the problem) </li></ul><ul><li>For some problems, any goal node is acceptable ( N or J ); for other problems, you want a minimum-depth goal node, that is, a goal node nearest the root (only J ) </li></ul>L M N O P G Q H J I K F E D B C A Goal nodes
  15. 15. Depth-first searching <ul><li>A depth-first search ( DFS ) explores a path all the way to a leaf before backtracking and exploring another path </li></ul><ul><li>For example, after searching A , then B , then D , the search backtracks and tries another path from B </li></ul><ul><li>Node are explored in the order A B D E H L M N I O P C F G J K Q </li></ul><ul><li>N will be found before J </li></ul>L M N O P G Q H J I K F E D B C A
  16. 16. How to do depth-first searching <ul><li>Put the root node on a stack; while (stack is not empty) { remove a node from the stack; if (node is a goal node) return success; put all children of node onto the stack; } return failure; </li></ul><ul><li>At each step, the stack contains some nodes from each of a number of levels </li></ul><ul><ul><li>The size of stack that is required depends on the branching factor b </li></ul></ul><ul><ul><li>While searching level n , the stack contains approximately b*n nodes </li></ul></ul><ul><li>When this method succeeds, it doesn’t give the path </li></ul>
  17. 17. Depth-First Search A B C D E F G A B D E C F G Stack: Current:
  18. 18. Depth-First Search A B C D E F G Undiscovered Marked Finished Active B newly discovered Stack: (A, C); (A, B) Current:
  19. 19. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active A already marked Visited B Stack: (B, E); (B, D); (B, A) (A, C); (A, B)
  20. 20. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active A already marked Visited B A Stack: (B, E); (B, D); (B, A) (A, C); (A, B)
  21. 21. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active D newly discovered A Stack: (B, E); (B, D); (B, A) (A, C); (A, B)
  22. 22. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Visited D A B Stack: (B, E); (B, D); (B, A) (A, C); (A, B) (B, D) Marked B
  23. 23. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Finished D A B Stack: (B, E); (B, D); (B, A) (A, C); (A, B)
  24. 24. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active E newly discovered Stack: (B, E); (B, D); (B, A) (A, C); (A, B) A B D Backtrack B
  25. 25. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Visited E Stack: (B, E); (B, D); (B, A) (A, C); (A, B) A B D (B, E)
  26. 26. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Finished E Stack: (B, E); (B, D); (B, A) (A, C); (A, B) A B D
  27. 27. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: (B, E); (B, D); (B, A) (A, C); (A, B) A B D E
  28. 28. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: (A, C); (A, B) A B D E Backtrack B
  29. 29. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: (A, C); (A, B) A B D E Backtrack A Finished B C newly discovered
  30. 30. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: (C, G); (C, F); (C, A) A B D E Visited C (A, C); (A, B)
  31. 31. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: (C, G); (C, F); (C, A) Marked C (A, C); (A, B) F newly discovered A B D E C
  32. 32. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: (C, G); (C, F); (C, A) (A, C); (A, B) Visited F A B D E C (F, C)
  33. 33. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: (C, G); (C, F); (C, A) (A, C); (A, B) Finished F A B D E C
  34. 34. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: (C, G); (C, F); (C, A) (A, C); (A, B) Backtrack C G newly discovered A B D E C F
  35. 35. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: (C, G); (C, F); (C, A) (A, C); (A, B) Marked C Visited G A B D E C F (G, C)
  36. 36. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: (C, G); (C, F); (C, A) (A, C); (A, B) Backtrack C Finished G A B D E C F G
  37. 37. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: (A, C); (A, B) Finished C A B D E C F G Backtrack A
  38. 38. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active Stack: A B D E C F G Finished A
  39. 39. Depth-First Search A B C D E F G Current: Undiscovered Marked Finished Active A B D E C F G
  40. 40. Breadth-first searching <ul><li>A breadth-first search ( BFS ) explores nodes nearest the root before exploring nodes further away </li></ul><ul><li>For example, after searching A , then B , then C , the search proceeds with D , E , F , G </li></ul><ul><li>Node are explored in the order A B C D E F G H I J K L M N O P Q </li></ul><ul><li>J will be found before N </li></ul>L M N O P G Q H J I K F E D B C A
  41. 41. How to do breadth-first searching <ul><li>Put the root node on a queue; while (queue is not empty) { remove a node from the queue; if (node is a goal node) return success; put all children of node onto the queue; } return failure; </li></ul><ul><li>Just before starting to explore level n , the queue holds all the nodes at level n-1 </li></ul><ul><li>In a typical tree, the number of nodes at each level increases exponentially with the depth </li></ul><ul><li>Memory requirements may be infeasible </li></ul><ul><li>When this method succeeds, it doesn’t give the path </li></ul><ul><li>There is no “recursive” breadth-first search equivalent to recursive depth-first search </li></ul>
  42. 42. Breadth-First Search A B C D E F G A B C D E F G Queue: Current:
  43. 43. Breadth-First Search A B C D E F G Queue: Current: A
  44. 44. Breadth-First Search A B C D E F G Queue: Current: A A
  45. 45. Breadth-First Search A B C D E F G Queue: Current: A A
  46. 46. Breadth-First Search A B C D E F G Queue: Current: B A A
  47. 47. Breadth-First Search A B C D E F G Queue: Current: C B A A
  48. 48. Breadth-First Search A B C D E F G Queue: Current: B A C B
  49. 49. Breadth-First Search A B C D E F G Queue: Current: B C A B
  50. 50. Breadth-First Search A B C D E F G Queue: Current: D C B A B
  51. 51. Breadth-First Search A B C D E F G Queue: Current: E D C B A B
  52. 52. Breadth-First Search A B C D E F G Queue: Current: C E D C A B
  53. 53. Breadth-First Search A B C D E F G Queue: Current: C A B C E D
  54. 54. Breadth-First Search A B C D E F G Queue: Current: C A B C F E D
  55. 55. Breadth-First Search A B C D E F G Queue: Current: C A B C G F E D
  56. 56. Breadth-First Search A B C D E F G Queue: Current: D A B C G F E D
  57. 57. Breadth-First Search A B C D A B C D E F G Queue: Current: D G F E
  58. 58. Breadth-First Search A B C D E F G A B C D Queue: Current: E G F E
  59. 59. Breadth-First Search A B C D E F G Queue: Current: F G A B C D E F
  60. 60. Breadth-First Search A B C D E F G Queue: Current: G G A B C D E F
  61. 61. Breadth-First Search A B C D E F G Queue: Current: G A B C D E F G
  62. 62. Breadth-First Search A B C D E F G A B C D E F G
  63. 63. Depth- vs. breadth-first searching <ul><li>When a breadth-first search succeeds, it finds a minimum-depth (nearest the root) goal node </li></ul><ul><li>When a depth-first search succeeds, the found goal node is not necessarily minimum depth </li></ul><ul><li>For a large tree, breadth-first search memory requirements may be excessive </li></ul><ul><li>For a large tree, a depth-first search may take an excessively long time to find even a very nearby goal node </li></ul><ul><li>How can we combine the advantages (and avoid the disadvantages) of these two search techniques? </li></ul>
  64. 64. findMin/ findMax <ul><li>Return the node containing the smallest element in the tree </li></ul><ul><li>Start at the root and go left as long as there is a left child. The stopping point is the smallest element </li></ul><ul><li>Similarly for findMax </li></ul><ul><li>Time complexity = O(height of the tree) </li></ul>
  65. 65. insert <ul><li>Proceed down the tree as you would with a find </li></ul><ul><li>If X is found, do nothing (or update something) </li></ul><ul><li>Otherwise, insert X at the last spot on the path traversed </li></ul><ul><li>Time complexity = O(height of the tree) </li></ul>
  66. 66. delete <ul><li>When we delete a node, we need to consider how we take care of the children of the deleted node. </li></ul><ul><ul><li>This has to be done such that the property of the search tree is maintained. </li></ul></ul>
  67. 67. Delete cont… <ul><li>Three cases: </li></ul><ul><li>(1) the node is a leaf </li></ul><ul><ul><li>Delete it immediately </li></ul></ul><ul><li>(2) the node has one child </li></ul><ul><ul><li>Adjust a pointer from the parent to bypass that node </li></ul></ul>
  68. 68. Delete cont… <ul><li>(3) the node has 2 children </li></ul><ul><ul><li>replace the key of that node with the minimum element at the right subtree </li></ul></ul><ul><ul><li>delete the minimum element </li></ul></ul><ul><ul><ul><li>Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2. </li></ul></ul></ul><ul><li>Time complexity = O(height of the tree) </li></ul>
  69. 69. Thank you for listening!

×