/**
   * Gets the smallest item in the tree.
   *
   * @return Smallest item in the tree
   */
  public Comparable getMin() {
    Node node = rootNode;
    if (node == null) return null;

    while (node.getLeft() != null) node = node.getLeft();

    splay(node.getValue());

    return node.getValue();
  }
 private Node rotateLeft(Node node) {
   Node temp = node;
   Node nodeRight = temp.getRight();
   Node nodeLeft = temp.getLeft();
   Node w = nodeRight.getLeft();
   Node x = nodeRight.getRight();
   temp = new Node(temp.getData(), nodeLeft, w);
   nodeRight = new Node(nodeRight.getData(), temp, x);
   return nodeRight;
 }
 private Node rotateRight(Node node) {
   Node temp = node;
   Node tempLeft = temp.getLeft();
   Node tempRight = temp.getRight();
   Node w = tempLeft.getLeft();
   Node x = tempLeft.getRight();
   temp = new Node(temp.getData(), x, tempRight);
   tempLeft = new Node(tempLeft.getData(), w, temp);
   return tempLeft;
 }
 private Node insert(Node node, int data) {
   if (node == null) {
     return new Node(data);
   }
   if (node.getData() < data) {
     node = new Node(node.getData(), node.getLeft(), insert(node.getRight(), data));
   } else if (node.getData() > data) {
     node = new Node(node.getData(), insert(node.getLeft(), data), node.getRight());
   }
   return checkingBalancing(node);
 }
 private void postorderPrinting(Node node) {
   if (node != null) {
     postorderPrinting(node.getLeft());
     postorderPrinting(node.getRight());
     System.out.print(node.getData() + " ");
   }
 }
  /**
   * Removes the node with the value comp.
   *
   * @param comp Comparable being removed
   */
  public void remove(Comparable comp) {
    splay(comp);

    if (comp.compareTo(rootNode.getValue()) != 0) // Node not found
    return;

    if (rootNode.getLeft() == null) rootNode = rootNode.getRight();
    else {
      Node node = rootNode.getRight();
      rootNode = rootNode.getLeft();
      splay(comp);
      rootNode.setRight(node);
    }

    size--;
  }
 private int balanceNumber(Node node) {
   int lValue = depth(node.getLeft());
   int rValue = depth(node.getRight());
   if (lValue - rValue >= 2) return -1;
   else if (lValue - rValue <= -2) return 1;
   return 0;
 }
Example #8
0
 public void PreOrder(Node localRoot) {
   if (localRoot != null) {
     System.out.println(localRoot.getItem());
     PreOrder(localRoot.getLeft());
     PreOrder(localRoot.getRight());
   }
 }
  /**
   * Inserts a node into the SplayTree, then splays around that node.
   *
   * @param comp Comparable value of new node being added
   */
  public void insert(Comparable comp) {
    Node node = new Node(comp);
    if (rootNode == null && size == 0) {
      rootNode = node;
      return;
    }

    splay(comp);

    int temp = comp.compareTo(rootNode.getValue());
    if (temp == 0) // Double checks for duplicates
    return;

    if (temp < 0) {
      node.setLeft(rootNode.getLeft());
      node.setRight(rootNode);
      rootNode.setLeft(null);
    } else {
      node.setRight(rootNode.getRight());
      node.setLeft(rootNode);
      rootNode.setRight(null);
    }
    rootNode = node;
    size++;
  }
Example #10
0
  private Node rebalanceLeft(Node localRoot) {
    Node leftChild = localRoot.getLeft();
    if (leftChild.balance > Node.RIGHT_HEAVY) { // if it is a left-right Heavy tree
      Node leftRightChild = leftChild.getRight();
      if (leftRightChild.balance > Node.BALANCED) { // this if and else if are reversed for me
        localRoot.balance--;
        leftRightChild.balance--;
        leftChild.balance++;
      } else if (leftRightChild.balance < Node.BALANCED) {
        localRoot.balance--;
        leftRightChild.balance++;
        leftChild.balance++;
      } else {
        localRoot.balance = Node.BALANCED;
        leftRightChild.balance = Node.BALANCED;
        leftChild.balance = Node.BALANCED;
      }
      increase = false;
      decrease = true;

      rotateLeft(leftChild);
      return rebalanceLeft(rotateRight(localRoot)); // change this
    } else { // LEFT-LEFT
      increase = false;
      decrease = true;
    }
    // Now rotate the
    return rotateRight(localRoot);
  }
 public boolean search(int data) {
   Node local = root;
   while (local != null) {
     if (local.getData() > (data)) local = local.getLeft();
     else if (local.getData() == data) return true;
     else local = local.getRight();
   }
   return false;
 }
Example #12
0
  private Node rotateLeft(Node localRoot) {
    System.out.println("Rotating Left");

    Node temp = localRoot.getRight();
    localRoot.setRight(temp.getLeft());
    temp.setLeft(localRoot);

    //// hint this was only three lines that I took out.
    return temp;
  }
Example #13
0
  private Node rebalanceRight(
      Node
          localRoot) { // called from add when localRoot.balance is 2 (so right heavy) so rotate
                       // left

    // Obtain reference to right child
    Node rightChild = localRoot.getRight();
    if (rightChild.balance < Node.LEFT_HEAVY) { // See if right-left heavy
      // Obtain reference to right-left child
      Node rightLeftChild = rightChild.getLeft();
      /* Adjust the balances to be their new values after
      the rotates are performed.
      */
      if (rightLeftChild.balance > Node.BALANCED) {
        // if the rightLeftChild's balance is positive it's right heavy
        // but when you're done rotating the balance values will be different
        localRoot.balance--;
        rightLeftChild.balance--;
        rightChild.balance++;

      } else if (rightLeftChild.balance
          < Node.BALANCED) { // the rightLeftChild balance value is negative you have a
        // different issue where you must update the balance values
        localRoot.balance--;
        rightLeftChild.balance++;
        rightChild.balance++;

      } else { // The balance is 0 of the rightleftChild so all the nodes effected should go to zero
        localRoot.balance = Node.BALANCED;
        rightLeftChild.balance = Node.BALANCED;
        rightChild.balance = Node.BALANCED;
      }
      /**
       * After the rotates the overall height will be reduced thus increase is now false, but
       * decrease is now true.
       */
      increase = false;
      decrease = true;
      // Perform double rotation
      rotateRight(rightChild);
      return rebalanceRight(rotateRight(rightChild));
    } else { // Right - Balanced (Rotations are still going to be done)
      /* After the rotate the rightChild (new root) will
      be left heavy, and the local root (new left child)
      will be right heavy. The overall height of the tree
      will not change, thus increase and decrease remain
      unchanged.
      */
      increase = false;
      decrease = true;
    }
    // Now rotate the
    System.out.println("You will always reach this when rebalance right is called");
    return rotateLeft(localRoot);
  }
Example #14
0
  private Node rotateRight(Node localRoot) { // make sure this and rotate left is correct
    System.out.println("Rotating Right");

    Node temp = localRoot.getLeft();
    localRoot.setLeft(temp.getRight());
    temp.setRight(localRoot);

    // There is where you set up your references to get the proper rotation
    // see hint in rotateLeft
    return temp;
  }
  /**
   * Makes the Node node the rootNode of the SplayTree.
   *
   * @param node Node to be splayed
   */
  public void splay(Comparable comp) {
    Node left = head;
    Node right = head;
    Node top = rootNode;
    Node temp;

    head.setLeft(null);
    head.setRight(null);

    while (true) {
      if (comp.compareTo(top.getValue()) < 0) {
        if (top.getLeft() == null) // exit
        break;

        if (comp.compareTo(top.getLeft().getValue()) < 0) { // Rotate right
          temp = top.getLeft();
          top.setLeft(temp.getRight());
          temp.setRight(top);
          top = temp;

          if (top.getLeft() == null) // exit
          break;
        }
        right.setLeft(top); // Links the right
        right = top;
        top = top.getLeft();
      } else if (comp.compareTo(top.getValue()) > 0) {
        if (top.getRight() == null) // exit
        break;

        if (comp.compareTo(top.getRight().getValue()) > 0) { // Rotate left
          temp = top.getRight();
          top.setRight(temp.getLeft());
          temp.setLeft(top);
          top = temp;

          if (top.getRight() == null) // exit
          break;
        }
        left.setRight(top); // Links the left
        left = top;
        top = top.getRight();
      } else break; // exit
    }
    left.setRight(top.getLeft());
    right.setLeft(top.getRight());
    top.setLeft(head.getRight());
    top.setRight(head.getLeft());
    rootNode = top;
  }
  private static <T extends Comparable<?>> void printNodeInternal(
      List<Node> nodes, int level, int maxLevel) {
    if (nodes.isEmpty() || BTreePrinter.isAllElementsNull(nodes)) return;

    int floor = maxLevel - level;
    int endgeLines = (int) Math.pow(2, (Math.max(floor - 1, 0)));
    int firstSpaces = (int) Math.pow(2, (floor)) - 1;
    int betweenSpaces = (int) Math.pow(2, (floor + 1)) - 1;

    BTreePrinter.printWhitespaces(firstSpaces);

    List<Node> newNodes = new ArrayList<Node>();
    for (Node node : nodes) {
      if (node != null) {
        System.out.print(node.getValue());
        newNodes.add(node.getLeft());
        newNodes.add(node.getRight());
      } else {
        newNodes.add(null);
        newNodes.add(null);
        System.out.print(" ");
      }

      BTreePrinter.printWhitespaces(betweenSpaces);
    }
    System.out.println("");

    for (int i = 1; i <= endgeLines; i++) {
      for (int j = 0; j < nodes.size(); j++) {
        BTreePrinter.printWhitespaces(firstSpaces - i);
        if (nodes.get(j) == null) {
          BTreePrinter.printWhitespaces(endgeLines + endgeLines + i + 1);
          continue;
        }

        if (nodes.get(j).getLeft() != null) System.out.print("/");
        else BTreePrinter.printWhitespaces(1);

        BTreePrinter.printWhitespaces(i + i - 1);

        if (nodes.get(j).getRight() != null) System.out.print("\\");
        else BTreePrinter.printWhitespaces(1);

        BTreePrinter.printWhitespaces(endgeLines + endgeLines - i);
      }

      System.out.println("");
    }

    printNodeInternal(newNodes, level + 1, maxLevel);
  }
 public void printTree() {
   root.setLevel(0);
   Queue<Node> q = new LinkedList<Node>();
   q.add(root);
   while (!q.isEmpty()) {
     Node node = q.poll();
     System.out.println(node);
     int level = node.getLevel();
     Node left = node.getLeft();
     Node right = node.getRight();
     if (left != null) {
       left.setLevel(level + 1);
       q.add(left);
     }
     if (right != null) {
       right.setLevel(level + 1);
       q.add(right);
     }
   }
 }
Example #18
0
  private Node add(Node localRoot, int item) {

    if (localRoot == null) {
      addReturn = true;
      increase = true;
      System.out.println("Added " + item);
      return new Node(item);
    }
    System.out.println("Add called with " + localRoot.getItem() + " as the root.");
    if (item == localRoot.getItem()) { // item is alreday in tree
      increase = false;
      addReturn = false;
      return localRoot;
    } else if (item < localRoot.getItem()) {
      System.out.println("Branching left"); // item < data
      localRoot.setLeft(add(localRoot.getLeft(), item));

      if (increase) {
        decrementBalance(localRoot);
        if (localRoot.balance < Node.LEFT_HEAVY) {
          increase = false;
          return rebalanceLeft(localRoot);
        }
      }
      return localRoot; // Rebalance not needed.
    } else {
      System.out.println("Branching right"); // item > data
      localRoot.setRight(add(localRoot.getRight(), item));
      if (increase) {
        incrementBalance(localRoot);
        if (localRoot.balance > Node.RIGHT_HEAVY) {
          return rebalanceRight(localRoot);
        } else {
          return localRoot;
        }
      } else {
        return localRoot;
      }
    }
  }
  private static <T extends Comparable<?>> int maxLevel(Node node) {
    if (node == null) return 0;

    return Math.max(BTreePrinter.maxLevel(node.getLeft()), BTreePrinter.maxLevel(node.getRight()))
        + 1;
  }