Package nodes

Examples of nodes.Node


   * @see Node
   * @deprecated
   */
  public BinarySearchTree()
  {
    root = new Node();
  }


   * @param data
   *            the data to set in the root node
   */
  public BinarySearchTree(int data)
  {
    root = new Node(data);
  }

   * @param root
   *            the node in which the method checks at the moment
   */
  private void removeMin(Node root)
  {
    Node current = root;

    if (current.getRight().getLeft() == null)
    {
      current.setRight(current.getRight().getRight());
    }
    else
    {
      current = current.getRight();

      do
      {
        if (current.getLeft().getLeft() != null)
        {
          current = current.getLeft();
        }
        else
        {
          current.setLeft(current.getLeft().getLeft());
          break;
        }
      }
      while (current.getLeft() != null);
    }
  }

    if (this.root.getData() == root.getData())
    {
      return null;
    }

    Node current = this.root;

    while (current != null)
    {
      if (current.getLeft() != null)
      {
        if (current.getLeft() == root)
        {
          return current;
        }
      }
      if (current.getRight() == root)
      {
        return current;
      }
      if (root.getData().compareTo(current.getData()) <= 0)
      {
        current = current.getLeft();
      }
      else
      {
        current = current.getRight();
      }
    }

    throw new ItemNotFoundException("The item doesn't exists in the tree.");
  }

        remove(root.getRight(), value);
      }
      // value found - check if has 2 branches
      else if (root.getRight() != null && root.getLeft() != null)
      {
        Node min = minValue(root.getRight());

        removeMin(root);

        min.setLeft(root.getLeft());
        min.setRight(root.getRight());

        Node ancesstor = findPredecessor(root);
        if (ancesstor == null)
        {
          this.root = min;
        }
        else if (root == ancesstor.getLeft())
        {
          ancesstor.setLeft(min);
        }
        else
        {
          ancesstor.setRight(min);
        }
      }
      else
      // if has 1 branch or less
      {
        // find the father of the node to delete
        Node ancesstor = findPredecessor(root);
        // if the value to delete is in the root
        if (ancesstor == null)
        {
          if (root.getRight() != null || root.getLeft() != null)
          {
            this.setRoot(root.getLeft() == null ? root.getRight()
                : root.getLeft());
          }
          else
          {
            throw new ItemNotFoundException(
                "Can not remove the only node in the tree. "
                    + "A binary search tree must have at least 1 value.");
          }
        } // if not root check if the father's left is not null
        else if (ancesstor.getLeft() != null)
        { // if not null, check if to be deleted
          if (ancesstor.getLeft().getData().compareTo(value) == 0)
          { // if to be deleted replace the target by the son
            ancesstor.setLeft(root.getRight() == null ? root
                .getLeft() : root.getRight());
          }
          else
          {
            ancesstor.setRight(root.getRight() == null ? root
                .getLeft() : root.getRight());
          }
        }
        else
        {
          if (ancesstor.getRight().getData().compareTo(value) == 0)
          { // if to be deleted replace the target by the son
            ancesstor.setRight(root.getRight() == null ? root
                .getLeft() : root.getRight());
          }
          else
          {
            ancesstor.setLeft(root.getRight() == null ? root
                .getLeft() : root.getRight());
          }
        }
      }
    }

   *            the value to insert to the tree.
   * @throws DuplicateItemException
   */
  public void insert(Comparable value) throws DuplicateItemException
  {
    insert(root, new Node(value));
  }

  /**
   * This is an iterative version to the in-order traversal.
   */
  public void inorderIterative()
  {
    Node current = this.root;
    Stack<Node> stack = new Stack<Node>();

    while (stack.isEmpty() == false || current != null)
    {
      if (current != null)
      {
        stack.push(current);
        current = current.getLeft();
      }
      else
      {
        current = stack.pop();
        System.out.print(current.getData() + " ");
        current = current.getRight();
      }
    }
  }

   * <p>
   * Prints the whole tree by printing the nodes in a in-order manner
   */
  public String toString()
  {
    Node current = this.root;
    Stack<Node> stack = new Stack<Node>();
    String resultString = "";

    while (stack.isEmpty() == false || current != null)
    {
      if (current != null)
      {
        stack.push(current);
        current = current.getLeft();
      }
      else
      {
        current = stack.pop();
        resultString += current.toString();
        current = current.getRight();
      }
    }

    return resultString;
  }

TOP

Related Classes of nodes.Node

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.