Last active
December 11, 2015 20:39
-
-
Save gtke/4657214 to your computer and use it in GitHub Desktop.
BST Insertion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Adds a data entry to the BST | |
| * | |
| * null is positive infinity | |
| * | |
| * @param data The data entry to add | |
| */ | |
| public void add(T data) { | |
| root = add(data, root); | |
| } | |
| private Node<T> add(T data, Node<T> node){ | |
| if(node != null) { | |
| if(data == null){ | |
| node.right = add(data, node.right); | |
| }else{ | |
| if(node.getData() == null){ | |
| node = new Node<T>(data); | |
| node.right = add(null, node.right); | |
| } | |
| if(data.compareTo(node.data) < 0){ | |
| node.left = add(data, node.left); | |
| } | |
| if(data.compareTo(node.data) > 0){ | |
| node.right = add(data, node.right); | |
| } | |
| } | |
| } | |
| else{ | |
| node = new Node<T>(data); | |
| size++; | |
| } | |
| return node; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment