What is binary search tree ?

  • The Binary search tree is a node-based on the binary tree data structure has the following properties,
    • The left-side sub tree of a node contains only nodes with keys lesser than the node’s key.
    • The right-side sub tree of a node contains only nodes with keys greater than the node’s key.
    • The left-side and right-side subtree each must also be a binary search tree.
Binary Search Tree

Binary Search Tree Traversing

Pre-order traversal

  • This traversal technique it may be traversal order is root-left-right.
    • Visit the node.
    • Call itself to traverse the node’s left subtree.
    • Call itself to traverse the node’s right subtree.

Algoithm

preOrder (t)
{
if (t is not empty)
{
access the root element of t;
preOrder (leftTree (t));
preOrder (rightTree (t));
} // if
} // preOrder traversal

Post-order traversal

  • This traversal technique the traversal order is left-right-root.
    • Call itself to traverse the node’s left subtree.
    • Call itself to traverse the node’s right subtree.
    • Visit the node.

Algoithm

postOrder (t)
{
if (t is not empty)
{
postOrder(leftTree(t));
postOrder(rightTree(t));
access the root element of t;
} // if
} // postOrder traversal

n-order traversal

  • An in-order traversal of a binary search tree will cause all the nodes to be visited in ascending order, based on their key values. If you want to create a sorted list of the data in a binary tree, this is one way to do it.
    • Call itself to traverse the node’s left subtree.
    • Visit the node.
    • Call itself to traverse the node’s right subtree

Algoithm

private void inOrder(node localRoot) 
{
if(localRoot != null)
{
inOrder(localRoot.leftChild);
localRoot.displayNode();
inOrder(localRoot.rightChild);
}
}

Binary Tree Traversing

Advantages

  • Binary Search Tree is fast in insertion and deletion etc when balanced.
  • It is very efficient and its code is easier than Linklists.

Disadvantages

  • The main disadvantage is that we should always implement a balanced binary search tree – AVL tree, Red-Black tree, Splay tree. Otherwise the cost of operations may not be logarithmic and degenerate into a linear search on an array.
  • Shape of the tree depends upon order of insertion and it can be degenerated.
  • Searching takes long time.

Categorized in:

Tagged in:

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,