1 / 37

Binary Search Trees

Binary Search Trees. Ravi Chugh March 28, 2014. Review: Linked Lists. Goal : Program that keeps track of friends Problem : Arrays have fixed length Solution : Linked Lists. null. Review: Linked Lists. class LinkedList { str friend; LinkedList next; bool find(str s) {

Download Presentation

Binary Search Trees

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 Ravi Chugh March 28, 2014

  2. Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution: Linked Lists null

  3. Review: Linked Lists class LinkedList { str friend; LinkedList next; bool find(str s) { if (s == this.friend) return true else if (this.next == null) return false else return this.next.find(s) } ... }

  4. Questions for Today • How “quickly” does find work? • How can we do better? class LinkedList { str friend; LinkedList next; bool find(str s) { if (s == this.friend) return true else if (this.next == null) return false else return this.next.find(s) } ... }

  5. find(s): How Many Nodes Traversed? BestCase Avg.Case WorstCase bool find(str s) { if (s == this.friend) return true else if (this.next == null) return false else return this.next.find(s) } ?? ?? ??

  6. find(s): How Many Nodes Traversed? BestCase Avg.Case WorstCase bool find(str s) { if (s == this.friend) return true else if (this.next == null) return false else return this.next.find(s) } 1 ≈ size size

  7. Idea: Keep Names Sorted! class LinkedList { str friend; LinkedList next; bool find(str s) { if (s == this.friend) return true else if (this.next == null) return false else return this.next.find(s) } ... }

  8. Idea: Keep Names Sorted! class LinkedList { str friend; LinkedList next; // all names in next // are bigger than friend bool find(str s) { if (s == this.friend) return true else if (this.next == null) return false else return this.next.find(s) } ... }

  9. Idea: Keep Names Sorted! class LinkedList { str friend; LinkedList next; // all names in next // are bigger than friend bool find(str s) { if (s == this.friend) return true else if (this.next == null) return false else if (s < this.friend) return false else return this.next.find(s) } ... }

  10. class LinkedList { str friend; LinkedList next; // all names in next // are bigger than friend basic info aboutdata structureis tracked“inside the PL” • more complexinvariantsare tracked“outside the PL” • comments • test cases • in your head!

  11. find(s): How Many Nodes Traversed? Data Structure BestCase Avg.Case WorstCase Lists 1 ≈ size size

  12. find(s): How Many Nodes Traversed? Data Structure+ Invariants BestCase Avg.Case WorstCase Lists 1 ≈ size size Lists + Sorted ?? ?? ??

  13. find(s): How Many Nodes Traversed? Data Structure+ Invariants BestCase Avg.Case WorstCase Lists 1 ≈ size size Lists + Sorted 1 ≈ size/2 size

  14. Binary Trees Linked Lists node has 0 or 1“next” pointers node has 0, 1, or 2“next” pointers null null null null null null null

  15. Binary Trees node has 0, 1, or 2“next” pointers “root” of the (upside-down) tree “branch” is path from root to leaf null null “leaves” of tree null null null null

  16. Binary Trees node has 0, 1, or 2“next” pointers

  17. class BinaryTree { str friend; BinaryTree left; BinaryTree right; bool find(str s) { ??? ??? ??? } ... }

  18. class BinaryTree { str friend; BinaryTree left; BinaryTree right; bool find(str s) { if (s == this.friend) return true if (this.left == this.right == null) return false if (this.left == null) return this.right.find(s) if (this.right == null) return this.left.find(s) else return this.left.find(s) || this.right.find(s) } ... } BestCase Avg.Case WorstCase ?? ?? ??

  19. find(s): How Many Nodes Traversed? Data Structure+ Invariants BestCase Avg.Case WorstCase Lists 1 ≈ size size Lists + Sorted 1 ≈ size/2 size Trees 1 ≈ size size

  20. class BinaryTree { str friend; BinaryTree left; BinaryTree right;

  21. class BinaryTree { str friend; BinaryTree left; // names smaller than friend BinaryTree right; // names bigger than friend Called aBinary “Search” Tree (BST)

  22. class BinaryTree { str friend; BinaryTree left; // names smaller than friend BinaryTree right; // names bigger than friend bool find(str s) { if (s == this.friend) return true else if (s < this.friend) if (this.left == null) return false else return this.left.find(s) else // if (s > this.friend) if (this.right == null) return false else return this.right.find(s) } ... }

  23. Tree Height “height” = length of longest branch minus one Height = 0 Height = 3 Height = 1

  24. Tree Height “height” = length of longest branch minus one Height = 3

  25. Tree Height “height” = length of longest branch minus one • Can store up to n nodesin a tree of height log2(n) - 1 • This is the upper bound…

  26. Group Exercise Group the following trees accordingto characteristics you think are interesting (a) (b) (c) (d) (e) (f)

  27. (Mostly) Balanced • branches differ by at most 1 (a) (b) (c) (d) (e) (f)

  28. (Mostly) Balanced • branches differ by at most 1 • (Really) Unbalanced • just crooked linked lists! (a) (b) (c) (d) (e) (f)

  29. (Mostly) Balanced • branches differ by at most 1 • Unbalanced • in between two extremes • (Really) Unbalanced • just crooked linked lists! (a) (b) (c) (d) (e) (f)

  30. Many Different BSTs for Same Data G K G E I A C I C K I A K E C A G E

  31. class BinaryTree { str friend; BinaryTree left; // names smaller than friend BinaryTree right; // names bigger than friend bool find(str s) { if (s == this.friend) return true else if (s < this.friend) if (this.left == null) return false else return this.left.find(s) else // if (s > this.friend) if (this.right == null) return false else return this.right.find(s) } BestCase Avg.Case WorstCase ?? ?? ??

  32. find(s): How Many Nodes Traversed? Data Structure+ Invariants BestCase Avg.Case WorstCase Lists 1 ≈ size size Lists + Sorted 1 ≈ size/2 size Trees 1 ≈ size size Trees + Sorted 1 ≈ height height Height(Tree) <= Size(Tree) If we’re lucky: Height << Size If we’re not: Height == Size

  33. find(s): How Many Nodes Traversed? Data Structure+ Invariants BestCase Avg.Case WorstCase Lists 1 ≈ size size Lists + Sorted 1 ≈ size/2 size Trees 1 ≈ size size Trees + Sorted 1 ≈ height height Trees + SortedTrees + Balanced 1 ≈ height height Height(Balanced Tree) << Size(Balanced Tree) !

  34. Recap: Introduction to Binary Trees Binary Trees • similar to linked lists but with two next pointers Binary Search Trees • smaller elements in left subtree • bigger elements in right subtree Balanced Binary Search Trees • height of tree is much smaller than size • average time to find is much faster than linked lists We will learn how to talk about “efficiency” with mathematical rigor in CS 35 / CS 41

  35. Recap: Introduction to Binary Trees Binary Trees • similar to linked lists but with two “next” pointers Binary Search Trees • smaller elements in “left” subtree • bigger elements in “right” subtree Balanced Binary Search Trees • combiningdata structure and invariantsis the art of programming • in CS 37 / CS 91, we’ll see how PLscan help blend this spectrum!

  36. Food for Thought • How to store data other than strings in a BST? • How to define insert/remove for BSTswhile maintaining balance? • Relationship between binary search trees and the binary search algorithm?

More Related