Binary Search Trees Data Structure

0
1226

Binary Search Trees Data Structure

A tree is called a binary search tree if all the nodes contain some information (key) and have the following properties:

  • The key of a node is greater than or equal to the key of all children on its left;
  • The key of a node is smaller than all the key of all its right children.

A balanced binary search tree is a tree in which every node has the same number of descendants to its left and right.

The following is a (non-balanced) binary search tree:

Binary Search Trees 1

As their name suggest, binary search trees are particularly useful for searching for an element in a data collection. The complexity of this operation is log(N) because of the way the nodes are arranged in a binary search tree. Let’s now look at some of the basic operations that can be performed using this data structure.

  1. Insertion can be performed with the following steps:
    1. Start with the root. Consider the root is the current node.
    2. Compare the key of the current node with the key of the node to be inserted.
      1. If the key of the node to be inserted is smaller than or equal to the key of the current node go left.
      2. If the key of the node to be inserted is greater than the key of the current node go right.
    3. Repeat step b for all the nodes until a free space is found.
    4. Place the element in the free space.

For example, if we were inserting ‘11’ in the binary search tree above, after the insertion the tree would look as follows:

Binary Search Trees 2

We start with the root and compare 10 to 11. 11 > 10, so we move right. Then 11 < 12, so we move left, where a free space is found. The element is then inserted at the free space.

 

  1. Searching for an element in the tree is done in a similar fashion. We start at the root and at each step we compare the key of the current node with the value we are looking for, moving left and right according to the result of this comparison. For example, searching for ‘8’ in the tree above would only take 2 steps, as we move left and right from the root. The value we are looking for is not in the tree if there is nowhere to go. For example, if we were looking for ‘13’ we would go from 10 to 12 and then stop because there is nothing on the right.
  2. The deletion operation needs to take into account the following cases:
    1. if the node we are trying to delete is a leaf, then just mark it as ‘NULL’ (delete it).
    2. if the node has only one direct child then delete it and bring the child to its original location. For example, deleting ‘12’ in the tree above only requires replacing the node with ‘11’.
    3. if the node has two children things become a little more complicated:
      1. We firstly delete the information in the node, but keep the node.
      2. Place the largest value in the left sub-tree in the node whose information we have just deleted.
  • Delete the node containing the largest value in the left sub-tree.

Let’s see what happens if we are trying to delete ‘10’ from the tree given as an example. We firstly delete ‘10’, but keep the node, as follows:

Binary Search Trees  3

Then we notice the largest value in the left sub-tree is 8. Generally, this can easily be determined, because we only need to go right in the left sub-tree to find the largest value. We then place 8 in the empty space and delete the node that contained it initially. The final tree is as follows:

Binary Search Trees 4

There are three ways of traversing a binary search tree. We will list them and give an example based on our initial tree from above (before deletion):

Binary Search Trees 5

  • Inorder traversal – recursively list the key of the node in the order left – root – right. For example, in the tree above we start from the root, move left and repeat the process for the left sub-tree (write down the left part of the root first – 5, then the root itself – 7 and then the right key – 8). The result is 5 7 8 10 11 12. Not that the inorder traversal of a binary search tree always results in the sorted keys, in ascending order.
  • Pre-order traversal: similar to inorder, but the order is now root – left – right. For the tree above we would obtain 10 7 5 8 12 11.
  • Post-order traversal: also similar to inorder, with the order left – right – root. For the tree above, this type of traversal gives 7 8 7 11 12 10. The root is always the last element.

Binary Search Tree Data Structure in Java 

http://java2novice.com/java-search-algorithms/binary-search/

http://www.tutorialspoint.com/java/util/arrays_binarysearch_int.htm

Binary Search Tree Data Structure in PHP

http://phpden.info/binary-search-trees-in-php

Binary Search Tree Data Structure in C++

http://www.cplusplus.com/forum/general/1551/

http://www.sanfoundry.com/cpp-program-implement-binary-tree-2/

http://www.algolist.net/Data_structures/Binary_search_tree

http://www.stoimen.com/blog/2012/06/22/computer-algorithms-binary-search-tree-data-structure/

Binary Search Tree Data Structure in Python

http://interactivepython.org/runestone/static/pythonds/Trees/SearchTreeImplementation.html

http://www.laurentluce.com/posts/binary-search-tree-library-in-python/

http://stackoverflow.com/questions/5444394/implementing-binary-search-tree-in-python

Binary Search Tree Data Structure in Ruby

http://zvkemp.github.io/blog/2014/04/25/binary-search-trees-in-ruby/

http://www.thelearningpoint.net/computer-science/basic-data-structures-in-ruby—binary-search-tre

http://codereview.stackexchange.com/questions/67404/binary-search-tree-in-ruby

SHARE
Previous articleRooted Trees Data Structure
Next articleRED-BLACK Tree Data Structure
Since last 15 years in different geographical locations, Sumit prepared hiring format for several hiring managers/teams to hire the balanced talents and interviewed talents on the various stages of their selection process. He also interviewed by hundreds of companies in different geographical locations.His best conclusion for hiring teams and candidate is to prepare in advance. Here ‘advance’ means keep your interview book ready and continue to update it even you are not going to interview candidates or applying for any job in next six months.