Friday, January 16, 2015

How to insert data to Binary Search Tree (BST)

Binary search tree is one of most important data structure in programming.So lets see how to work with a binary search tree manually.
Main property of a binary search tree is all of the values in the left sub-tree are less than the value in the root and all of the values in the right sub-tree are greater than the root. 

when we enter a data set to a binary search tree.first value goes to the root.If next value is greater than root it is the right child of root. If next value is less than root it is the left child of root.

Lets enter this data set in to a binary search tree.
{ 8, 3, 6, 10, 1, 4, 7, 14, 13}

8 is the root of this BST. 3 is the left child of 8 (root) because 3 is less than 8. 10 is the right child of the 8 (root) because  10 is greater than 8. Next element of the array is 1. 1 goes to left sub-tree of 8 because 1 is less than 8 and it is the left child of the 3 because 1 is less than 3. So on after completing the BST it will looks like this.



Lets try another exercise.
{ 50, 72, 54, 17, 12, 23, 19, 14, 76, 9, 54, 67}
After entering this data set into BST it will look like this




2 comments:

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...