Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void someMethod(T newEntry) {
- /*
- * Setting up the necessary variables
- */
- BinaryNode<T> currentNode = getRootNode();
- BinaryNode<T> previousNode = getRootNode();
- BinaryNode<T> newNode = new BinaryNode<T>(newEntry);
- /*
- * Searching for the end of the binary search tree.
- *
- * Current will first be tested to see if it's null. Then previousNode is going to keep track of currentNode's position.
- * Next step was to see where the currentNode will head next left or right using the compareTo method from Comparable.
- * If the currentNode.data is greater than or equal to the incoming newEntry (some positive Integer or zero respectively),
- * then the currentNode should look at the left child and right child otherwise (some negative Integer).
- *
- * The while loop exits after currentNode is set to NULL. After this the currentNode is set back to the previousNode.
- *
- * Finally it's the matter of comparing the currentData and the newEntry to see whether to add to the left or right.
- *
- * Side note: reversing the compareTo variables might have been easier to understand in hindsight.
- */
- while (currentNode != null) {
- previousNode = currentNode;
- if (currentNode.getData().compareTo(newEntry) >= 0) {
- currentNode = currentNode.getLeftChild();
- } else {
- currentNode = currentNode.getRightChild();
- }
- }
- /*
- * The while loop exits after currentNode is set to NULL. After this the currentNode is set back to the previousNode.
- *
- * Finally it's the matter of comparing the currentData and the newEntry to see whether to add to the left or right.
- *
- * Side note: reversing the compareTo variables might have been easier to understand in hindsight.
- */
- currentNode = previousNode;
- if (currentNode.getData().compareTo(newEntry) >= 0) {
- currentNode.setLeftChild(newNode);
- } else {
- currentNode.setRightChild(newNode);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement