Lec24. Binary Search Tree (BST)
Lec24. Binary Search Tree (BST)
Lec24. Binary Search Tree (BST)
read Chapter 10
Sequential search
• sequential search: Locates a target value in an array/list by
examining each element from start to finish.
– How many elements will it need to examine?
– Example: Searching the array below for the value 42:
inde 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
x
value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 10
3
i
2
Binary search
• binary search: Locates a target value in a sorted array/list by
successively eliminating half of the array from consideration.
– How many elements will it need to examine?
– Example: Searching the array below for the value 42:
inde 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
x
value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 10
3
min mid max
3
Binary search-Guessing Game
• Assume that a computer
program generates a random
number between 1 and 100
and the user tries to guess it.
• After every incorrect guess,
the program gives a hint
whether the guess is too high
or too low.
• A poor algorithm is to guess 1,
2, 3, and so on.
• A smarter algorithm is to
guess the middle number and
cut the range in half each time
4
Binary search trees (BST)
• BSTs store their elements in sorted
order, which is helpful
root
for searching/sorting tasks.
• A BST is a binary tree that is either: 55
– empty, or
29 87
– a root node R such that:
• every element of R's left subtree
contains data "less than" R's data, -3 42 60 91
• every element of R's right subtree
contains data "greater than" R's data,
• R's left and right subtrees are also binary
search trees.
5
Exercise
• Which of the trees shown are legal binary search trees?
m 8
42
g q 5 11
b k x 2 7 10 18
7.2
e 4 20
-5 1.9 9.6
18
-1 -7 8.1 21.3
6
Searching a BST
• Describe an algorithm for searching the tree below for the value 57.
// find node with given key, assumes
//non-empty tree Complexity : O(logn)
PtrNode BinarySearchTree::find(int key){
// start at root
Node* current = root;
57 <= 63?
// while no match,
while(current->elt != key){ 63
if(key < current->elt) 57 <= 27?
// go left? 27 80
current =
current->left; 57 <= 51?
else 13 51 70 92
// or go right?
current =
current->right; 57 <= 58? 82
// if no child, 26 33 58
if(current == NULL)
return NULL;
}
return current; // found
57 = 57 57 60 7
Searching a BST
struct Node { // a node of the tree
Elem elt; // element value
Node* left; // left child
Node* right; // right child
Node() : elt(), left(NULL), right(NULL) { } // constructor
Node(int value) : elt(value), left(NULL), right(NULL) { }
// constructor
};
PtrNode BinarySearchTree::contains(int k) {
PtrNode v = contains(k, root); // search from virtual root
return v;
}
27
12 13 26 27 33 51 13 51
12 26 33
9
Inorder Traversal of BST
• Given a list of n numbers or key-value pairs
• Insert the keys into a BST
• Traverse the BST using in-order traversal
• Running time?
– Insert the keys into a BST O(n * h)
– Traverse the BST using in-order traversal O(n)
– Running time? In the worst-case still O(n^2) 27
13 51
12 13 26 27 33 51
12 26 33
10
Preorder Traversal & Postorder Traversal
Preorder 27
• Process (e.g. print) the node first
• Then process the left sub-tree 13 51
• Then process the right sub-tree
• Thus, you process the nodes in the order you visit
12 26 33
them, from left sub-trees to right sub-trees
27 13 12 26 51 33
Postorder
• Process the node after visiting the sub-trees
– For each node:
• Traverse the left sub-tree 12 26 13 33 51 27
• Traverse the right sub-tree
• Process the node
11
Adding to a BST
• Suppose we want to add the value 14 to the BST below.
– Where should the new node be added?
50 50
20
75
98 20 75
80
31
150 11 31 98
39
23 23 39 80 150
11
77
77
13
Exercise
• Add a method add to the BinarySearchTree class that adds a
given integer value to the tree. Assume that the elements of the
BinarySearchTree constitute a legal binary search tree, and add
the new value in the appropriate place to maintain ordering.
• tree.add(49);
root
55
29 87
-3 42 60 91
49 14
add(int value)-
iterative
Adding a node
– Check if the tree is empty
– find the insertion point then link the node to the tree
...
15
add(int value)-
iterative
while(true) {
if(value < current->elt) { // go left?
if(current->left == NULL) { // if end of the line
current->left = newNode;
return;
}
current = current->left;
} // end if go left
else { // or go right?
Complexity : O(logn)
// if end of the right line
if(current->right == NULL) {
current->right = newNode;
27
return;
}
current = current->right;
} // end else go right 13 51
} // end while
} // end else
n++;
} 26 33 58
newNode 12 57 60
16
Searching BSTs
• The BSTs below contain the same elements. root
– What orders are "better" for searching?
root
19
root 14
7
9 11
4 19
6 14 9
6 14
7
4 7 11 19
9 6
11 4
• What is the worst-case height of the tree?
• Could be O(n)! 17
Trees and balance
• balanced tree: One whose subtrees differ in height by at most 1 and
are themselves balanced.
– A balanced tree of N nodes has a height of ~ log2 N.
– A very unbalanced tree can have a height close to N.
6 14
4 8 19
height = 4 7
(balanced)
18
Exercise
• Add a method getMin to the class that returns the minimum integer
value from the tree. Assume that the elements constitute a legal
binary search tree.
int min = tree.getMin(); // -3
Keep going to the left till you hit a leaf O(h)
root
• getMax? Finding the maximum:
– Keep going to the right till you hit a leaf O(h) 55
42 60 91
36 73
20
Cases for removal 1
1. a leaf: replace with null
2. a node with a left child only: replace with left child
3. a node with a right child only: replace with right child
55 55 29 42
29 29 42
tree.remove(29);
-3 42 42
tree.remove(-3); tree.remove(55);
21
Cases for removal 2
4. a node with both children: replace with min from right
55 60
tree.remove(55);
29 87 29 87
-3 42 60 91 -3 42 91
22
Deleting Nodes
• To delete a node (without destroying the 5
24
Erase
The node is deleted and left of 81 is set to NULL
25
Node removal
Erasing the node containing 40 is similar
26
Node removal
The node is deleted and right of 39 is set to NULL
27
Node removal
If a node has only one child, we can simply promote the sub-
tree associated with the child
– Consider removing 8 which has one left child
28
Node removal
The node 8 is deleted and the tree of 11
is updated to point to 3
29
Node removal
There is no difference in promoting a single node or a sub-tree
– To remove 39, it has a single child 11
30
Node removal
The node containing 39 is deleted and left of 42 is updated to
point to 11
– Notice that order is still maintained
31
Node removal
Consider erasing the node containing 99
32
Node removal
The node is deleted and the left sub-tree is promoted:
– The member variable right of 70 is set to point to 92
– Again, the order of the tree is maintained
33
Node removal
Finally, we will consider the problem of erasing a full node, e.g.,
42
34
Node removal
In this case, we replace 42 with 47
– We temporarily have two copies of 47 in the tree
35
Node removal
We now recursively erase 47 from the right sub-tree
– We note that 47 is a leaf node in the right sub-tree
36
Node removal
37
Node removal
Suppose we want to erase the root 47 again:
– We must copy the minimum of the right sub-tree
– We could promote the maximum object in the left sub-tree and
achieve similar results
38
Node removal
We copy 51 from the right sub-tree
39
Node removal
We must proceed by delete 51 from the right sub-tree
40
Node removal
In this case, the node storing 51 has just a single child
41
Node removal
We delete the node containing 51 and assign the member
variable left of 70 to point to 59
42
Node removal
Note that after seven removals, the remaining tree is still
correctly sorted
43
Node removal
In the two examples of removing a full node, we promoted:
– A node with no children
– A node with right child
Is it possible, in removing a full node, to promote a child with
two children?
44
Node removal
Recall that we promoted the minimum value in the right sub-
tree
– If that node had a left sub-tree, that sub-tree would contain a
smaller value
45
Code
Code will be posted on Moodle
46