03-05-2013, 03:10 PM
Binary Search Trees
Binary Search.ppt (Size: 1.01 MB / Downloads: 83)
Basic Property
In a binary search tree,
the left subtree contains key values less than the root
the right subtree contains key values greater than or equal to the root.
BST Insertion
To insert data all we need to do is follow the branches to an empty subtree and then insert the new node.
In other words, all inserts take place at a leaf or at a leaflike node – a node that has only one null subtree.
Deletion
There are the following possible cases when we delete a node:
The node to be deleted has no children. In this case, all we need to do is delete the node.
The node to be deleted has only a right subtree. We delete the node and attach the right subtree to the deleted node’s parent.
The node to be deleted has only a left subtree. We delete the node and attach the left subtree to the deleted node’s parent.
The node to be deleted has two subtrees. It is possible to delete a node from the middle of a tree, but the result tends to create very unbalanced trees.
Deletion from the middle of a tree
Rather than simply delete the node, we try to maintain the existing structure as much as possible by finding data to take the place of the deleted data. This can be done in one of two ways.
We can find the largest node in the deleted node’s left subtree and move its data to replace the deleted node’s data.
We can find the smallest node on the deleted node’s right subtree and move its data to replace the deleted node’s data.
Either of these moves preserves the integrity of the binary search tree.