/**
   * 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++;
  }
Пример #2
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;
  }
Пример #3
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;
  }
  /**
   * 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--;
  }
  /**
   * 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;
  }
Пример #6
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;
      }
    }
  }