Binary Search Tree: Reny Jose
Binary Search Tree: Reny Jose
Reny Jose
Binary Search Tree
• Binary Search Tree is a node-based
binary tree data structure which has the
following properties:
• The left sub tree of a node contains only
nodes with keys lesser than the node’s key.
• The right sub tree of a node contains only
nodes with keys greater than the node’s
key.
• The left and right sub tree each must also
be a binary search tree.
Binary Search Tree Property
• Stored keys must satisfy the binary search
tree property. 56
• y in left subtree of x, then key[y] key[x].
• y in right subtree of x, then key[y] key[x]. 26 200
18 28 190 213
12 24 27
Tree-Search(x,
Tree-Search(x,k) k)
1.
1. ififxx==NIL
NILor
orkk==key[x]
key[x] 26 200
2.
2. then thenreturn
returnxx
3.
3. ififkk<<key[x]
key[x]
18 28 190 213
4.
4. then thenreturn
returnTree-Search(left[x],
Tree-Search(left[x],k)
k)
5.
5. else elsereturn
returnTree-Search(right[x],
Tree-Search(right[x],k)k)
12 24 27
Running time: O(h)
Aside: tail-recursion
• Implementing
insertion
resursively
Does the
order of
inserting
elements
into a tree
matter?
• Unbalanced trees are not desirable
because search time increases!
• Advanced tree structures, such as
red-black trees, guarantee balanced
trees.
Deletion
• Delete function is used to delete the specified node
from a binary search tree.
• There are three situations of deleting a node from
binary search tree.
• The node to be deleted is a leaf node
• The node to be deleted has only one child.
• The node to be deleted has two children.
The node to be deleted is a leaf node
• Replace the leaf node with the NULL and simple free the allocated
space.
The node to be deleted has only one child.
• Replace the node with its child and delete the child node
Deleting a node with only one child
The node to be deleted has two children.
1. Replace the node’s value with its in-order predecessor (largest value
in the left sub-tree) or in-order successor (smallest value in the right
sub-tree).
2. After the procedure, replace the node with NULL and free the
allocated space.
Deleting a node with two children (cont.)
• In-order predecessor of 17 is 15 ,
• Predecessor of 6 is 4
• Predecessor of 13 is 9.
The in order predecessor is a node with maximum value in left subtree
Case 1: if given node is left most leaf node of tree, there is no in order predecessor, in that case return NULL
Case 2: Node has left subtree. In that case, maximum value in left subtree will be predecessor of given node.
We can find maximum value in tree by going deep down right subtree, till right subtree is NULL, and then return the
last node.
Case 3: node does not have left subtree but it is also not the leftmost leaf node
Then parent of given node will be inorder predecessor.
.
Algorithm Step 1
Delete (TREE, VAL) 1. if TREE=NULL, the node to be deleted is not present in the tree.
Step 1:
1. IF TREE = NULL
2. If the value is less, we call the algorithm recursively on the node’s
Write "VAL not found in the tree" left sub-tree,
2. ELSE IF VAL < TREE DATA 3. If the value is greater we call the algorithm recursively on the
Delete(TREE->LEFT, VAL) node’s right sub-tree
3. ELSE IF VAL > TREE DATA 4. If we have found the node whose value is equal to VAL, then we
Delete(TREE RIGHT, VAL) check which case of deletion it is.
4. ELSE IF TREE LEFT AND TREE RIGHT a) If the node to be deleted has both left and right children, then
1. SET TEMP = findLargestNode(TREE LEFT)
b) we find the in-order predecessor of the node by calling
2. SET TREE DATA = TEMP DATA
3. Delete(TREE LEFT, TEMP DATA)
findLargestNode(TREE -> LEFT)
5. ELSE c) replace the current node’s value with that of its in-order
1. SET TEMP = TREE predecessor.
2. IF TREE LEFT = NULL AND TREE RIGHT = d) call Delete(TREE -> LEFT, TEMP -> DATA) to delete the initial
NULL node of the in-order predecessor.
SET TREE = NULL 5. If the node to be deleted does not have any child, then we simply
3. ELSE IF TREE LEFT != NULL set the node to NULL.
SET TREE = TREE LEFT
6. if the node to be deleted has either a left or a right child but not
4. ELSE
SET TREE = TREE RIGHT
both, then the current node is replaced by its child node and the
5. [END OF IF] initial child node is deleted from the tree.
6. FREE TEMP
6. [END OF IF]
Step 2: END
3 is to be deleted
Home work
• Consider the following numbers
98, 2, 48, 12, 56, 32, 4, 67, 23, 87, 23, 55, 46
1. Create a binary search tree with the given input
2. Insert 21, 39, 45, 54, and 63 into the tree
3. Delete values 23, 56, 2, and 45 from the tree
4. Demonstrate the binary traversal
References
• https://www.javatpoint.com/binary-search-tree
• https://makeinjava.com/find-inorder-predecessor-binary-search-tree-
bst-examples
/
• https://youtu.be/jBDc4Uuif28