Binary Tree
Algorithm for the implementation of a Binary tree:
Step-1: If value of new element < current element, then go to step-2 or else step -3 Step-2: If the current element does not have a left sub-tree, then make your new
element the left child of the current element; else make the existing left child as your current element and go to step-1
Step-3: If the current element does not have a right sub-tree, then make your new element the right child of the current element; else make the existing right child as your current element and go to step-1
Program 6.1 depicts the segment of code for the creation of a binary tree. struct NODE
{
struct NODE *left; int value;
struct NODE *right;
};
create_tree( struct NODE *curr, struct NODE *new )
{
Trees
if(new->value <= curr->value)
{
if(curr->left != NULL) create_tree(curr->left, new); else
curr->left = new;
}
else
{
if(curr->right != NULL) create_tree(curr->right, new); else
curr->right = new;
}
}
Program 6.1 : Binary tree creation