Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed...
Transcript of Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed...
![Page 1: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/1.jpg)
Binary Search TreesSection 6.8
1
![Page 2: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/2.jpg)
107 - Trees
Trees are efficient
• Many algorithms can be performed on trees in O(log n) time.
• Searching for elements using a binary search can work on a tree if the elements are ordered in the obvious way.
• Adding and removing elements is a little trickier.
2
![Page 3: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/3.jpg)
107 - Trees
The Binary Search Tree property
• All values in the nodes in the left subtree of a node are less than the value in the node.
• All values in the nodes in the right subtree of a node are greater than the value in the node.
3
http://en.wikipedia.org/wiki/Binary_search_tree
![Page 4: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/4.jpg)
107 - Trees
Constructing a BST
• We can go through a list of elements adding them in the order they occur.
• e.g. 70, 31, 93, 94, 14, 23, 73
4
![Page 5: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/5.jpg)
107 - Trees
70, 31, 93, 94, 14, 23, 73
5
70
![Page 6: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/6.jpg)
107 - Trees
70, 31, 93, 94, 14, 23, 73
6
70
31
![Page 7: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/7.jpg)
107 - Trees
70, 31, 93, 94, 14, 23, 73
7
70
31 93
![Page 8: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/8.jpg)
107 - Trees
70, 31, 93, 94, 14, 23, 73
8
70
31 93
94
![Page 9: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/9.jpg)
107 - Trees
70, 31, 93, 94, 14, 23, 73
9
70
31 93
9414
![Page 10: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/10.jpg)
107 - Trees
70, 31, 93, 94, 14, 23, 73
10
70
31 93
9414
23
![Page 11: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/11.jpg)
107 - Trees
70, 31, 93, 94, 14, 23, 73
11
70
31 93
9414
23
73
![Page 12: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/12.jpg)
107 - Trees
Your turn
• Add the elements 17, 5, 25, 2, 11, 35, 9, 16, 29, 38, 7 to a binary search tree
12
![Page 13: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/13.jpg)
107 - Trees
Map ADT and BSTs• If we use a key as the ordering component in our
BSTs we can also store a separate value.
• We can then use a BST as a Map with functions such as:
• put(key, value) - stores value using key
• get(key) - returns the value found from key
• The text book does this.
• Most introductions just use a value - this is what I will use.
13
![Page 14: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/14.jpg)
107 - Trees
Binary Search Tree code
class BST: """A Binary Search Tree (BST) class.""" def __init__(self, value, parent=None): """Construct a BST. value -- the value of the root node parent -- the parent node (of this BST subtree) """ self.value = value self.left = None self.right = None self.parent = parent # useful for some operations
14
![Page 15: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/15.jpg)
107 - Trees
Inserting a value def insert(self, value): """Insert value into the BST.""" if value == self.value: # already in the tree return elif value < self.value: if self.left: self.left.insert(value) else: self.left = BST(value, parent=self) else: if self.right: self.right.insert(value) else: self.right = BST(value, parent=self)
15
![Page 16: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/16.jpg)
107 - Trees
A Factory Method
def create(a_list): """Create a BST from the elements in a_list.""" bst = BST(a_list[0]) for i in range(1, len(a_list)): bst.insert(a_list[i]) return bst
# A factory method is one which creates and returns# a new object.
# e.g. this would be called like thisbst = BST.create([70, 31, 93, 94, 14, 23, 73])
16
![Page 17: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/17.jpg)
107 - Trees
Doing something in order
def inorder(self, function): """Traverse the BST in order performing function.""" if self.left: self.left.inorder(function) function(self.value) if self.right: self.right.inorder(function)
# for example:bst = BST.create([70, 31, 93, 94, 14, 23, 73])bst.inorder(print)
# The output is 14 23 31 70 73 93 94
17
![Page 18: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/18.jpg)
107 - Trees
Your turn
• Write a __contains__ method which returns True if the BST contains the value, otherwise False.
def __contains__(self, value):
18
![Page 19: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/19.jpg)
107 - Trees
Deleting a value• We need to find the node the value is stored
in.
• There are three cases
• the node has no children
• the node has one child
• the node has two children
19
![Page 20: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/20.jpg)
107 - Trees
Finding the node
def locate(self, value): """Return the node holding value.""" if value == self.value: return self elif value < self.value and self.left: return self.left.locate(value) elif value > self.value and self.right: return self.right.locate(value) else: return None
20
![Page 21: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/21.jpg)
107 - Trees
No children
21
![Page 22: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/22.jpg)
107 - Trees
No children
• Just delete the node and fix up its parent.
22
![Page 23: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/23.jpg)
107 - Trees
One child
23
![Page 24: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/24.jpg)
107 - Trees
One child
• Delete the node and shift its child up to take its place by changing the parent.
24
![Page 25: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/25.jpg)
107 - Trees
Two children
25
![Page 26: Binary Search Trees Section 6.8 1. 107 - Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.](https://reader036.fdocuments.in/reader036/viewer/2022081519/56649da95503460f94a9776f/html5/thumbnails/26.jpg)
107 - Trees
Two children
• Replace the value in the node with its inorder successor.
• We also have to delete the inorder successor node.
• But this can’t have more than one child.
• Why not?
26