Insertion into a Binary Search Tree
A new node must be inserted in a way that preserves the conditions for a binary search tree
Insertion is similar to search
- Here’s an alternative version of insert (the one in the book might work, but appears to have some problems):
void InsertTree(TreeNode **subtree, TreeNode *newnode){ if ( *subtree == NULL ) { *subtree = newnode; newnode->left = newnode->right = NULL; } else if ( LT(newnode->entry.key,(*subtree)->entry.key) )
InsertTree(&(*subtree)->left,newnode);
else
InsertTree(&(*subtree)->right,newnode);}
To insert a new node in the tree, allocate the new node, then:
InsertTree( & root, newnode );
Note that ideally we should check for duplicate entries