Skip to content

Instantly share code, notes, and snippets.

@gtke
Last active December 11, 2015 20:39
Show Gist options
  • Select an option

  • Save gtke/4657214 to your computer and use it in GitHub Desktop.

Select an option

Save gtke/4657214 to your computer and use it in GitHub Desktop.
BST Insertion
/**
* 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