Module 4.3
Module 4.3
Module 4.3
State whether the binary trees are binary search trees or not.
• Create a binary search tree using the following data elements: 45, 39, 56,
12, 34, 78, 32, 10, 89, 54, 67, 81
• The first value in the given list is always the root
Problem
1. Create a binary search tree with the input given below:
98, 2, 48, 12, 56, 32, 4, 67, 23, 87, 23, 55, 46
6
OPERATIONS ON BINARY SEARCH TREES
Operations on Binary Search Tree are:
1. Searching for a Node in a Binary Search Tree
2. Inserting a New Node in a Binary Search Tree
3. Deleting a Node from a Binary Search Tree
Case 1: Deleting a Node that has No Children
Case 2: Deleting a Node with One Child
Case 3: Deleting a Node with Two Children
4. Determining the Height of a Binary Search Tree
5. Determining the Number of Nodes
Determining the Number of Internal Nodes
Determining the Number of External Nodes
6. Finding the Mirror Image of a Binary Search Tree
7. Deleting a Binary Search Tree
8. Finding the Smallest Node in a Binary Search Tree
9. Finding the Largest Node in a Binary Search Tree
Searching for a Node in a Binary Search Tree
The delete function deletes a node from the binary search tree.
Case 1: Deleting a node that has no children
Case 2: Deleting a node with one child
Case 3: Deleting a node with 2 children
Case 1: Deleting a Node that has No Children
• Look at the binary search tree given in Fig. If we have to delete node 78, we
can simply remove this node without any issue.
• This is the simplest case of deletion.
Case 2: Deleting a Node with One Child
• In this case, the node’s child is set
as the child of the node’s parent.
• In other words, replace the node
with its child.
• Now, if the node is the left child of
its parent, the node’s child becomes
the left child of the node’s parent.
• Correspondingly, if the node is the
right child of its parent, the node’s
child becomes the right child of the
node’s parent.
Case 3: Deleting a Node with Two Children
• In this case, 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).
• The in-order predecessor or the successor can then be deleted using any of these cases.
• Look at the Fig. see how deletion of node with value 56 is handled with its in-order
predecessor.
This deletion could also be handled by replacing node 56 with its
in-order successor, as shown in Fig.
• In Step 1 of the algorithm, first check if TREE=NULL, because
if it is true, then the node to be deleted is not present in the tree.
• If that is not the case, then we check if the value to be deleted is
less than the current node’s data. In case the value is less, we
call the algorithm recursively on the node’s left sub-tree,
otherwise the algorithm is called recursively on the node’s right
sub-tree.
• Note that if we have found the node whose value is equal to
VAL, then we check which case of deletion it is.
• If the node to be deleted has both left and right children, then we
find the in-order predecessor of the node by calling
findLargestNode(TREE ->LEFT) and replace the current node’s
value with that of its in-order predecessor.
• Then, we call Delete(TREE -> LEFT, TEMP -> DATA) to delete
the initial node of the in-order predecessor. Thus, we reduce the
case 3 of deletion into either case 1 or case 2 of deletion
• If the node to be deleted does not have any child, then we simply
set the node to NULL.
• Last but not the least, if the node to be deleted has either a left or
a right child bu not both, then the current node is replaced by its
Determining the height of a BST
• In order to determine the height of a binary search tree, we
calculate the height of the left sub-tree and the right sub-tree.
Whichever height is greater, 1 is added to it.
• For example, if the height of the left sub-tree is greater than that of
the right sub-tree, then 1 is added to the left sub-tree, else 1 is
added to the right sub-tree.
• Look at Fig. Since the height of the right sub-tree is greater than
the height of the left sub-tree, the height of the tree = height (right
sub-tree) + 1= 2 + 1 = 3.
• Figure shows a recursive algorithm that
determines the height of a binary search
tree.
• In Step 1 of the algorithm, first check if the
current node of the TREE = NULL. If the
condition is true, then 0 is returned to the
calling code.
• Otherwise, for every node, we recursively
call the algorithm to calculate the height of
its left sub-tree as well as its right sub-tree.
• The height of the tree at that node is given
by adding 1 to the height of the left sub-tree
or the height of right sub-tree, whichever is
greater.
Determining the Number of Nodes
• To calculate the total number of internal nodes or non-leaf nodes, we count the
number of internal nodes in the left sub-tree and the right sub-tree and add 1
to it (1 is added for the root node).
• Number of Internal Nodes= totalInternalNodes(left sub–tree) +
totalInternalNodes(right sub–tree) + 1
• Consider the tree given in Fig. The total number of internal nodes in the tree
can be calculated as:
• Total internal nodes of left sub–tree = 0
• Total internal nodes of right sub–tree = 3
• Total internal nodes of tree = (0 + 3) + 1 = 4
• For every node, we recursively call the
algorithm on its left subtree as well as
the right sub-tree.
• The total number of internal nodes at
a given node is then returned by
adding internal nodes in its left as
well as right sub-tree.
• However, if the tree is empty, that is
TREE = NULL, then the number of
internal nodes will be zero.
• Also if there is only one node in the
tree, then the number of internal
nodes will be zero.
Determining the Number of External Nodes
• To calculate the total number of external nodes or leaf nodes, we add the
number of external nodes in the left sub-tree and the right sub-tree.
• However if the tree is empty, that is TREE = NULL, then the number of external
nodes will be zero.
• But if there is only one node in the tree, then the number of external nodes will
be one.
• Number of external nodes = totalExternalNodes(left sub–tree) +
totalExternalNodes (right sub–tree).
• The total number of external nodes in the given tree can be calculated as:
• Total external nodes of left sub–tree = 1
• Total external nodes of left sub–tree = 2
• Total external nodes of tree = 1 + 2 = 3
• For every node, we recursively call the
algorithm on its left sub-tree as well as
the right sub-tree.
• The total number of external nodes at a
given node is then returned by adding
the external nodes in its left as well as
right sub-tree.
• However if the tree is empty, that is
TREE = NULL, then the number of
external nodes will be zero.
• Also if there is only one node in the tree,
then there will be only one external
node (that is the root node).
Finding the Mirror Image of a Binary Search Tree