Package aud
Class A234Tree<Key extends Comparable<Key>>
java.lang.Object
aud.A234Tree<Key>
- All Implemented Interfaces:
Graphvizable
Simple implementation of 2-3-4-trees based on
KTreeNode.
Note that in contrast to BinarySearchTree ...
- we store only keys rather than key-value pairs.
- there is no dummy node
head, there is no reference to parents.
- In top-down mode,
insert(Key)calls the recursiveinsert_top_down(aud.KTreeNode<Key>, Key), which splits all 4-nodes (usingsplit_top_down(aud.KTreeNode<Key>)) on the way down to the insertion position. (This may split the root!) So the finalKTreeNode.insert(Key, int)is guaranteed to not cause a split. - In bottom-up mode,
insert(Key)calls the recursiveinsert_bottom_up(aud.KTreeNode<Key>, Key), which inserts first and splits if the new node is a 5-node (recursivesplit_bottom_up(aud.KTreeNode<Key>,int)).
Note that this is sort of cheating for ease of implementation because in a proper 2-3-4-tree, there cannot be 5-nodes! (We useKTreeNode.) This has the side effect, that the selection of the node that is "pulled up" to the parent byKTreeNode.mergeChild(int)) possibly has to be "corrected" if the insertion position was less than 2.
Note that there is currently no operation for removal. (This would cause
KTreeNode.mergeChild(int).)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidfew consistency checkschild.entries_.at(i).childfind key in treebooleaninsert entryprotected booleaninsert_bottom_up(KTreeNode<Key> node, Key key) protected booleaninsert_top_down(KTreeNode<Key> node, Key key) static voidexample and testprotected voidcallback invoked bysplit_bottom_up(aud.KTreeNode<Key>,int)orsplit_top_down(aud.KTreeNode<Key>), default implementation is emptyprotected voidsplit_bottom_up(KTreeNode<Key> node, int pos) Split 5-node and merges with parent.split_top_down(KTreeNode<Key> node) Split 4-node and merges with parent.toDot()Get dot representation.toString()toTikZ()get TikZ code for LaTeX export (callsKTreeNode.toTikZ())
-
Constructor Details
-
A234Tree
public A234Tree()create an empty 2-3-4-tree -
A234Tree
public A234Tree(boolean top_down) create an empty 2-3-4-tree and select insertion strategy
-
-
Method Details
-
find
find key in tree- Returns:
- found key, note that this is generally a different
instance than
key! (We have "only"compareKeys()==0.)
-
insert
insert entry- Returns:
trueifkeywas not an entry of child before
-
split_bottom_up
Split 5-node and merges with parent.- I'm sort of cheating here: I let a node eventually become a 5-node and split only then. Implementation is simpler, but it's not (storage) efficient!
- The
posargument stores the insertion position into the 5-node before splitting. It is used to correctly determine the index of the element that is "pulled up". - This may lead to a recursive split up to the root.
- The method has no effect on 2- and 3-nodes.
splitcallsonSplit(aud.KTreeNode<Key>)before the split.
-
split_top_down
Split 4-node and merges with parent.- This method is called on
insert(Key)while traversing top-down. splitcallsonSplit(aud.KTreeNode<Key>)before the split.
- Returns:
node(no split) or its parent (on slit), such that insertion "continues" at returned node
- This method is called on
-
onSplit
callback invoked bysplit_bottom_up(aud.KTreeNode<Key>,int)orsplit_top_down(aud.KTreeNode<Key>), default implementation is empty -
insert_bottom_up
-
insert_top_down
-
toDot
Description copied from interface:GraphvizableGet dot representation.- Specified by:
toDotin interfaceGraphvizable
-
toTikZ
get TikZ code for LaTeX export (callsKTreeNode.toTikZ()) -
toString
-
checkConsistency
public void checkConsistency()few consistency checkschild.entries_.at(i).child- Throws:
RuntimeException- on error (and/or assertion fails)
-
main
example and test
-