binary search trees - stanford university · binary search tree designed to keep the height of the...
TRANSCRIPT
![Page 1: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/1.jpg)
Binary Search Trees
![Page 2: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/2.jpg)
Friday Four Square!4:15PM, Outside Gates
![Page 3: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/3.jpg)
Implementing Set
● On Monday and Wednesday, we saw how to implement the Map and Lexicon, respectively.
● Let's now turn our attention to the Set.
● Major operations:● Insert● Remove● Contains
![Page 4: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/4.jpg)
An Inefficient Implementation
● We could implement the Set as a list of all the values it contains.
● To add an element:
● Check if the element already exists.
● If not, append it.
● To remove an element:
● Find and remove it from the list.
● To see if an element exists:
● Search the list for the element.
![Page 5: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/5.jpg)
Using Hashing
● If we have a hash function for the elements being stored, we can implement a Set using a hash table.
● What is the expected time to insert a value?
● Answer: O(1).
● What is the expected time to remove a value?
● Answer: O(1).
● What is the expected time to check if a value exists?
● Answer: O(1).
● However, writing a good hash function for a set of elements can be very tricky!
![Page 6: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/6.jpg)
Using Tries
● If our keys are strings, we can store the set using a trie.
● Looking up or inserting a string with L letters takes time O(L).
● Doesn't work for arbitrary values.
![Page 7: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/7.jpg)
An Entirely Different Approach
![Page 8: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/8.jpg)
2
-1
4
3
0
-2
6
![Page 9: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/9.jpg)
2
-1
4
3
0
-2
6
![Page 10: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/10.jpg)
2
-1
4
3
0
-2
6
![Page 11: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/11.jpg)
2
-1
4
3
0
-2
6
![Page 12: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/12.jpg)
2
-1
40
-2
6
3
![Page 13: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/13.jpg)
2
-1
40
-2
6
3
![Page 14: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/14.jpg)
2
-1
40
-2
6
3
![Page 15: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/15.jpg)
2
-1
40
-2
6
3
![Page 16: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/16.jpg)
2
40
-2
6
-1
3
![Page 17: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/17.jpg)
2
40-2 6
-1
3
![Page 18: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/18.jpg)
2
40-2 6
-1
3
![Page 19: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/19.jpg)
2
40-2 6
-1
3
![Page 20: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/20.jpg)
2
40-2 6
-1
3
![Page 21: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/21.jpg)
2
4
3
0-2 6
-1
![Page 22: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/22.jpg)
2
3
0-2 6
-1 4
![Page 23: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/23.jpg)
2
30-2 6
-1 4
![Page 24: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/24.jpg)
2
30-2 6
-1 4
![Page 25: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/25.jpg)
2
30-2 6
-1 4
![Page 26: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/26.jpg)
22
30-2 6
-1 4
30-2 6
-1 4
![Page 27: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/27.jpg)
4
22
30-2 6
-1 4
30-2 6
-1
![Page 28: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/28.jpg)
4
3
4
22
30-2 6
-1
0-2 6
-1
![Page 29: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/29.jpg)
3
4
3
4
22
0-2 6
-1
0-2 6
-1
![Page 30: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/30.jpg)
2
3
4
3
4
2
0-2 6
-1
0-2 6
-1
![Page 31: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/31.jpg)
2
-1
2
3
4
3
4
0-2 6
-1
0-2 6
![Page 32: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/32.jpg)
-1
-2
2
-1
2
3
4
3
4
0-2 60 6
![Page 33: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/33.jpg)
-2
-1
-2
2
-1
2
3
4
3
4
0 60 6
![Page 34: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/34.jpg)
Binary Search Trees
● The data structure we have just seen is called a binary search tree (or BST).
● Uses comparisons between elements to store elements efficiently.
● No need for a complex hash function, or the ability to work one symbol at a time.
![Page 35: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/35.jpg)
The Intuition
![Page 36: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/36.jpg)
The Intuition
2
-1 4
-2 0 63
![Page 37: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/37.jpg)
The Intuition
2
-1 4
-2 0 63
0
![Page 38: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/38.jpg)
The Intuition
2
-1 4
-2 0 63
0
![Page 39: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/39.jpg)
The Intuition
2
-1 4
-2 0 63
0
![Page 40: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/40.jpg)
The Intuition
2
-1 4
-2 0 63
0
Values less than two Values greater than two
![Page 41: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/41.jpg)
The Intuition
2
-1 4
-2 0 63
0
Values less than two Values greater than two
![Page 42: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/42.jpg)
The Intuition
2
-1 4
-2 0 63
0
![Page 43: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/43.jpg)
The Intuition
2
-1 4
-2 0 63
0
Values lessthan -1
Values greaterthan -1
![Page 44: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/44.jpg)
The Intuition
2
-1 4
-2 0 63
0
Values lessthan -1
Values greaterthan -1
![Page 45: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/45.jpg)
The Intuition
2
-1 4
-2 0 63
0
![Page 46: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/46.jpg)
Tree Terminology
● As with a trie, a BST is a collection of nodes.
● The top node is called the root node.● Nodes with no children are called
leaves.2
-1 4
-2 0 63
![Page 47: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/47.jpg)
A Recursive View of BSTs
2
-1 4
-2 0 63
![Page 48: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/48.jpg)
A Recursive View of BSTs
2
-1 4
-2 0 63
![Page 49: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/49.jpg)
A Recursive View of BSTs
![Page 50: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/50.jpg)
Implementing Lookups
![Page 51: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/51.jpg)
Inserting into a BST
2
30-2 6
-1 4
![Page 52: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/52.jpg)
2
30-2 6
-1 4
Inserting into a BST
2
30-2 6
-1 4
1
![Page 53: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/53.jpg)
22
30-2 6
-1 4
Inserting into a BST
30-2 6
-1 4
1
![Page 54: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/54.jpg)
2
-1
2
30-2 6
-1 4
Inserting into a BST
30-2 6
4
1
![Page 55: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/55.jpg)
-1
0
2
-1
2
30-2 6
4
Inserting into a BST
3-2 6
4
1
![Page 56: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/56.jpg)
0
-1
0
2
-1
2
3-2 6
4
Inserting into a BST
3-2 6
4
1
![Page 57: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/57.jpg)
Let's Code it Up!
![Page 58: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/58.jpg)
Insertion Order Matters
● Suppose we create a BST of numbers in this order:
4, 2, 1, 3, 6, 5, 7
4
2
1 3
6
5 7
![Page 59: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/59.jpg)
Insertion Order Matters
● Suppose we create a BST of numbers in this order:
1, 2, 3, 4, 5, 6, 7
12
34
56
7
![Page 60: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/60.jpg)
Tree Terminology
● The height of a tree is the number of nodes in the longest path from the root to a leaf.
4
2
1 3
6
5 7
![Page 61: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/61.jpg)
Tree Terminology
● The height of a tree is the number of nodes in the longest path from the root to a leaf.
12
34
56
7
![Page 62: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/62.jpg)
Efficiency of Insertion
● What is the big-O complexity of adding a node to a tree?
● Depends on the height of a tree!● Worst-case: have to take the longest path
down to find where the node goes.● Time is O(h), where h is the height of the
tree.
![Page 63: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/63.jpg)
Tree Heights
● What are the maximum and minimum heights of a tree with n nodes?
● Maximum height: all nodes in a chain. Height is O(n).
● Minimum height: Tree is as complete as possible. Height is O(log n).
4
2
1 3
6
5 7
![Page 64: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/64.jpg)
Tree Heights
● What are the maximum and minimum heights of a tree with n nodes?
● Maximum height: all nodes in a chain. Height is O(n).
● Minimum height: Tree is as complete as possible. Height is O(log n).
4
2
1 3
6
5 7
![Page 65: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/65.jpg)
Keeping the Height Low
● There are many modifications of the binary search tree designed to keep the height of the tree low (usually O(log n)).
● A self-balancing binary search tree is a binary search tree that automatically adjusts itself to keep the height low.
● Details next time.
![Page 66: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/66.jpg)
Walking Trees
http://blog.pro-ecuador.com/wp-content/uploads/2010/03/floating-tree.jpghttp://media.tumblr.com/tumblr_lp2fs6CgA11qejdya.jpg
![Page 67: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/67.jpg)
Walking a BST
● One advantage of a BST is that elements are stored in sorted order.
● We can iterate over the elements of a BST in sorted order by walking the tree recursively.
4
2
1 3
6
5 7
![Page 68: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/68.jpg)
Walking a BST
● One advantage of a BST is that elements are stored in sorted order.
● We can iterate over the elements of a BST in sorted order by walking the tree recursively.
4
2
1 3
6
5 7
![Page 69: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/69.jpg)
Walking a BST
● One advantage of a BST is that elements are stored in sorted order.
● We can iterate over the elements of a BST in sorted order by walking the tree recursively.
4
2
1 3
6
5 7
![Page 70: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/70.jpg)
Walking a BST
● One advantage of a BST is that elements are stored in sorted order.
● We can iterate over the elements of a BST in sorted order by walking the tree recursively.
4
2
1 3
6
5 7
1 2 3
![Page 71: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/71.jpg)
Walking a BST
● One advantage of a BST is that elements are stored in sorted order.
● We can iterate over the elements of a BST in sorted order by walking the tree recursively.
4
2
1 3
6
5 7
1 2 3
![Page 72: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/72.jpg)
Walking a BST
● One advantage of a BST is that elements are stored in sorted order.
● We can iterate over the elements of a BST in sorted order by walking the tree recursively.
4
2
1 3
6
5 7
1 2 3 4
![Page 73: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/73.jpg)
Walking a BST
● One advantage of a BST is that elements are stored in sorted order.
● We can iterate over the elements of a BST in sorted order by walking the tree recursively.
4
2
1 3
6
5 7
1 2 3 4
![Page 74: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/74.jpg)
Walking a BST
● One advantage of a BST is that elements are stored in sorted order.
● We can iterate over the elements of a BST in sorted order by walking the tree recursively.
4
2
1 3
6
5 7
1 2 3 4 5 6 7
![Page 75: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/75.jpg)
Tree Traversals
● There are three general types of tree traversals:
● Preorder: Visit the node, then visit the children.
● Inorder: Visit the left child, then the node, then the right child.
● Postorder: Visit the children, then visit the node.
![Page 76: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/76.jpg)
Walking a Tree
4
2
1 3
6
5 7
1 2 3 4 5 6 7
4 2 1 3 6 5 7
1 3 2 5 7 6 4
Inorder
Preorder
Postorder
![Page 77: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/77.jpg)
Getting Rid of Trees
http://www.tigersheds.com/garden-resources/image.axd?picture=2010%2F6%2Fdeforestation1.jpg
![Page 78: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/78.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
2
1 3
6
5 7
![Page 79: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/79.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
2
1 3
6
5 7
![Page 80: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/80.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
2
1 3
6
5 7
![Page 81: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/81.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
2
1 3
6
5 7
![Page 82: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/82.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
2
1 3
6
5 7
![Page 83: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/83.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
2
3
6
5 7
![Page 84: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/84.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
2
3
6
5 7
![Page 85: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/85.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
2
3
6
5 7
![Page 86: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/86.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
2 6
5 7
![Page 87: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/87.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
2 6
5 7
![Page 88: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/88.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
6
5 7
![Page 89: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/89.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
6
5 7
![Page 90: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/90.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
6
5 7
![Page 91: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/91.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
6
5 7
![Page 92: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/92.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
6
7
![Page 93: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/93.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
6
7
![Page 94: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/94.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
6
7
![Page 95: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/95.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
6
![Page 96: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/96.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
6
![Page 97: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/97.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
![Page 98: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/98.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
4
![Page 99: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/99.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
![Page 100: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/100.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
![Page 101: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/101.jpg)
Freeing a Tree
● Once we're done with a tree, we need to free all of its nodes.
● As with a linked list, we have to be careful not to use any nodes after freeing them.
● This is done as follows:
● Base case: There is nothing to delete in an empty tree.
● Recursive step: Delete both subtrees, then delete the current node.
● What kind of tree traversal is this?
![Page 102: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/102.jpg)
Range Searches
● We can use BSTs to do range searches, in which we find all values in the BST within some range.
● For example:● If values in a BST are dates, can find all
events that occurred within some time window.
● If values in a BST are samples of a random variable, can find everything within one and two standard deviations above the mean.
![Page 103: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/103.jpg)
The Intuition
2
-1 4
-2 0 63
![Page 104: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/104.jpg)
The Intuition
2
-1 4
-2 0 63
-1 +5
![Page 105: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/105.jpg)
The Intuition
2
-1 4
-2 0 63
-1 +5
![Page 106: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/106.jpg)
The Intuition
2
-1 4
-2 0 63
-1 +5
![Page 107: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/107.jpg)
The Intuition
2
-1 4
-2 0 63
-1 +5
![Page 108: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/108.jpg)
The Intuition
2
-1 4
-2 0 63
-1 +5
![Page 109: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/109.jpg)
The Intuition
2
-1 4
-2 0 63
-1 +5
![Page 110: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/110.jpg)
The Intuition
2
-1 4
-2 0 63
-1 +5
![Page 111: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/111.jpg)
The Logic
● Base case:● The empty tree has no nodes within any range.
● Recursive step:● If this node is below the lower bound, recursively
search the right subtree.● If this node is above the upper bound, recursively
search the left subtree.● If this node is within bounds:
– Search the left subtree.
– Add this node to the output.
– Search the right subtree.
![Page 112: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/112.jpg)
Complexity of Range Searches
● How do we get a runtime for a range search?
● Depends on how many nodes we find.
![Page 113: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/113.jpg)
Complexity of Range Searches
● How do we get a runtime for a range search?
● Depends on how many nodes we find.
![Page 114: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/114.jpg)
Complexity of Range Searches
● How do we get a runtime for a range search?
● Depends on how many nodes we find.
![Page 115: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/115.jpg)
Complexity of Range Searches
● How do we get a runtime for a range search?
● Depends on how many nodes we find.
![Page 116: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/116.jpg)
Complexity of Range Searches
● How do we get a runtime for a range search?
● Depends on how many nodes we find.
![Page 117: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/117.jpg)
Complexity of Range Searches
● How do we get a runtime for a range search?
● Depends on how many nodes we find.
![Page 118: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/118.jpg)
Complexity of Range Searches
● How do we get a runtime for a range search?
● Depends on how many nodes we find.
![Page 119: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/119.jpg)
Complexity of Range Searches
● How do we get a runtime for a range search?
● Depends on how many nodes we find.
![Page 120: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/120.jpg)
Complexity of Range Searches
● How do we get a runtime for a range search?
● Depends on how many nodes we find.
![Page 121: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/121.jpg)
Complexity of Range Searches
● How do we get a runtime for a range search?
● Depends on how many nodes we find.
![Page 122: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/122.jpg)
Complexity of Range Searches
● How do we get a runtime for a range search?
● Depends on how many nodes we find.
● If there are k nodes within the range, we do at least O(k) work finding them.
● In addition, we have two “border sets” of nodes that are immediately outside that range. Each set has size O(h), where h is the height of the tree.
● Total work done is O(k + h).
● This is an output-sensitive algorithm.
![Page 123: Binary Search Trees - Stanford University · binary search tree designed to keep the height of the tree low (usually O(log n)). A self-balancing binary search tree is a binary search](https://reader033.vdocuments.mx/reader033/viewer/2022042406/5f2073cab879f20ef34d0e22/html5/thumbnails/123.jpg)
Next Time
● Fun With Data Structures.● Balanced binary search trees.● Ternary search trees.● DAWGs.