binary search trees ravi chugh march 28, 2014 1. review: linked lists goal: program that keeps track...

37
Binary Search Trees Ravi Chugh March 28, 2014 1

Upload: koby-heller

Post on 01-Apr-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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:

1

Binary Search Trees

Ravi ChughMarch 28, 2014

Page 2: 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:

2

Review: Linked Lists

Goal: Program that keeps track of friends

Problem: Arrays have fixed length

Solution: Linked Lists

null

Page 3: 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:

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) }

... }

Page 4: 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:

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?

Page 5: 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:

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) }

Page 6: 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:

6

find(s): How Many Nodes Traversed?

BestCase

Avg.Case

WorstCase

1 ≈ size size

bool find(str s) { if (s == this.friend) return true else if (this.next == null) return false else return this.next.find(s) }

Page 7: 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:

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!

Page 8: 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:

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!

Page 9: 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:

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!

Page 10: 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:

10

class LinkedList { str friend; LinkedList next; // all names in next // are bigger than friend

basic info aboutdata structure

is tracked“inside the PL”

more complexinvariantsare tracked“outside the PL”

o commentso test caseso in your head!

Page 11: 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:

11

Lists

Data Structure

BestCase

Avg.Case

WorstCase

1

find(s): How Many Nodes Traversed?

≈ size size

Page 12: 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:

12

Lists

Lists + Sorted

Data Structure+ Invariants

BestCase

Avg.Case

WorstCase

1

find(s): How Many Nodes Traversed?

≈ size size

?? ?? ??

Page 13: 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:

13

Lists

Lists + Sorted

Data Structure+ Invariants

BestCase

Avg.Case

WorstCase

1

find(s): How Many Nodes Traversed?

≈ size size

1 ≈ size/2 size

Page 14: 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:

14

Binary TreesLinked Lists

null

node has 0 or 1“next” pointers

node has 0, 1, or 2“next” pointers

null

null null null null

null

Page 15: 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:

15

null

null null null null

null

Binary Treesnode has 0, 1, or 2

“next” pointers

“root” of the (upside-down) tree

“leaves” of tree

“branch” is path from root to leaf

Page 16: 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:

16

Binary Treesnode has 0, 1, or 2

“next” pointers

Page 17: 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:

17

class BinaryTree {

str friend;

BinaryTree left;

BinaryTree right;

bool find(str s) {

???

???

???

}

...

}

Page 18: 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:

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

?? ?? ??

Page 19: 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:

19

Lists

Lists + Sorted

Trees

Data Structure+ Invariants

BestCase

Avg.Case

WorstCase

1

1

find(s): How Many Nodes Traversed?

≈ size

≈ size/2

size

size

1 ≈ size size

Page 20: 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:

20

class BinaryTree {

str friend;

BinaryTree left;

BinaryTree right;

Page 21: 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:

21

class BinaryTree {

str friend;

BinaryTree left; // names smaller than friend

BinaryTree right; // names bigger than friend

Called aBinary “Search” Tree (BST)

Page 22: 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:

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) }

...

}

Page 23: 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:

23

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

Height = 3

Height = 0

Height = 1

Page 24: 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:

24

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

Height = 3

height max size0 11 32 73 154 315 636 127… …h 2(h+1) - 1

Page 25: 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:

25

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

height max size0 11 32 73 154 315 636 127… …h 2(h+1) - 1

• Can store up to n nodesin a tree of height log2(n) - 1

• This is the upper bound…

Page 26: 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:

26

(b)(a)

Group ExerciseGroup the following trees according

to characteristics you think are interesting(c)

(d) (e) (f)

Page 27: 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:

27

(b)(a) (c)

(d) (e) (f)

(Mostly) Balanced branches differ by at most 1

Page 28: 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:

28

(b)(a) (c)

(d) (e) (f)

(Mostly) Balanced branches differ by at most 1

(Really) Unbalanced just crooked linked lists!

Page 29: 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:

29

(b)(a) (c)

(d) (e) (f)

(Mostly) Balanced

(Really) Unbalanced

branches differ by at most 1

just crooked linked lists!

Unbalanced in between two extremes

Page 30: 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:

30

Many Different BSTs for Same Data

G

IE

KC

A

K

A

I

C

G

E

G

IC

KA E

Page 31: 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:

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

?? ?? ??

Page 32: 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:

32

Lists

Lists + Sorted

Trees

Trees + Sorted

Data Structure+ Invariants

BestCase

Avg.Case

WorstCase

1

1

1

find(s): How Many Nodes Traversed?

≈ size

≈ size/2

≈ size

size

size

size

1 ≈ height heightHeight(Tree) <= Size(Tree)If we’re lucky:

Height << SizeIf we’re not:

Height == Size

Page 33: 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:

33

Lists

Lists + Sorted

Trees

Trees + Sorted

Trees + SortedTrees + Balanced

Data Structure+ Invariants

BestCase

Avg.Case

WorstCase

1

1

1

1

find(s): How Many Nodes Traversed?

≈ size

≈ size/2

≈ size

≈ height

size

size

size

height

1 ≈ height height

Height(Balanced Tree) << Size(Balanced Tree) !

Page 34: 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:

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

Page 35: 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:

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

– combining data structure and invariantsis the art of programming

– in CS 37 / CS 91, we’ll see how PLscan help blend this spectrum!

Page 36: 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:

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?

Page 37: 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:

37