You are on page 1of 2

INSERTION OF BINARY TREES Insertion begins as a search would begin; if the root is not equal to the value, we search

the left or right subtrees as before. Eventually, we will reach an external node and add the value as its right or left child, depending on the node's value. In other words, we examine the root and recursively insert the new node to the left subtree if the new value is less than the root, or the right subtree if the new value is greater than or equal to the root. Another way to explain insertion is that in order to insert a new node in the tree, its value is first compared with the value of the root. If its value is less than the root's, it is then compared with the value of the root's left child. If its value is greater, it is compared with the root's right child. This process continues, until the new node is compared with a leaf node, and then it is added as this node's right or left child, depending on its value. There are other ways of inserting nodes into a binary tree, but this is the only way of inserting nodes at the leaves and at the same time preserving the BST structure. The General Steps of Insertion A binary tree is constructed by the repeated insertion of new nodes into a binary tree structure. Insertion must maintain the order of the tree. That is, values to the left of a given node must be less than that node, and values to the right must be greater. The Technical Details of Insertion Inserting a node into a tree is actually two separate operations. First, the tree must be searched to determine where the node is to be inserted. Second, on the completion of the search, the node is inserted into the tree. Assuming that duplicate entries are not allowed in the tree, two cases must be considered when constructing a binary tree.

1. Insertion into an empty tree.

In this case, the node inserted into the tree is considered the root node.

2. Inserting into a non-empty tree.

The tree must be searched, as was noted above, to determine where the node is to be inserted. The new node is compared first to the root node of the tree. If the value of the new node is less than the value of the root node, the new node is: o appended as the left leaf of the root node if the left subtree is empty o else, the search continues down the left subtree.

If the value of the new node is greater than the value of the root node, the new node is: o o appended as the right leaf of the root node if the right subtree is empty else, the search process continues down the right subtree.

If this search procedure finds that the insertion node already exists in the tree, the procedure terminates as it can not insert it again. Note that careful consideration of the above algorithm reveals that the searching procedure described is actually a modified inorder traversal of the tree.

Deletion is the 'hard nut' to crack! Suppose we wish to delete a node from the tree below:

Fig. 2.3.1: An example binary search tree (shown in abstraction)


There are basically four cases to deal with as regards the node (holding the value) to be deleted:
1. 2. 3. 4.

A leaf node A non-leaf node with an empty left subtree A non-leaf node with an empty right subtree A non-leaf node with neither of its subtrees empty

Consistently using the in-order successor or the in-order predecessor for every instance of the two-child case can lead to an unbalanced tree, so good implementations add inconsistency to this selection. Running Time Analysis: Although this operation does not always traverse the tree down to a leaf, this is always a possibility; thus in the worst case it requires time proportional to the height of the tree. It does not require more even when the node has two children, since it still follows a single path and does not visit any node twice.

Traversal
Once the binary search tree has been created, its elements can be retrieved in-order by recursively traversing the left subtree of the root node, accessing the node itself, then recursively traversing the right subtree of the node, continuing this pattern with each node in the tree as it's recursively accessed. As with all binary trees, one may conduct a pre-order traversal or a post-order traversal, but neither are likely to be useful for binary search trees.

Sort
A binary search tree can be used to implement a simple but efficient sorting algorithm. Similar to heapsort, we insert all the values we wish to sort into a new ordered data structurein this case a binary search treeand then traverse it in order,

You might also like