You are on page 1of 2

SOME EXTRA ALGORITHMS OF A BINARY TREE (IMP)

1. DETERMINING HEIGHT OF A BINARY TREE


The height of a Binary Tree at a given node will be equal to the max height of the left and right sub
trees plus 1.

Algorithm to determine the height of the binary tree


Determine_height ( BST tree )
1. If ( tree == NULL ) then return 0
2. Else
3. Left_height = determine_height( tree->left )
Right_height = determine_height( tree->right )
4. If (left_height > right_height)
Return ++left_height
Else
Return ++right_height

2. DETERMINING NUMBER OF EXTERNAL OR LEAF NODES


The no. of external or leaf nodes in a tree is equal to the sum of the external or leaf nodes in the left
subtree and the external or leaf nodes in the right subtree of a given node.

Algorithm to determine the no. of leaf nodes of the binary tree


Leaf_nodes( BST tree)

1. If ( tree==NULL ) then return 0


2. else if ( ( tree->left==NULL ) && ( tree->right==NULL ) )
return 1
3. else
4. return ( leaf_nodes( tree->left ) + leaf_nodes( tree->right ) )

3. DETERMINING NUMBER OF INTERNAL OR NON LEAF NODES


The no. of internal or non-leaf nodes in a tree is equal to the no. of the internal or non-leaf
nodes in the left subtree plus no. of internal or non-leaf nodes in the right subtree of a given
node plus 1.

Algorithm to determine the no. of Internal or non-leaf nodes of the binary


tree
internal_nodes( BST tree )

if ( ( tree==NULL ) || ( ( tree->left == NULL ) && ( tree->right == NULL ) ) )

return 0

else
return ( internal_nodes (tree->left) + internal_nodes(tree->right) + 1

4. DETERMINING DEPTH OF THE BINARY TREE


The depth of the binary tree is 1 more than the max of the depths of the left and right
subtrees.

Algorithm to determine the depth of the binary tree


Depth ( left, right, root, depth )
1. If root = NULL then set depth = 0 and return.
2. Call depth (left, right, left[root], depth_left )
3. Call depth (left, right, right[root], depth_right )
4. If depth_left >= depth_left then
Set depth = depth_left + 1
Else
Set depth = depth_right + 1
5. Return

5. DETERMINING TOTAL NO. OF THE NODES IN THE BINARY TREE


The no. of the nodes in the binary tree is 1 more than the no. of nodes in the left subtree
plus the no. of the nodes in the right subtree.

Algorithm to determine the total no. of the nodes in the binary tree
Count ( left, right, root, num )
1. If root = NULL then set num = 0 and return.
2. Call count ( left, right, left[root], num_left)
3. Call count (left, right, right[root], num_right)
4. Set num = num_left + num_right + 1
5. Return

6. DETERMINING THE MIRROR IMAGE OF THE BINARY TREE


The mirror image of the binary tree is obtained by interchanging left and right child recursively.

Algorithm to determine the mirror image of the binary tree


Find_mirrorimage ( BST tree )
1. if tree != NULL
2. Call find_mirrorimage ( tree->left )
3. Call find_mirrorimage ( tree->right )
4. set temp = tree->left
5. tree->left = tree->right
6. tree->right = temp
7. return

You might also like