Package dsa.lab06.solutions
Class BinaryTree<Item>
java.lang.Object
dsa.lab06.solutions.BinaryTree<Item>
- Type Parameters:
- Item- the item type
A binary tree.
 
This implementation is designed for binary trees that are built bottom-up. It will work in other cases, just less efficiently.
- 
Nested Class SummaryNested ClassesModifier and TypeClassDescriptionstatic enumstatic classA node in a binary tree.static class
- 
Constructor SummaryConstructorsConstructorDescriptionConstruct an empty binary tree.BinaryTree(Item... items) Construct a binary tree containing the given items.BinaryTree(Iterable<Item> items) Construct a binary tree containing the given items.BinaryTree(Iterable<Item> items, int size) Construct a binary tree containing the given items more efficiently thanBinaryTree(Iterable).
- 
Method SummaryModifier and TypeMethodDescriptionstatic <Item> BinaryTree<Item> buildInOrder(Item... items) static <Item> BinaryTree<Item> buildInOrder(Iterable<Item> items) static <Item> BinaryTree<Item> buildInOrder(Iterable<Item> items, int size) intheight()Get the number of levels below the root of the tree.inOrder()voidinsertRoot(BinaryTree.Node<Item> root) Insert the given node as the root.booleanisEmpty()Check if it's empty.items()Get an iterable that yields each item once.preOrder()voidvoidvoidRemove and return the root.root()Get the root node.intsize()Get the number of contained items.toString()Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.IterableforEach, spliterator
- 
Constructor Details- 
BinaryTreepublic BinaryTree()Construct an empty binary tree.
- 
BinaryTreeConstruct a binary tree containing the given items.- Parameters:
- items- the items
 
- 
BinaryTreeConstruct a binary tree containing the given items more efficiently thanBinaryTree(Iterable).- Parameters:
- items- the items
- size- the number of items
- Throws:
- IllegalArgumentException- if- size!=- n(where- nis- items's size)
 
- 
BinaryTreeConstruct a binary tree containing the given items.- Parameters:
- items- the items
 
 
- 
- 
Method Details- 
buildInOrder
- 
buildInOrder
- 
buildInOrder
- 
rootGet the root node.If there is none (i.e. it is empty), returns null.- Returns:
- the root node
 
- 
insertRootpublic void insertRoot(BinaryTree.Node<Item> root) throws IllegalStateException, IllegalArgumentException Insert the given node as the root.- Parameters:
- root- the new root
- Throws:
- IllegalStateException- if there's already a root, or
- IllegalArgumentException- if the one given already has a parent or is in a different tree
 
- 
removeRootRemove and return the root.- Returns:
- the old root
- Throws:
- NoSuchElementException- if there is no root
 
- 
heightpublic int height()Get the number of levels below the root of the tree.If the tree is empty, returns -1, otherwise returns a non-negative integer.- Returns:
- the height
 
- 
sizepublic int size()Description copied from interface:ContainerGet the number of contained items.
- 
isEmptypublic boolean isEmpty()Description copied from interface:ContainerCheck if it's empty.
- 
printPreOrderpublic void printPreOrder()
- 
printInOrderpublic void printInOrder()
- 
printPostOrderpublic void printPostOrder()
- 
toString
- 
preOrderNodes
- 
inOrderNodes
- 
postOrderNodes
- 
reversePreOrderNodes
- 
reverseInOrderNodes
- 
reversePostOrderNodes
- 
preOrder
- 
inOrder
- 
postOrder
- 
reversePreOrder
- 
reverseInOrder
- 
reversePostOrder
- 
itemsDescription copied from interface:ContainerGet an iterable that yields each item once.
 
-