Package dsa.lab07.solutions
Class BinarySearchTree<Key extends Comparable<Key>,Value>
java.lang.Object
dsa.lab07.solutions.BinarySearchTree<Key,Value>
- Type Parameters:
Key- the key typeValue- the value type
- All Implemented Interfaces:
Container<MapItem<Key,,Value>> Map<Key,,Value> OrderedMap<Key,,Value> Iterable<MapItem<Key,Value>>
public class BinarySearchTree<Key extends Comparable<Key>,Value>
extends Object
implements OrderedMap<Key,Value>
A binary search tree.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBinarySearchTree.ItemIterator<Key extends Comparable<Key>,Value> protected static classBinarySearchTree.Node<Key extends Comparable<Key>,Value> A node in a binary search tree. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BinarySearchTree.Node<Key, Value> The root of the BST.protected intThe number of nodes in the BST. -
Constructor Summary
ConstructorsConstructorDescriptionConstruct an empty binary search tree.BinarySearchTree(DynamicArray<MapItem<Key, Value>> items) Construct a binary search tree containing the given items.BinarySearchTree(MapItem<Key, Value>... items) Construct a binary search tree containing the given items.BinarySearchTree(Iterable<MapItem<Key, Value>> items) Construct a binary search tree containing the given items.BinarySearchTree(Iterable<MapItem<Key, Value>> items, int size) Construct a binary search tree containing the given items more efficiently thanBinarySearchTree(Iterable). -
Method Summary
Modifier and TypeMethodDescriptionFind the item with the given key.protected BinarySearchTree.Node<Key, Value> Find the node containing the item with the given key.voidInsert the given item.items()Get a forward iterable that yields each item once.max()Get the item with the greatest key.min()Get the item with the least key.Get the item that would be the successor of one with the given key.Get the item that would be the predecessor of one with the given key.Remove and return the item with the given key.reversed()Get a reverse iterable that yields each item once.intsize()Get the number of contained items.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface dsa.lab04.base.Map
containsKey, containsValue, get, insert, valuesMethods inherited from interface dsa.lab05.base.OrderedMap
keys, reversedKeys
-
Field Details
-
root
The root of the BST. Null if empty. -
size
protected int sizeThe number of nodes in the BST.
-
-
Constructor Details
-
BinarySearchTree
public BinarySearchTree()Construct an empty binary search tree. -
BinarySearchTree
Construct a binary search tree containing the given items.- Parameters:
items- the items
-
BinarySearchTree
Construct a binary search tree containing the given items more efficiently thanBinarySearchTree(Iterable).- Parameters:
items- the items
-
BinarySearchTree
Construct a binary search tree containing the given items.- Parameters:
items- the items
-
BinarySearchTree
Construct a binary search tree containing the given items.- Parameters:
items- the items
-
-
Method Details
-
size
public int size()Description copied from interface:ContainerGet the number of contained items.- Specified by:
sizein interfaceContainer<Key extends Comparable<Key>>- Returns:
- the size
-
findNode
Find the node containing the item with the given key.- Parameters:
key- the key- Returns:
- the node
- Throws:
NoSuchElementException- if no node has keykey
-
find
Description copied from interface:MapFind the item with the given key.- Specified by:
findin interfaceMap<Key extends Comparable<Key>,Value> - Parameters:
key- the item's key- Returns:
- the item
- Throws:
NoSuchElementException- if no item has keykey
-
insert
Description copied from interface:MapInsert the given item.If there's already an item with the same key, that item is replaced with this one.
This means that if the key was not already contained, the size is incremented, otherwise it isn't.
-
remove
Description copied from interface:MapRemove and return the item with the given key.Decrements the size (assuming the item was actually in the map).
- Specified by:
removein interfaceMap<Key extends Comparable<Key>,Value> - Parameters:
key- the item's key- Returns:
- the item
- Throws:
NoSuchElementException- ifkeyis not contained
-
previous
Description copied from interface:OrderedMapGet the item that would be the predecessor of one with the given key.The map may or may not contain an item with the given key.
- Specified by:
previousin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Parameters:
key- a key- Returns:
- the previous item (by key), or
nullif there is none
-
next
Description copied from interface:OrderedMapGet the item that would be the successor of one with the given key.The map may or may not contain an item with the given key.
- Specified by:
nextin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Parameters:
key- a key- Returns:
- the next item (by key), or
nullif there is none
-
min
Description copied from interface:OrderedMapGet the item with the least key.- Specified by:
minin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Returns:
- the minimum item (by key), or
nullif there is none
-
max
Description copied from interface:OrderedMapGet the item with the greatest key.- Specified by:
maxin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Returns:
- the maximum item (by key), or
nullif there is none
-
toString
-
items
Description copied from interface:OrderedMapGet a forward iterable that yields each item once.The items are iterated over in least-to-greatest order (by key).
- Specified by:
itemsin interfaceContainer<Key extends Comparable<Key>>- Specified by:
itemsin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Returns:
- an iterable over the items
-
reversed
Description copied from interface:OrderedMapGet a reverse iterable that yields each item once.The items are iterated over in greatest-to-least order (by key).
- Specified by:
reversedin interfaceOrderedMap<Key extends Comparable<Key>,Value> - Returns:
- an iterable over the items
-