544 Programming and Data Structures
Explanation In the above program, the following user-defined functions are given,
int insert (int) : This function is used for insertion of an element,
int del (int) : This function deletes a specific element.
int inorder (struct nodes *) : This function displays the elements in order traversal.
int preorder (struct nodes *) : This function is used to display the list of elements in preorder
traversal.
int postorder (struct nodes *) : This function displays the list of elements in postorder traversal
format.
int show (struct nodes*, int) : This function shows the elements in entered format.
For the details of above traversal methods read the topic Traversing Binary Trees.
15.6 BINARY SEARCH TREE
A binary search tree is a tree in which each node of the tree contains a value larger than all nodes of
their left subtree and smaller than all nodes of their right subtree. It is a very important data structure
and a particular element can be searched.
Fig. 15.13 shows the binary search tree.
Fig. 15.13 Binary search tree
15.7 INSERTION AND DELETION OPERATIONS
Searching and insertion operations are related with each other in a binary search tree. Before performing
insertion operation, the location where the element is to be inserted is known. For example, an element
is given and the task is to find and insert the element. This operation can be carried out as follows.
Compare the element with the root node element. The conditions would be as follows.
a) If element < root node element: If element is smaller than the element of the root node then select
left branch node and place it at the left side of the node.
b) If element> root node element: If element is greater than the element of the root node then place
the element in the right branch of root node.
This process is performed for all the nodes in the entire tree and an element is placed at the left when
its value is smaller than the root node and at the right side when its value is larger than the root node.
Finally, when our search is successful and we get the element having the same value as the element
to be inserted, we reach the accurate location to perform an insertion operation. Fig. 15.14 shows the
insertion operation.