Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Search Trees Ravi Chugh March 28, 2014 1. Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution:

Similar presentations


Presentation on theme: "Binary Search Trees Ravi Chugh March 28, 2014 1. Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution:"— Presentation transcript:

1 Binary Search Trees Ravi Chugh March 28,

2 Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution: Linked Lists 2 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) }... } 3

4 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) }... } Questions for Today How “quickly” does find work? How can we do better? 4

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

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

7 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) }... } Idea: Keep Names Sorted! 7

8 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) }... } Idea: Keep Names Sorted! 8

9 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) }... } Idea: Keep Names Sorted! 9

10 class LinkedList { str friend; LinkedList next; // all names in next // are bigger than friend basic info about data structure is tracked “inside the PL” more complex invariants are tracked “outside the PL” o comments o test cases o in your head! 10

11 Lists Data Structure Best Case Avg. Case Worst Case 1 find(s) : How Many Nodes Traversed? ≈ sizesize 11

12 Lists Lists + Sorted Data Structure + Invariants Best Case Avg. Case Worst Case 1 find(s) : How Many Nodes Traversed? ≈ sizesize ?? 12

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

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

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

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

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

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) }... } 18 Best Case Avg. Case Worst Case ??

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

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

21 class BinaryTree { str friend; BinaryTree left; // names smaller than friend BinaryTree right; // names bigger than friend 21 Called a Binary “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) }... } 22

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

24 Tree Height 24 “height” = length of longest branch minus one Height = 3 heightmax size …… h2 (h+1) - 1

25 Tree Height 25 “height” = length of longest branch minus one heightmax size …… h2 (h+1) - 1 Can store up to n nodes in a tree of height log 2 (n) - 1 This is the upper bound…

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

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

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

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

30 Many Different BSTs for Same Data 30 G IE KC A K A I C G E G IC KAE

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) } 31 Best Case Avg. Case Worst Case ??

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

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

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 34

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 35 – combining data structure and invariants is the art of programming – in CS 37 / CS 91, we’ll see how PLs can help blend this spectrum!

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

37 37


Download ppt "Binary Search Trees Ravi Chugh March 28, 2014 1. Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution:"

Similar presentations


Ads by Google