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 Summary
ConstructorsConstructorDescriptionNode(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 Summary
Modifier 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
-
Node
Construct a binary node with no parent nor children.- Parameters:
tree- the containing treeitem- the contained item
-
Node
Construct a binary node with no parent and only a left child.- Parameters:
tree- the containing treeleft- the left nodeitem- the contained item
-
Node
Construct a binary node with no parent and only a right child.- Parameters:
tree- the containing treeitem- the contained itemright- 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 treeleft- the left nodeitem- the contained itemright- the right node
-
-
Method Details
-
parent
Get the parent node.- Returns:
- the parent
-
tree
Get the containing binary tree.- Returns:
- the tree
-
left
Get the left child node.- Returns:
- the left node
-
item
Get the contained item.- Returns:
- the item
-
right
Get the right child node.- Returns:
- the right node
-
setItem
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 childIllegalArgumentException- 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 childIllegalArgumentException- if the one given already has a parent or is in a different tree
-
removeLeft
Remove and return the left child.- Returns:
- the old left child
- Throws:
NoSuchElementException- if there is no left child
-
removeRight
Remove and return the right child.- Returns:
- the old right child
- Throws:
NoSuchElementException- if there is no right child
-
toString
-