Class BinaryTree.Node<Item>

java.lang.Object
dsa.lab06.exercises.BinaryTree.Node<Item>
Type Parameters:
Item - the item type
Enclosing class:
BinaryTree<Item>

public static class BinaryTree.Node<Item> extends Object
A node in a binary tree.
  • Constructor Details

    • Node

      public Node(BinaryTree<Item> tree, Item item)
      Construct a binary node with no parent nor children.
      Parameters:
      tree - the containing tree
      item - the contained item
    • Node

      public Node(BinaryTree<Item> tree, BinaryTree.Node<Item> left, Item item)
      Construct a binary node with no parent and only a left child.
      Parameters:
      tree - the containing tree
      left - the left node
      item - the contained item
    • Node

      public Node(BinaryTree<Item> tree, Item item, BinaryTree.Node<Item> right)
      Construct a binary node with no parent and only a right child.
      Parameters:
      tree - the containing tree
      item - the contained item
      right - the right node
    • Node

      public Node(BinaryTree<Item> tree, BinaryTree.Node<Item> left, Item item, BinaryTree.Node<Item> right)
      Construct a binary node with no parent and both children.
      Parameters:
      tree - the containing tree
      left - the left node
      item - the contained item
      right - the right node
  • Method Details

    • parent

      public BinaryTree.Node<Item> parent()
      Get the parent node.
      Returns:
      the parent
    • tree

      public BinaryTree<Item> tree()
      Get the containing binary tree.
      Returns:
      the tree
    • left

      public BinaryTree.Node<Item> left()
      Get the left child node.
      Returns:
      the left node
    • item

      public Item item()
      Get the contained item.
      Returns:
      the item
    • right

      public BinaryTree.Node<Item> right()
      Get the right child node.
      Returns:
      the right node
    • setItem

      public void setItem(Item item)
      Set the contained item.
      Parameters:
      item - the new item
    • hasParent

      public boolean hasParent()
      Check if it has a parent (i.e. isn't the root).
      Returns:
      whether it has a parent
    • hasLeft

      public boolean hasLeft()
      Check if it has a left child.
      Returns:
      whether it has a left child
    • hasRight

      public boolean hasRight()
      Check if it has a right child.
      Returns:
      whether it has a right child
    • isRoot

      public boolean isRoot()
      Check if it is the root (i.e. doesn't have a parent).
      Returns:
      whether it's the root
    • isLeaf

      public boolean isLeaf()
      Check if it is a leaf (i.e. doesn't have either child).
      Returns:
      whether it's a leaf
    • isLeft

      public boolean isLeft()
      Check if it is a left child (i.e. has a parent and is its left child).
      Returns:
      whether it's a left child
    • isRight

      public boolean isRight()
      Check if it is a right child (i.e. has a parent and is its right child).
      Returns:
      whether it's a right child
    • size

      public int size()
      Get the number of contained items.
      Returns:
      the size
    • height

      public int height()
      Get the number of levels below.
      Returns:
      the height
    • level

      public int level()
      Get the number of levels above.
      Returns:
      the level
    • printPreOrder

      public void printPreOrder()
      Print out the items in this subtree, "pre-order".

      Prints items using System.out.println(item), in the order "node-left-right".

    • printInOrder

      public void printInOrder()
      Print out the items in this subtree, "in-order".

      Prints items using System.out.println(item), in the order "left-node-right".

    • printPostOrder

      public void printPostOrder()
      Print out the items in this subtree, "post-order".

      Prints items using System.out.println(item), in the order "left-right-node".

    • insertLeft

      public void insertLeft(BinaryTree.Node<Item> left) throws IllegalStateException, IllegalArgumentException
      Insert the given node as the left child.
      Parameters:
      left - the new left child
      Throws:
      IllegalStateException - if there's already a left child
      IllegalArgumentException - if the one given already has a parent or is in a different tree
    • insertRight

      public void insertRight(BinaryTree.Node<Item> right) throws IllegalStateException, IllegalArgumentException
      Insert the given node as the right child.
      Parameters:
      right - the new right child
      Throws:
      IllegalStateException - if there's already a right child
      IllegalArgumentException - if the one given already has a parent or is in a different tree
    • removeLeft

      public BinaryTree.Node<Item> removeLeft() throws NoSuchElementException
      Remove and return the left child.
      Returns:
      the old left child
      Throws:
      NoSuchElementException - if there is no left child
    • removeRight

      public BinaryTree.Node<Item> removeRight() throws NoSuchElementException
      Remove and return the right child.
      Returns:
      the old right child
      Throws:
      NoSuchElementException - if there is no right child
    • toString

      public String toString()
      Overrides:
      toString in class Object