Lecture 22 - 21 Binary Search Trees
Lecture 22 - 21 Binary Search Trees
following properties:
◦ All items (keys) in the left subtree are less than the root’s key.
◦ All items (keys) in the right subtree are greater than the root’s
key.
◦ Each subtree is, itself, a binary search tree.
◦ Assume each node of a binary tree stores a data item
◦ Assume data items are of some type that be ordered and all
items are distinct. No two items have the same value.
◦ A binary search tree is a binary tree such that
◦ for every node X in the tree:
the values of all the items in its left subtree are smaller than the
value of the item in X
the values of all items in its right subtree are greater than the
value of the item in X.
Here you can see the
basic structure of a
binary search tree.
A binary search tree is organized as the name
suggests
A tree can be represented by a linked data structure
Key
Key Left Right Key Left Right
If a child or parent is missing, the appropriate field
contains the value NULL.
6 6
2 8 2 8
1 4 1 4
3 3 7
◦ Insertion
◦ Search
◦ Traversal
◦ Deletion
Find Minimum
Find the item that has the minimum value in the tree
Find Maximum
Find the item that has the maximum value in the tree
Print
Print the values of all items in the tree using a traversal strategy
that is appropriate for the application
Successor
Predecessor
The first value inserted goes at the root
Every node inserted becomes a leaf
Descend left or right depending on value
Inserting Item 5 to the Tree
t
6 Tree root node
t
2 8 NULL NULL
t
1 NULL NULL 4 NULL
New Node
ALGORITHM
If node == NULL
return createNode(data)
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
1. void insert(int key) {root = insertKey(root, key);}
2. // Insert key in the tree
3. Node insertKey(Node root, int key) {
4. // Return a new node if the tree is empty
5. if (root == null)
6. {
7. root = new Node(key);
8. return root;
9. }
10. // Traverse to the correct place and insert the node
11. if (key < root.key)
12. root.left = insertKey(root.left, key);
13. else if (key > root.key)
14. root.right = insertKey(root.right, key);
15. return root;
16. }
If z has no children, we modify its parent to replace z
with NULL as child.
25
15 40
5 20 45 25
15 40
18
removeElement(45)
-------------> 5 20
18
x occurs at a node with one child v: remove the node
containing x and make v a direct child of x’s parent (if any).
25
15 40
5 20 45
18
removeElement(20) 25
------------->
15 40
5 18 45
x occurs at a node with two children: first replace x with
smallest value in right subtree of x. This value occurs at a
node with no left child. So we can delete this node using
one of the two previous cases.
25
15 40
5 20 45
25
18 18 40
removeElement(15)
5 20 45
------------->
2 8 3 8
1 5 1 5
3 3
4 Deletion of node 2. 4
before after
There are three cases for deleting a node from a binary search tree.
CASE I
In the first case, the node to be deleted is the leaf node. In such a case, simply delete the
node from the tree.
4 is to be deleted
There are three cases for deleting a node from a binary search tree.
CASE I
In the first case, the node to be deleted is the leaf node. In such a case, simply delete the
node from the tree.
4 is deleted
Case II
1.Inthe second case, the node to be deleted lies has a single child node. In such a case follow the steps
below:
2.Replace that node with its child node.
6 is to be deleted
Case II
1.Inthe second case, the node to be deleted lies has a single child node. In such a case follow the steps
below:
2.Replace that node with its child node.
copy the value of its child to the node and delete the child
Case II
In the second case, the node to be deleted lies has a single child node. In such a case follow the steps
below:
1. Replace that node with its child node.
2. Remove the child node from its original position.
Final tree
Case III
In the third case, the node to be deleted has two children. In such a case follow the steps below:
1. Get the inorder successor of that node.
2. Replace the node with the inorder successor.
3. Remove the inorder successor from its original position.
3 is to be deleted
Case III
In the third case, the node to be deleted has two children. In such a case follow the steps below:
1. Get the inorder successor of that node.
2. Replace the node with the inorder successor.
3. Remove the inorder successor from its original position.
Minimum
Maximum
Successor
Predecessor
This function follows the BST property,
if value of k is smaller than that of x, then it should be in left sub
tree of x,
else it should be in right sub tree of x.
2 8
1 4
3
Start at the root
Is target = value at current node?
◦ We’re done
ALGORITHM
If root == NULL
return NULL;
If number == root->data
return root->data;
If number < root->data
return search(root->left)
If number > root->data
return search(root->right)
The minimum element of BST is the left most node of
left sub tree.
Therefore the minimum can always be found by
tracking the left child pointers until an empty sub tree
is reached.
If there is no left sub tree then minimum key is at
root( i.e. key[ x])
Tree-Minimum(x)
{
while( left[ x] != NULL)
x = left [x]
return x
}
The maximum element of BST is the right most node of
right sub tree.
Therefore the maximum can always be found by tracking
the right child pointers until an empty sub tree is reached.
Tree-Maximum( x)
{
while( right[ x] != NULL)
x = right [x]
return x
}
The successor of a node x, is node y, that has the smallest
key greater than that of x.
◦ If x has a right subtree, then successor(x) is the left most
element in that sub tree.
◦ If x has a left sub tree, then the predecessor must be the right
most element of the left sub tree.