Ads

banner image

Binary Search Tree

A tree can be defined as an abstract model of a hierarchical structure; a family tree or an organizational chart is a typical example of a tree.





Note: 
    Binary search trees keep their keys in sorted order so that lookup and other operations can use the principle of binary search: when looking for a key in a tree (or a place to insert a new key), they traverse the tree from root to leaf, making comparisons to keys stored in the nodes of the tree and deciding, on the basis of the comparison, to continue searching in the left or right subtrees. On average, this means that each comparison allows the operations to skip about half of the tree so that each lookup, insertion or deletion takes time proportional to the logarithm of the number of items stored in the tree.

The major advantage of binary search trees over other data structures is that the related sorting algorithms and search algorithms such as in-order traversal can be very efficient; they are also easy to code.

  1. When inserting or searching for an element in a binary search tree, the key of each visited node has to be compared with the key of the element to be inserted or found.
  2. The shape of the binary search tree depends entirely on the order of insertions and deletions, and can become degenerate.


A degenerate tree is a tree where for each parent node, there is only one associated child node. It is unbalanced and, in the worst case, performance degrades to that of a linked list. If your add node function does not handle re-balancing, then you can easily construct a degenerate tree by feeding it with data that is already sorted. What this means is that in a performance measurement, the tree will essentially behave like a linked list data structure.

Time and space complexity of BST
AlgorithmAverageWorst case
SpaceO(n)O(n)
SearchO(log n)
O(n)
InsertO(log n)O(n)
DeleteO(log n)O(n)
Binary Search Tree Binary Search Tree Reviewed by Idowu Wasiu on December 31, 2019 Rating: 5

No comments:

Powered by Blogger.