Package dsa.lab07.exercises
Class BinarySearchTree.Node<Key extends Comparable<Key>,Value>
java.lang.Object
dsa.lab07.exercises.BinarySearchTree.Node<Key,Value>
- Type Parameters:
Key
- the key typeValue
- the value type
- Enclosing class:
BinarySearchTree<Key extends Comparable<Key>,
Value>
A node in a binary search tree.
-
Field Summary
FieldsModifier and TypeFieldDescription -
Constructor Summary
ConstructorsConstructorDescriptionNode
(BinarySearchTree.Node<Key, Value> parent, BinarySearchTree<Key, Value> tree, BinarySearchTree.Node<Key, Value> left, MapItem<Key, Value> item, BinarySearchTree.Node<Key, Value> right) -
Method Summary
Modifier and TypeMethodDescriptionFind the node containing the item with the given key within this subtree.Insert the given item within this subtree.max()
Get the item in this subtree with the greatest key.maxNode()
Get the node in this subtree with the greatest key.min()
Get the item in this subtree with the least key.minNode()
Get the node in this subtree with the least key.Get the item within this subtree that would be the successor of one with the given key.Get the item within this subtree that would be the predecessor of one with the given key.remove()
Remove the item that this node contains from the tree, and return the node removed.
-
Field Details
-
parent
-
tree
-
left
-
item
-
right
-
-
Constructor Details
-
Node
public Node(BinarySearchTree.Node<Key, Value> parent, BinarySearchTree<Key, Value> tree, BinarySearchTree.Node<Key, Value> left, MapItem<Key, Value> item, BinarySearchTree.Node<Key, Value> right)
-
-
Method Details
-
findNode
Find the node containing the item with the given key within this subtree.- Parameters:
key
- the key- Returns:
- the node
- Throws:
NoSuchElementException
- if no node has keykey
-
insert
Insert the given item within this subtree.If there's already an item with the same key, that item is replaced with this one, and that node is returned.
If instead there isn't already an item with the same key, a new node is inserted and returned.
- Parameters:
item
- the item- Returns:
- the node containing the item, once inserted
-
remove
Remove the item that this node contains from the tree, and return the node removed.The removed node is only this node if this is a leaf.
If this node isn't a leaf, find a suitable leaf node (if this has a left subtree, then its maximum node, otherwise the right subtree's minimum), copy its item into this node, and remove and return that node.
- Returns:
- the removed node
-
previous
Get the item within this subtree that would be the predecessor of one with the given key.There may or may not be an item with the given key.
- Parameters:
key
- a key- Returns:
- the previous item (by key), or
null
if there is none
-
next
Get the item within this subtree that would be the successor of one with the given key.There may or may not be an item with the given key.
- Parameters:
key
- a key- Returns:
- the next item (by key), or
null
if there is none
-
minNode
Get the node in this subtree with the least key.- Returns:
- the minimum node (the leftmost descendant)
-
maxNode
Get the node in this subtree with the greatest key.- Returns:
- the maximum node (the rightmost descendant)
-
min
Get the item in this subtree with the least key.- Returns:
- the minimum item (by key)
-
max
Get the item in this subtree with the greatest key.- Returns:
- the maximum item (by key)
-