1 / 68

A Binary Tree

A Binary Tree. root. leaf. leaf. leaf. A Binary Tree. root. descendent of root. descendent of root. parent of leaf. leaf. leaf. leaf. Complete Binary Tree On 12 nodes. Full Binary Tree on 7 nodes. i. Number of nodes at depth i = 2 Maximum depth of a node = floor(log 2 (n)),

Télécharger la présentation

A Binary Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. A Binary Tree root leaf leaf leaf

  2. A Binary Tree root descendent of root descendent of root parent of leaf leaf leaf leaf

  3. Complete Binary Tree On 12 nodes

  4. Full Binary Tree on 7 nodes i Number of nodes at depth i = 2 Maximum depth of a node = floor(log2(n)), a formula which also holds for any complete binary tree

  5. Searching looking for 31 28 33 16 8 18 30 42 6 17 15 31

  6. Searching looking for 31 28 Compare – go right 33 16 8 18 30 42 6 17 15 31

  7. Searching looking for 31 28 33 16 Compare – go left 8 18 30 42 6 17 15 31

  8. Searching looking for 31 28 33 16 8 18 30 42 Compare – go right 6 17 15 31

  9. Searching looking for 31 28 33 16 8 18 30 42 Found it! 6 17 15 31 Complexity O(d), where d is depth of tree

  10. Inserting 22 28 33 16 8 18 30 42 6 31 17 15

  11. Inserting 22 28 compare to 28, move left 33 16 8 18 30 42 6 31 17 15

  12. Inserting 22 28 compare to 16, move right 33 16 8 18 30 42 6 31 17 15

  13. Inserting 22 28 33 16 8 18 30 42 compare to 18, insert to right 6 31 17 15

  14. Inserting 22 28 33 16 8 18 30 42 6 31 17 15 22 done complexity: O(d), where d is depth of tree

  15. Deletions require no list comparisons, only pointer assignments 28 33 16 Delete 15 8 18 30 42 6 15 31 17 Delete leaf nodes by simply cutting them off

  16. 15 Deleted 28 33 16 8 18 30 42 6 31 17

  17. Delete 18 28 To delete a node with only one child, simply “splice” around it. 33 16 8 18 30 42 6 31 17

  18. 18 Deleted 28 To delete a node with only one child, simply “splice” around it. 33 16 8 17 30 42 6 31

  19. To delete a node with two children, replace it with its inorder successor, and then delete the inorder successor (which has at most one child). Delete 28 28 33 16 8 17 30 42 6 31

  20. Replace 28 by inorder successor 30. Delete 28 30 33 16 8 17 30 42 6 31

  21. Now delete original occurrence of 28’s inorder successor 30. Delete inorder successor of 28 30 33 16 8 17 30 42 6 31

  22. Deletion of 28 now complete. 30 33 16 8 17 31 42 6

  23. A (Min) Heap A Complete Binary Tree with nodes containing numbers. Hence, full at every level except (possibly) the deepest. Nodes at deepest level number filled left-to-right. Children (if any) of node at index i have indices 2i + 1and 2i + 2, respectively. Parent of node at index i has index floor((i-1)/2). All descendents of a node contain not smaller numbers. A (Max) Heap is same as above, except “not smaller” is replaced by “not larger.” A (Max) Heap: Same as above, except “not smaller” replaced by “not larger.” A

  24. A (Min) Heap 5 34 12 23 75 35 55 33 54 89

  25. Delete a Node 34 12 23 75 35 55 33 54 89

  26. Delete a Node 89 34 12 23 75 35 55 33 54

  27. Delete a Node 12 34 89 23 75 35 55 33 54

  28. Delete a Node 12 34 23 89 75 35 55 33 54

  29. Delete a Node 12 34 23 33 75 35 55 89 54

  30. Delete a Node 12 34 23 33 75 35 55 89 54 Complexity of delete: O(log n)

  31. Insert a Node 12 34 23 33 75 35 55 89 54 14

  32. Insert a Node 12 34 23 33 14 35 55 89 54 75

  33. Insert a Node 12 34 14 33 23 35 55 89 54 75

  34. Insert a Node 12 34 14 33 23 35 55 89 54 75 Complexity of insert: O(log n)

  35. Build a (min) Heap by inserting the elements 12,8,6,5,4,3,2,1 stored in array A[0:7] as in MakeMinHeap1 12 Complexity: 0

  36. Build by inserting 12 8 Complexity: 0

  37. Build by inserting 8 12 Complexity: 0+1

  38. Build by inserting 8 6 12 Complexity: 0+1

  39. Build by inserting 6 8 12 Complexity: 0+1+1

  40. Build by inserting 6 8 12 5 Complexity: 0+1+1

  41. Build by inserting 6 8 5 12 Complexity: 0+1+1

  42. Build by inserting 5 8 6 12 Complexity: 0+1+1+2

  43. Build by inserting 5 8 6 12 4 Complexity: 0+1+1+2

  44. Build by inserting 5 8 4 12 6 Complexity: 0+1+1+2

  45. Build by inserting 4 8 5 12 6 Complexity: 0+1+1+2+2

  46. Build by inserting 4 8 5 12 6 3 Complexity: 0+1+1+2+2

  47. Build by inserting 4 3 5 12 6 8 Complexity: 0+1+1+2+2

  48. Build by inserting 3 4 5 12 6 8 Complexity: 0+1+1+2+2+2

  49. Build by inserting 3 4 5 12 6 8 2 Complexity: 0+1+1+2+2+2

  50. Build by inserting 3 2 5 12 6 8 4 Complexity: 0+1+1+2+2+2

More Related