1 / 37

Binary Search Trees (aka BSTs)

Binary Search Trees (aka BSTs). Just another data structure Dynamic Non-linear Left pointer Right pointer Still have access only to the headNode Is amazingly fast**!. ** When balanced. 51. 23. 72. 89. 61. 44. 17. 51. 23. 72. 89. 61. 44. 17.

quade
Télécharger la présentation

Binary Search Trees (aka BSTs)

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. Binary Search Trees(aka BSTs) • Just another data structure • Dynamic • Non-linear • Left pointer • Right pointer • Still have access only to the headNode • Is amazingly fast**! ** When balanced

  2. 51 23 72 89 61 44 17

  3. 51 23 72 89 61 44 17 Left “child” is less than “parent”

  4. 51 23 72 89 61 44 17 Right “child” is >= than “parent”

  5. 51 23 72 89 61 44 17 This property is recursively true for both sides!

  6. 51 23 72 89 61 44 17 This property is recursively true for both sides!

  7. Why is this SO AMAZING? • Searching • If value of data < this node, go left • If value of data >= this node, go right • Each pass cuts the data in half! • Logarithmic performancefor: • Searching • Inserting new data • Updating data

  8. Logarithms(specifically log2n) How many times can you chop it in half?

  9. Logarithms(specifically log2n) 16

  10. Logarithms(specifically log2n) 1 chop 16 1 chop 8 8

  11. Logarithms(specifically log2n) 2 chops 16 1 chop 8 8 1 chop 4 4 4 4

  12. Logarithms(specifically log2n) 3 chops 16 1 chop 8 8 1 chop 4 4 4 4 1 chop 2 2 2 2 2 2 2 2

  13. Logarithms(specifically log2n) 4 chops 16 1 chop 8 8 1 chop 4 4 4 4 1 chop 2 2 2 2 2 2 2 2 1 chop 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

  14. Logarithms(specifically log2n) log216 = 4 16 1 chop 8 8 1 chop 4 4 4 4 1 chop 2 2 2 2 2 2 2 2 1 chop 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

  15. 51 23 72 89 61 44 17 Looking for the number 61

  16. 51 23 72 89 61 44 17 Is 61 >= 51?

  17. 51 23 72 89 61 44 17 Is 61 >= 51? Yes! Go right!

  18. 51 23 72 89 61 44 17 Is 61 >= 51? Yes! Go right!

  19. 51 23 72 89 61 44 17 Is 61 >= 72?

  20. 51 23 72 89 61 44 17 Is 61 >= 72? No! Go left!

  21. 51 23 72 89 61 44 17 Is 61 >= 72? No! Go left!

  22. 51 23 72 89 61 44 17 We have 7 nodes log27 = 2(-ish)

  23. 51 23 72 89 61 44 17 What if we had 4G nodes?

  24. 51 23 72 89 61 44 17 What if we had 4G nodes? 4,000,000,000,000

  25. 51 23 72 89 61 44 17 log24G = 32

  26. Where Performance Degrades

  27. Where Performance Degrades 11

  28. Where Performance Degrades 11 12

  29. Where Performance Degrades 11 12

  30. Where Performance Degrades 11 12 13

  31. Where Performance Degrades 11 12 13 15

  32. Where Performance Degrades 11 12 13 15 Linked List

  33. Code class LL_Node { int data; Node next;}

  34. BST Code class LL_Node { int data; Node next;} class BST_Node { int data; Node left; Node right;}

  35. BST Code public void traverse () { if (left != null) { left.traverse(); } Console.WriteLine(this.data); if (right != null) { right.traverse(); } }

  36. BST Code public void traverse () { if (left != null) { left.traverse(); } Console.WriteLine(this.data); if (right != null) { right.traverse(); } } Trace this. It’s worth it!

  37. Summary • YADS (yet another data structure) • Dynamic • Non-linear • Left pointer • Right pointer • Still have access only to the headNode • Is amazingly fast**! ** When balanced

More Related