### Inserting a node into a Binary Search Tree

• Inserting a value into a Binary Search Tree

• Example: insert the value 10 into the following BST:

• Before the insertion:

• After the insertion:

• Notice that:

• The value 10 must use the node 9 as parent node

 If 10 is inserted under some other node, the resulting tree is not a Binary Search Tree !!!

Example:

• Note:

 We should not insert nodes containing same values into a Binary Search Tree

Example: insert the value 4 into the following BST

will not change the Binary Search Tree because the value 4 is already stored in the tree.

• Recall: the augmented search algorithm

• The augmented BST search algorithm findNode(x):

• If x is stored in the Binary Search Tree, findNode(x) will return the reference to the node containing the value x

• If x is not stored in the Binary Search Tree, findNode(x) will return the null

Further more:

 The variable myParent will point to the node that can be used as the parent node to store the value x

• A preliminary put(x) algorithm for a BST

• Pseudo code:

 ``` public void put(double x) { Node p; // Help variable /* ---------------------------------------------------- Find the node with search value == "x" in the BST ---------------------------------------------------- */ p = findNode(x); if ( x was found in the BST ) { return; // x is already there.... don't insert again } /* ------------------------------------------------- When program reaches here, x was NOT found To do: Insert a new node (x) under myParent !!! -------------------------------------------------- */ Node q = new Node( x ); // Create a new node with the value Attach q under the myParent node; } ```

• Java:

 ``` public void put(double x) { Node p; // Help variable /* ---------------------------------------------------- Find the node with search value == "x" in the BST ---------------------------------------------------- */ p = findNode(x); /* =============================================== findNode(x) returns non-null if x is found ! =============================================== */ if ( p != null ) { return; // x is already there.... don't insert again } /* ------------------------------------------------- When program reaches here, x was NOT found To do: Insert a new node (x) under myParent !!! -------------------------------------------------- */ Node q = new Node( x ); // Create a new node with the value /* ======================================================== Attach q under the myParent node; There are 2 possible way to add q: as left child or as right child ! ======================================================== */ if ( x < myParent.value ) myParent.left = q; // Add q as left child else myParent.right = q; // Add q as right child } ```

• Fixing the preliminary put(x) algorithm for a BST

• Fact:

 Just like the linked list, he have to check for special situatiosn where the general algorithm will fail to work. (And handle the special situations with if-statements)

• Consider the highlighted statement in the preliminary algorithm:

 ``` public void put(double x) { Node p; // Help variable /* ---------------------------------------------------- Find the node with search value == "x" in the BST ---------------------------------------------------- */ p = findNode(x); /* =============================================== findNode(x) returns non-null if x is found ! =============================================== */ if ( p != null ) { return; // x is already there.... don't insert again } /* ------------------------------------------------- When program reaches here, x was NOT found To do: Insert a new node (x) under myParent !!! -------------------------------------------------- */ Node q = new Node( x ); // Create a new node with the value /* ======================================================== Attach q under the myParent node; There are 2 possible way to add q: as left child or as right child ! ======================================================== */ if ( x < myParent.value ) myParent.left = q; // Add q as left child else myParent.right = q; // Add q as right child } ```

Note that:

• You can only use myParent.left and myParent.right if:

 myParent points to a Node object

• Therefore:

 The preliminary algorithm will fail if the Binary Search Tree is empty !!!

• The fixed node insertion algorithm:

 ``` public void put(double x) { Node p; // Help variable /* ------------------------------------------------- Handle the special case -------------------------------------------------- */ if ( root == null ) { // Insert into an empty BST root = new Node( x ); return; } /* ---------------------------------------------------- Find the node with search value == "x" in the BST ---------------------------------------------------- */ p = findNode(x); /* =============================================== findNode(x) returns non-null if x is found ! =============================================== */ if ( p != null ) { return; // x is already there.... don't insert again } /* ------------------------------------------------- When program reaches here, x was NOT found To do: Insert a new node (x) under myParent !!! -------------------------------------------------- */ Node q = new Node( x ); // Create a new node with the value /* ======================================================== Attach q under the myParent node; There are 2 possible way to add q: as left child or as right child ! ======================================================== */ if ( x < myParent.value ) myParent.left = q; // Add q as left child else myParent.right = q; // Add q as right child } ```

• Test program:

 ``` public static void main(String[] args) { Scanner in = new Scanner(System.in); BST x = new BST(); // Create an empty BST x.put( 50 ); // Insert away !!! x.put( 30 ); x.put( 70 ); x.put( 10 ); x.put( 5 ); x.put( 7 ); x.put( 40 ); x.put( 45 ); x.put( 80 ); } ```

Output: (the BST is printed side ways....)

 ```[50.0] ================================ [50.0] [30.0] ================================ [70.0] [50.0] [30.0] ================================ [70.0] [50.0] [30.0] [10.0] ================================ [70.0] [50.0] [30.0] [10.0] [5.0] ================================ [70.0] [50.0] [30.0] [10.0] [7.0] [5.0] ================================ [70.0] [50.0] [40.0] [30.0] [10.0] [7.0] [5.0] ================================ [70.0] [50.0] [45.0] [40.0] [30.0] [10.0] [7.0] [5.0] ================================ [80.0] [70.0] [50.0] [45.0] [40.0] [30.0] [10.0] [7.0] [5.0] ================================ ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac testProg.java To run:          java testProg