4. Background
Up to this point, we have seen:
• Binary search in an array data structure
• The concept of a binary tree
• Recursion
We are going to use all of these concepts when learning about binary
search trees.
2
7. Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
3
8. Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
3
9. Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
– Each node can legally have zero, one, or two children
3
10. Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
– Each node can legally have zero, one, or two children
• The Binary Search Tree Property must hold
3
11. Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
– Each node can legally have zero, one, or two children
• The Binary Search Tree Property must hold
– If duplicates not allowed: “The key in each node must be greater
than all keys stored in the left sub-tree, and less-than all keys in the
right sub-tree.”
3
12. Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
– Each node can legally have zero, one, or two children
• The Binary Search Tree Property must hold
– If duplicates not allowed: “The key in each node must be greater
than all keys stored in the left sub-tree, and less-than all keys in the
right sub-tree.”
– If duplicates allowed: “The key in each node must be greater than all
keys stored in the left sub-tree, and less-than or equal-to all keys in
the right sub-tree.”
3
13. Binary Search Tree
A Binary Search Tree (BST) has the following properties:
• Is a binary tree data structure
– Each node of the tree has at most two children (a “left” child and a
“right” child)
– Each node can legally have zero, one, or two children
• The Binary Search Tree Property must hold
– If duplicates not allowed: “The key in each node must be greater
than all keys stored in the left sub-tree, and less-than all keys in the
right sub-tree.”
– If duplicates allowed: “The key in each node must be greater than all
keys stored in the left sub-tree, and less-than or equal-to all keys in
the right sub-tree.”
– This guarantees that the tree is in sorted order.
3
14. Binary Search Tree
• If the Binary Search Tree Property holds, this guarantees that the
tree is in sorted order, thus making it a BST.
• An in-order traversal of the tree will visit the nodes in order from
lowest to highest.
• Let’s take a look at a few trees and apply these tests to them.
4
15. Binary Search Tree
Is the following a binary search tree?
. 37 .
. 21 .
11 24
. 55 .
48 62
5
16. Binary Search Tree
Is the following a binary search tree?
. 37 .
. 20 .
13 24
55 .
56 .
100
6
17. Binary Search Tree
Is the following a binary search tree?
. 37 .
. 20 .
21 24
. 55 .
18 62
7
19. Binary Search Tree
Why use a BST? What are the advantages of a BST compared to some
of the data structures we have already learned about, such as...
• An array?
• A sorted array?
• A linked-list?
We will revisit this after discussing how searching, insertion, and deletion
works.
9
21. Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
10
22. Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
10
23. Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
10
24. Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
10
25. Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
• If has a right child, continue recursively on the right child
10
26. Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, search key not in tree
10
27. Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, search key not in tree
• Else K < C
10
28. Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, search key not in tree
• Else K < C
• If has a left child, continue recursively on the left child
10
29. Search
How do we search for a key K in a binary search tree? Beginning at the
root of the tree, do the following operations:
• Call the key of the current node C
• If K == C, our search is complete!
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, search key not in tree
• Else K < C
• If has a left child, continue recursively on the left child
• Else, search key not in tree
10
32. Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
12
33. Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
12
34. Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
12
35. Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
12
36. Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
12
37. Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
• If has a right child, continue recursively on the right child
12
38. Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, add right child with key K
12
39. Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, add right child with key K
• Else K < C
12
40. Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, add right child with key K
• Else K < C
• If has a left child, continue recursively on the left child
12
41. Insert
How do we insert a key K in a binary search tree? Starting from the root
node:
• Call the key of the current node C
• If the root node is null, insert a new node with key K as the root
• Else if K == C, key already exists in tree, no need to insert
(assuming no duplicates)
• Else if K > C
• If has a right child, continue recursively on the right child
• Else, add right child with key K
• Else K < C
• If has a left child, continue recursively on the left child
• Else, add left child with key K
12
59. Delete
How do we do deletion?
• Traverse the tree, searching for the node with key K
28
60. Delete
How do we do deletion?
• Traverse the tree, searching for the node with key K
• If no node with key K exists, nothing to do!
28
61. Delete
How do we do deletion?
• Traverse the tree, searching for the node with key K
• If no node with key K exists, nothing to do!
• Else, execute the recursive delete operation.
28
63. Delete
What does the delete operation look like?
• Call the node needing deletion C
29
64. Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
29
65. Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
29
66. Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
29
67. Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
29
68. Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
29
69. Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
– In order predecessor: The right-most node of the left sub-tree.
29
70. Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
– In order predecessor: The right-most node of the left sub-tree.
– Copy the key from G into C
29
71. Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
– In order predecessor: The right-most node of the left sub-tree.
– Copy the key from G into C
– Call the delete operation recursively on node G
29
72. Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
– In order predecessor: The right-most node of the left sub-tree.
– Copy the key from G into C
– Call the delete operation recursively on node G
29
73. Delete
What does the delete operation look like?
• Call the node needing deletion C
• If C has 0 children: just drop the reference to it, and we’re done
• Else if C has 1 child: replace the parent of C’s reference to C with a
reference to the single child, and we’re done
• Else C has 2 children:
– Find the in-order successor (or predecessor) of C. Call this node G.
– In order successor: The left-most node of the right sub-tree.
– In order predecessor: The right-most node of the left sub-tree.
– Copy the key from G into C
– Call the delete operation recursively on node G
When doing recursion in the 2-children case, eventually a node will be
reached that has either 0 or 1 children, in which case we do one of the
base-case operations described above. Let’s look at an example.
29