//recursively calls insert down the tree until it find an open spot public void Insert(int id, object value) { bool insertRightNode = id >= nodeID; //if the value passed in is greater or equal to the data then insert to right node if (insertRightNode) { //if right child node is null create one if (rightNode == null) { rightNode = new BSTNode(id, value); } else {//if right node is not null recursivly call insert on the right node rightNode.Insert(id, value); } } else { //if the value passed in is less than the data then insert to left node if (leftNode == null) { //if the leftnode is null then create a new node leftNode = new BSTNode(id, value); } else {//if the left node is not null then recursively call insert on the left node leftNode.Insert(id, value); } } }
//O(Log n) public void Insert(int id, object data) { //if the root is not null then we call the Insert method on the root node if (root != null) { root.Insert(id, data); } else {//if the root is null then we set the root to be a new node based on the data passed in root = new BSTNode(id, data); } }