Sunteți pe pagina 1din 7

Data Structure Notes

Binary Search Tree


Binary Search tree is a binary tree in which each internal node x stores an element such that the element stored in the left subtree of x are less than or equal to x and elements stored in the right subtree of x are greater than or equal to x. This is called binary-search-tree property.

The Height of the Binary Search Tree equals the number of links from the root node to the deepest node.

Binary Searching
Searching a binary tree for a specific value can be a recursive or iterative process. This explanation covers a recursive method. We begin by examining the root node. If the tree is null, the value we are searching for does not exist in the tree. Otherwise, if the value equals the root, the search is successful. If the value is less than the root, search the left subtree. Similarly, if it is greater than the root, search the right subtree. This process is repeated until the value is found or the indicated subtree is null. If the searched value is not found before a null subtree is reached, then the item must not be present in the tree.

Insertion into Binary tree


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 tthe 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.

Prepared By Prof. Amit P. Patil RCP Institute of Mgt. Research & Development patilamit03@gmail.com

Data Structure Notes

1. Insertion into an empty tree.

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

into

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.

Deletion into Binary Tree


Remove operation on binary search tree is more complicated, than add and search.

Prepared By Prof. Amit P. Patil RCP Institute of Mgt. Research & Development patilamit03@gmail.com

Data Structure Notes

There are Two possible cases to consider:


Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply remove it from the tree. Deleting a node with one child: Delete it and replace it with its child. Deleting a node with two children.

As with all binary trees, a node's in-order successor is the left-most child of its right subtree, and a node's in-order predecessor is the right-most child of its left subtree. In either case, this node will have zero or one children. Delete it according to one of the two simpler cases above.
1. Node to be removed has no children.

This case is quite simple. Algorithm sets corresponding link of the parent to NULL and disposes the node. Example. Remove -4 from a BST.

2. Node to be removed has one child. It this case, node is cut from the tree and algorithm links single child (with it's subtree) directly to the parent of the removed node. Example. Remove 18 from a BST.

Prepared By Prof. Amit P. Patil RCP Institute of Mgt. Research & Development patilamit03@gmail.com

Data Structure Notes

3. Node to be removed has two children.

Prepared By Prof. Amit P. Patil RCP Institute of Mgt. Research & Development patilamit03@gmail.com

Data Structure Notes

This is the most complex case. To solve it, let us see one useful BST property first. We are going to use the idea, that the same set of values may be represented as different binary-search trees. For example those BSTs:

contains the same values {5, 19, 21, 25}. To transform first tree into second one, we can do following: 1. choose minimum element from the right subtree (19 in the example); 2. replace 5 by 19; 3. hang 5 as a left child. The same approach can be utilized to remove a node, which has two children: 4. find a minimum value in the right subtree; 5. replace value of the node to be removed with found minimum. Now, right subtree contains a duplicate! 6. apply remove to the right subtree to remove a duplicate. Notice, that the node with minimum value has no left child and, therefore, it's removal may result in first or second cases only.
1. Example. Remove 12 from a BST.

Prepared By Prof. Amit P. Patil RCP Institute of Mgt. Research & Development patilamit03@gmail.com

Data Structure Notes

Find minimum element in the right subtree of the node to be removed. In current example it is 19.

Replace 12 with 19. Notice, that only values are replaced, not nodes. Now we have two nodes with the same value.

Prepared By Prof. Amit P. Patil RCP Institute of Mgt. Research & Development patilamit03@gmail.com

Data Structure Notes

Remove 19 from the left subtree.

Prepared By Prof. Amit P. Patil RCP Institute of Mgt. Research & Development patilamit03@gmail.com

S-ar putea să vă placă și