binary search trees - stanford university › class › archive › cs › cs106b › cs106b... ·...
TRANSCRIPT
![Page 1: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/1.jpg)
Binary Search TreesPart Two
![Page 2: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/2.jpg)
Recap from Last Time
![Page 3: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/3.jpg)
Binary Search Trees
● A binary search tree (or BST) is a data structure often used to implement maps and sets.
● The tree consists of a number of nodes, each of which stores a value and has zero, one, or two children.
● Key structural property: All values in a node’s left subtree are smaller than the node’s value, and all values in a node’s right subtree are greater than the node’s value.
-2
-1
1
2
3
6
3
4
7
9
0
6
4
865
![Page 4: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/4.jpg)
A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…
x
<x >x
… a single node,whose left subtree
is a BST of smaller values …
… and whose right subtree is a BST of larger values.
![Page 5: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/5.jpg)
Tree Terminology
● The height of a tree is the number of nodes in the longest path from the root to a leaf.
● By convention, an empty tree has height -1.
4
2
1 3
6
5 7
![Page 6: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/6.jpg)
Tree Terminology
12
34
56
7
● The height of a tree is the number of nodes in the longest path from the root to a leaf.
● By convention, an empty tree has height -1.
![Page 7: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/7.jpg)
Efficiency Questions
● In a balanced BST, the cost of doing an insertion or lookup is O(log n).
● Although we didn’t cover this, the cost of a deletion is also O(log n) (play around with this in section!)
● The runtimes of these operations depend on the height of the BST, which we’re going to assume is O(log n) going forward.
![Page 8: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/8.jpg)
New Stuff!
![Page 9: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/9.jpg)
Walking Trees
![Page 10: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/10.jpg)
Printing a Tree
● BSTs store their elements in sorted order.● By visiting the nodes of a BST in the right order, we’ll
get back the nodes in sorted order!● (This is also why iterating over a Map or Set gives you
the keys/elements in sorted order!)
4
2
1 3
6
5 7
![Page 11: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/11.jpg)
A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…
x
<x >x
… a single node,whose left subtree
is a BST of smaller values …
… and whose right subtree is a BST of larger values.
![Page 12: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/12.jpg)
Inorder Traversals
● The particular recursive pattern we just saw is called an inorder traversal of a binary tree.
● Specifically:● Recursively visit all the nodes in the left
subtree.● Visit the node itself.● Recursively visit all the nodes in the right
subtree.
![Page 13: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/13.jpg)
Getting Rid of Trees
http://www.tigersheds.com/garden-resources/image.axd?picture=2010%2F6%2Fdeforestation1.jpg
![Page 14: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/14.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 15: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/15.jpg)
A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…
x
<x >x
… a single node,whose left subtree
is a BST of smaller values …
… and whose right subtree is a BST of larger values.
![Page 16: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/16.jpg)
Postorder Traversals
● The particular recursive pattern we just saw is called a postorder traversal of a binary tree.
● Specifically:● Recursively visit all the nodes in the left
subtree.● Recursively visit all the nodes in the right
subtree.● Visit the node itself.
![Page 17: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/17.jpg)
Time-Out for Announcements!
![Page 18: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/18.jpg)
Assignment 5
● Assignment 5 is due this Friday at the start of class.
● Recommendation: Aim to complete the first three implementations by the end of tonight. Finish the binary heap by Wednesday.
● Questions? Ask your SL, stop by the LaIR, visit office hours, or ask on Piazza!
![Page 19: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/19.jpg)
![Page 20: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/20.jpg)
Back to CS106B!
![Page 21: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/21.jpg)
Has this ever happened to you?
![Page 22: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/22.jpg)
What’s Going On?
● Internally, the Map and Set types are implemented using binary search trees.
● BSTs assume there’s a way to compare elements against one another using the relational operators.
● But you can’t compare two structs using the less-than operator!
● “There’s got to be a better way!”
![Page 23: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/23.jpg)
Defining Comparisons
● Most programming languages provide some mechanism to let you define how to compare two objects.
● C has comparison functions, Java has the Comparator interface, Python has __cmp__, etc.
● In C++, we can use a technique called operator overloading to tell it how to compare objects using the < operator.
![Page 24: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/24.jpg)
bool operator< (const Doctor& lhs, const Doctor& rhs) { /* … */}
Doctor zhivago = /* … */Doctor acula = /* … */
if (zhivago < acula) { /* … */}
![Page 25: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/25.jpg)
bool operator< (const Doctor& lhs, const Doctor& rhs) { /* … */}
Doctor zhivago = /* … */Doctor acula = /* … */
if (zhivago < acula) { /* … */}
![Page 26: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/26.jpg)
bool operator< (const Doctor& lhs, const Doctor& rhs) { /* … */}
Doctor zhivago = /* … */Doctor acula = /* … */
if (zhivago < acula) { /* … */}
This function is named “operator<”This function is named “operator<”
Its arguments correspond to the left-hand and right-hand operands
to the < operator.
Its arguments correspond to the left-hand and right-hand operands
to the < operator.
![Page 27: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/27.jpg)
bool operator< (const Doctor& lhs, const Doctor& rhs) { /* … */}
Doctor zhivago = /* … */Doctor acula = /* … */
if (zhivago < acula) { /* … */}
C++ treats this as
operator< (zhivago, acula)
C++ treats this as
operator< (zhivago, acula)
![Page 28: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/28.jpg)
Overloading Less-Than
● To store custom types in Maps or Sets in C++, overload the less-than operator by defining a function like this one:
bool operator< (const Type& lhs, const Type& rhs);● This function must obey four rules:
● It is consistent: writing x < y always returns the same result given x and y.
● It is irreflexive: x < x is always false.● It is transitive: If x < y and y < z, then x < z.● It has transitivity of incomparability: If neither x < y nor y < x are true, then x and y behave indistinguishably.
● (These rules mean that < is a strict weak order; take CS103 for details!)
![Page 29: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/29.jpg)
Overloading Less-Than
A standard technique for implementing the less-than operator is to use a lexicographical comparison, which looks like this:
bool operator< (const Type& lhs, const Type& rhs) { if (lhs.field1 != rhs.field1) { return lhs.field1 < rhs.field1; } else if (lhs.field2 != rhs.field2) { return lhs.field2 < rhs.field2; } else if (lhs.field3 != rhs.field3) { return lhs.field3 < rhs.field3; } … { … } else { return lhs.fieldN < rhs.fieldN; }}
![Page 30: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/30.jpg)
One Last Cool Trick, If We Have Time
![Page 31: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/31.jpg)
Filtering Trees
![Page 32: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/32.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 the values in the BST are dates, we can
find all events that occurred within some time window.
● If the values in the BST are number of diagnostic scans ordered, we can find all doctors who order a disproportionate number of scans.
![Page 33: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/33.jpg)
A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…
x
<x >x
… a single node,whose left subtree
is a BST of smaller values…
… and whose right subtree is a BST of larger values.
![Page 34: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/34.jpg)
A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…
x
<x >x
… a single node,whose left subtree
is a BST of smaller values…
… and whose right subtree is a BST of larger values.
![Page 35: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/35.jpg)
A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…
x
<x >x
… a single node,whose left subtree
is a BST of smaller values…
… and whose right subtree is a BST of larger values.
![Page 36: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/36.jpg)
A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…
x
<x >x
… a single node,whose left subtree
is a BST of smaller values…
… and whose right subtree is a BST of larger values.
![Page 37: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/37.jpg)
Range Searches
● The cost of a range search in a balanced BST is
O(log n + z),
where z is the number of matches reported.● In a general BST, it’s O(h + z).● Curious about where that analysis comes from?
Come talk to me after class!
![Page 38: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/38.jpg)
To Summarize:
![Page 39: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/39.jpg)
A Binary Search Tree Is Either…an empty tree, represented by nullptr, or…
x
<x >x
… a single node,whose left subtree
is a BST of smaller values …
… and whose right subtree is a BST of larger values.
![Page 40: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/40.jpg)
struct Node { int value; Node* left; // Smaller values Node* right; // Bigger values };
![Page 41: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/41.jpg)
bool contains(Node* root, const string& key) { if (root == nullptr) return false; else if (key == root->value) return true; else if (key < root->value) return contains(root->left, key); else return contains(root->right, key);}
void insert(Node*& root, const string& key) { if (root == nullptr) { root = new Node; node->value = key; node->left = node->right = nullptr; } else if (key < root->value) { insert(root left, key);→ } else if (key > root->value) { insert(root->right, key); } else { // Already here! }}
![Page 42: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/42.jpg)
4
2
1 3
6
5 7
12
34
56
7
![Page 43: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/43.jpg)
void printTree(Node* root) { if (root == nullptr) return;
printTree(root->left); cout << root->value << endl; printTree(root->right);}
void freeTree(Node* root) { if (root == nullptr) return;
freeTree(root->left); freeTree(root->right); delete root;}
![Page 44: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/44.jpg)
bool operator< (const Type& lhs, const Type& rhs) { if (lhs.field1 != rhs.field1) { return lhs.field1 < rhs.field1; } else if (lhs.field2 != rhs.field2) { return lhs.field2 < rhs.field2; } else if (lhs.field3 != rhs.field3) { return lhs.field3 < rhs.field3; } … { … } else { return lhs.fieldN < rhs.fieldN; }}
![Page 45: Binary Search Trees - Stanford University › class › archive › cs › cs106b › cs106b... · 2017-02-27 · Binary Search Trees A binary search tree (or BST) is a data structure](https://reader034.vdocuments.net/reader034/viewer/2022052300/5f11aa31b92d905d933eeb7f/html5/thumbnails/45.jpg)
Next Time
● Beyond Data Structures● Why are these ideas useful outside of the
realm of sets and maps?● Huffman Encoding
● A powerful data compression algorithm.