// Iterative put method public class BST, Value> { /* =========================== Node structure =========================== */ private class Node { private Key key; private Value val; private Node left; private Node right; public Node(Key key, Value val) { this.key = key; this.val = val; this.left = null; this.right = null; } } private Node root; // References the root node of the BST /* ================================================================ put(key, val): store the (key,val) pair into the BST 1. if the key "key" is found in the BST, we replace the val that is associated with the key "key" 1. if the key "key" is NOT found in the BST, we insert a new node containing (key, val) ================================================================ */ public void put(Key key, Value val) { Node curr_node; // Help variable /* ---------------------------------------------------------- Just like linked list, insert in an EMPTY BST must be taken care off separately by an if-statement ---------------------------------------------------------- */ if ( root == null ) { // Insert into an empty BST root = new Node( key, val ); return; } /* -------------------------------------------- Here: insert into a non-empty BST Find the node with key == "key" in the BST -------------------------------------------- */ curr_node = root; // Always start at the root node while ( curr_node != null ) { if ( key.compareTo( curr_node.key ) < 0 ) { if ( curr_node.left != null ) curr_node = curr_node.left; // Continue search in left subtree else break; // Stop, key is not in BST } else if ( key.compareTo( curr_node.key ) > 0 ) { if ( curr_node.right != null ) curr_node = curr_node.right; // Continue search in right subtree else break; // Stop, key is not in BST } else { // Found key in BST ==> replace vale curr_node.val = val; return; } } // "Key" Not found ===> Add (key, val) as a child of curr_node if ( key.compareTo( curr_node.key ) < 0 ) curr_node.left = new Node( key, val ); // Add it as left child else // key.compareTo( curr_node.key ) > 0 curr_node.right = new Node( key, val ); // Add it as left child } public Value get(Key key) { Node curr_node; // Help variable /* -------------------------------------------- Find the node with key == "key" in the BST -------------------------------------------- */ curr_node = root; // Always start at the root node while ( curr_node != null ) { if ( key.compareTo( curr_node.key ) < 0 ) { curr_node = curr_node.left; // Continue search in left subtree } else if ( key.compareTo( curr_node.key ) > 0 ) { curr_node = curr_node.right; // Continue search in right subtree } else { // key == curr_node.key, return the val in the node curr_node return curr_node.val; } } // "Key" Not found !! return null; } public void printnode(Node x, int h) { for (int i = 0; i < h; i++) System.out.print(" "); System.out.println("[" + x.key + "," + x.val + "]"); } void printBST() { showR( root, 0 ); System.out.println("================================"); } public void showR(Node t, int h) { if (t == null) return; showR(t.right, h+1); printnode(t, h); showR(t.left, h+1); } }