Binary Search Trees Ravi Chugh March 28, 2014 1. Review: Linked Lists Goal: Program that keeps track...
-
Upload
koby-heller -
Category
Documents
-
view
214 -
download
0
Transcript of Binary Search Trees Ravi Chugh March 28, 2014 1. Review: Linked Lists Goal: Program that keeps track...
![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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/1.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/2.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/3.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/4.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/5.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/6.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/7.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/8.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/9.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/10.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/11.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/12.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/13.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/14.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/15.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/16.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/17.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/18.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/19.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/20.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/21.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/22.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/23.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/24.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/25.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/26.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/27.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/28.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/29.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/30.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/31.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/32.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/33.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/34.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/35.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/36.jpg)
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:](https://reader034.fdocuments.us/reader034/viewer/2022052618/551c0b17550346a84f8b510a/html5/thumbnails/37.jpg)
37