Package dsa.lab06.exercises
Class BinaryTree<Item>
java.lang.Object
dsa.lab06.exercises.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 Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
static class
A node in a binary tree.static class
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct 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 Summary
Modifier 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) int
height()
Get the number of levels below the root of the tree.inOrder()
void
insertRoot
(BinaryTree.Node<Item> root) Insert the given node as the root.boolean
isEmpty()
Check if it's empty.items()
Get an iterable that yields each item once.preOrder()
void
void
void
Remove and return the root.root()
Get the root node.int
size()
Get the number of contained items.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
BinaryTree
public BinaryTree()Construct an empty binary tree. -
BinaryTree
Construct a binary tree containing the given items.- Parameters:
items
- the items
-
BinaryTree
Construct a binary tree containing the given items more efficiently thanBinaryTree(Iterable)
.- Parameters:
items
- the itemssize
- the number of items- Throws:
IllegalArgumentException
- ifsize
!=n
(wheren
isitems
's size)
-
BinaryTree
Construct a binary tree containing the given items.- Parameters:
items
- the items
-
-
Method Details
-
buildInOrder
-
buildInOrder
-
buildInOrder
-
root
Get the root node.If there is none (i.e. it is empty), returns
null
.- Returns:
- the root node
-
insertRoot
public 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, orIllegalArgumentException
- if the one given already has a parent or is in a different tree
-
removeRoot
Remove and return the root.- Returns:
- the old root
- Throws:
NoSuchElementException
- if there is no root
-
height
public 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
-
size
public int size()Description copied from interface:Container
Get the number of contained items. -
isEmpty
public boolean isEmpty()Description copied from interface:Container
Check if it's empty. -
printPreOrder
public void printPreOrder() -
printInOrder
public void printInOrder() -
printPostOrder
public void printPostOrder() -
toString
-
preOrderNodes
-
inOrderNodes
-
postOrderNodes
-
reversePreOrderNodes
-
reverseInOrderNodes
-
reversePostOrderNodes
-
preOrder
-
inOrder
-
postOrder
-
reversePreOrder
-
reverseInOrder
-
reversePostOrder
-
items
Description copied from interface:Container
Get an iterable that yields each item once.
-