Tuesday, March 3, 2009

What is a Search Tree?

Search Tree is based on the Binary Search Tree (BST) in which each node has a left and right child. All the childern to the left of the node with key values have smaller than the root node, all the childern to the right of the node have values greater than the root node.

For example, a sequence of numbers like 34, 54, 66, 12, 43, 28, 83 in a Search Tree is represented as follows:
Now, in order to search a given value i.e. 43 in this constructed tree. Start at the root node to search 43, this is done by making comparsion with the node value. When the value compared with the search value results to be greater, then move to the right node or vice versa. In this case, we move right because 43 is greater than 34.

From, now on just repeat the comparison procedure until you find a matching value or end the comparing procedure at a leaf node.

Continuing our search, we now compare 54 with 43, we notice 43 is less than 54, hence we move left and compare the leaf nodes value i.e. 43 with the search value i.e. 43, therefore 43 equals to 43... match found!

Advantages of such a search tree: Insertion, Removal, Search are performed efficiently. In particularly, when considering the searching iteration we reduce the number of elements to search by one-half.


No comments:

Post a Comment