Wednesday 26 June 2013

Algorithm for constructing a BinarySearchTree

Step 1: Creating Varibales
1.1: Create a integer variable to store the value
1.2: Create two references for the same class to point to leftNode and rightNode
Step 2: Initializing constructor
2.1: Create a constructor which sets the value for a node and make rightNode as null and leftNode as
null
Step 3: Mapping the node
3.1: If this the the first node then mapping is not required
3.2: If already node is available get value of the currentNode in the tree and compare with the value of
the new node
3.3: If the value is less than the currentNode check whether the leftNode is null or has any other node
3.3.1: If the leftNode has a value then perform 3.2 again
3.3.2: Else if the leftNode is null then insert the node here by pointing the null value to the
newNode
3.4: Else if the value is greater than the currentNode check whether the rightNode is null or has any
other node
3.4.1: If the rightNode has a value then perform 3.2 again
3.4.2: Else if the rightNode is null then insert the node here by pointing the null value to the
newNode

No comments:

Post a Comment