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 Traversing
- Pre-order traversal.
- Post-order traversal.
- In-order traversal.
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
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
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
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.