Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. public void add(T element) {
  2.         if(size == 0) {
  3.             root = new Node<T>(element); //if the tree is empty, element = root
  4.             size++;
  5.         return;
  6.         }
  7.             if(contains(element)) {
  8.                 return;
  9.             }
  10.             Node<T> currentNode = root;
  11.             int temp = currentNode.data.compareTo(element);
  12.             while(currentNode != null) {
  13.                 if(temp == -1) { //currentNode < element
  14.                 if(currentNode.right == null) {
  15.                         currentNode.right = new Node<T>(element); //this is where the element is put in the tree (if the tree was not empty)
  16.                     size++;
  17.                     break; //går ut ur while-loopen
  18.                 }
  19.                     currentNode = currentNode.right;
  20.                 }
  21.                     if(temp == 1) { //currentNode > element
  22.                         if(currentNode.left == null) {
  23.                         currentNode.left = new Node<T>(element); //this is where the element is put in the tree (if the tree was not empty)
  24.                     size++;
  25.                     break;
  26.                 }
  27.             currentNode = currentNode.left;
  28.                 }
  29.                 temp = currentNode.data.compareTo(element);
  30.             }
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement