If the binary search tree is initially empty, then the element is inserted as a root node. Otherwise the element is inserted as terminal node. If the element is less than the element in the root node, then the element is inserted in the left sub tree else to the right sub tree.

Determining the Height of a Binary Tree

We find the height of the left sub tree and right sub tree of a given node. The height of a binary tree at a given node will be equal to maximum height of the left and right sub tree plus 1.The program given below illustrates the implementation of various steps required,

Calculating Total Number of Nodes

Any of the traversal schemes or algorithm can be used to determine the number of elements present in the tree.

The approach we use is:

Number of elements in the tree is equal to number of nodes in left sub tree plus number of nodes in right sub tree plus one.

Complete program illustrating insertion and traversal in Binary Search Tree