public void insertValue(int value) { if (rootNode == null) { rootNode = new IntNode(value); } else { IntNode currentNode = rootNode; while (currentNode.getValue() != value) { if (value > currentNode.getValue()) { // Right child path if (currentNode.getRightChild() == null) { currentNode.setRightChild(new IntNode(value)); } currentNode = currentNode.getRightChild(); } else { // Left child path if (currentNode.getLeftChild() == null) { currentNode.setLeftChild(new IntNode(value)); } currentNode = currentNode.getLeftChild(); } } } }
public boolean search(int value) { IntNode currentNode = rootNode; while (currentNode != null && (currentNode.getValue() != value)) { if (value > currentNode.getValue()) { // Continue down the right child path currentNode = currentNode.getRightChild(); } else { // Continue down the left child path currentNode = currentNode.getLeftChild(); } } // If the current node exists and its value is equal to the input, return true, else return // false return currentNode != null && (currentNode.getValue() == value); }