Package dsa.lab06.solutions
Class BinaryTree.Node<Item>
java.lang.Object
dsa.lab06.solutions.BinaryTree.Node<Item>
- Type Parameters:
- Item- the item type
- Enclosing class:
- BinaryTree<Item>
A node in a binary tree.
- 
Constructor SummaryConstructorsConstructorDescriptionNode(BinaryTree<Item> tree, BinaryTree.Node<Item> left, Item item) Construct a binary node with no parent and only a left child.Node(BinaryTree<Item> tree, BinaryTree.Node<Item> left, Item item, BinaryTree.Node<Item> right) Construct a binary node with no parent and both children.Node(BinaryTree<Item> tree, Item item) Construct a binary node with no parent nor children.Node(BinaryTree<Item> tree, Item item, BinaryTree.Node<Item> right) Construct a binary node with no parent and only a right child.
- 
Method SummaryModifier and TypeMethodDescriptionbooleanhasLeft()Check if it has a left child.booleanCheck if it has a parent (i.e.booleanhasRight()Check if it has a right child.intheight()Get the number of levels below.voidinsertLeft(BinaryTree.Node<Item> left) Insert the given node as the left child.voidinsertRight(BinaryTree.Node<Item> right) Insert the given node as the right child.booleanisLeaf()Check if it is a leaf (i.e.booleanisLeft()Check if it is a left child (i.e.booleanisRight()Check if it is a right child (i.e.booleanisRoot()Check if it is the root (i.e.item()Get the contained item.left()Get the left child node.intlevel()Get the number of levels above.parent()Get the parent node.voidPrint out the items in this subtree, "in-order".voidPrint out the items in this subtree, "post-order".voidPrint out the items in this subtree, "pre-order".Remove and return the left child.Remove and return the right child.right()Get the right child node.voidSet the contained item.intsize()Get the number of contained items.toString()tree()Get the containing binary tree.
- 
Constructor Details- 
NodeConstruct a binary node with no parent nor children.- Parameters:
- tree- the containing tree
- item- the contained item
 
- 
NodeConstruct a binary node with no parent and only a left child.- Parameters:
- tree- the containing tree
- left- the left node
- item- the contained item
 
- 
NodeConstruct a binary node with no parent and only a right child.- Parameters:
- tree- the containing tree
- item- the contained item
- right- the right node
 
- 
Nodepublic 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- 
parentGet the parent node.- Returns:
- the parent
 
- 
treeGet the containing binary tree.- Returns:
- the tree
 
- 
leftGet the left child node.- Returns:
- the left node
 
- 
itemGet the contained item.- Returns:
- the item
 
- 
rightGet the right child node.- Returns:
- the right node
 
- 
setItemSet the contained item.- Parameters:
- item- the new item
 
- 
hasParentpublic boolean hasParent()Check if it has a parent (i.e. isn't the root).- Returns:
- whether it has a parent
 
- 
hasLeftpublic boolean hasLeft()Check if it has a left child.- Returns:
- whether it has a left child
 
- 
hasRightpublic boolean hasRight()Check if it has a right child.- Returns:
- whether it has a right child
 
- 
isRootpublic boolean isRoot()Check if it is the root (i.e. doesn't have a parent).- Returns:
- whether it's the root
 
- 
isLeafpublic boolean isLeaf()Check if it is a leaf (i.e. doesn't have either child).- Returns:
- whether it's a leaf
 
- 
isLeftpublic 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
 
- 
isRightpublic 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
 
- 
sizepublic int size()Get the number of contained items.- Returns:
- the size
 
- 
heightpublic int height()Get the number of levels below.- Returns:
- the height
 
- 
levelpublic int level()Get the number of levels above.- Returns:
- the level
 
- 
printPreOrderpublic void printPreOrder()Print out the items in this subtree, "pre-order".Prints items using System.out.println(item), in the order "node-left-right".
- 
printInOrderpublic void printInOrder()Print out the items in this subtree, "in-order".Prints items using System.out.println(item), in the order "left-node-right".
- 
printPostOrderpublic void printPostOrder()Print out the items in this subtree, "post-order".Prints items using System.out.println(item), in the order "left-right-node".
- 
insertLeftpublic 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
 
- 
insertRightpublic 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
 
- 
removeLeftRemove and return the left child.- Returns:
- the old left child
- Throws:
- NoSuchElementException- if there is no left child
 
- 
removeRightRemove and return the right child.- Returns:
- the old right child
- Throws:
- NoSuchElementException- if there is no right child
 
- 
toString
 
-