38public class A234Tree<Key
extends Comparable<Key>> implements
Graphvizable {
41 boolean top_down_ =
true;
45 root_=
new KTreeNode<Key>();
48 public A234Tree(
boolean top_down) {
58 public Key find(Key key) {
59 return root_.find(key);
65 public boolean insert(Key key) {
67 return insert_top_down(root_,key);
69 return insert_bottom_up(root_,key);
85 protected void split_bottom_up(KTreeNode<Key> node,
int pos) {
90 node.split(2 + (pos<2 ? 1 : 0));
92 if (node.parent_!=
null) {
93 int i=node.parent_.getIndexOfChild(node);
95 node.parent_.mergeChild(i);
96 node.parent_.checkConsistency();
98 split_bottom_up(node.parent_,i);
111 protected KTreeNode<Key> split_top_down(KTreeNode<Key> node) {
118 if (node.parent_!=
null) {
119 int i=node.parent_.getIndexOfChild(node);
121 node.parent_.mergeChild(i);
122 node.parent_.checkConsistency();
124 assert(node.parent_.getK()<=4) :
125 "parent was not a 4-node (top-down split)";
136 protected void onSplit(KTreeNode<Key> node) {
139 protected boolean insert_bottom_up(KTreeNode<Key> node, Key key) {
141 for (
int i=1; i<node.getK(); ++i) {
142 int cmp=node.compareKeys(key,node.getKey(i));
146 if (node.getChild(i-1)==
null) {
156 node.insert(key,i-1);
157 split_bottom_up(node,i-1);
161 return insert_bottom_up(node.getChild(i-1),key);
166 KTreeNode<Key> right=node.getChild(k);
168 return insert_bottom_up(right,key);
171 split_bottom_up(node,k);
176 protected boolean insert_top_down(KTreeNode<Key> node, Key key) {
177 node=split_top_down(node);
180 for (
int i=1; i<node.getK(); ++i) {
181 int cmp=node.compareKeys(key,node.getKey(i));
185 if (node.getChild(i-1)==
null) {
187 node.insert(key,i-1);
191 return insert_top_down(node.getChild(i-1),key);
196 KTreeNode<Key> right=node.getChild(node.getK()-1);
198 return insert_top_down(right,key);
200 node.insert(key,node.getK()-1);
206 public String toDot() {
207 return root_.toDot();
211 public String toTikZ() {
212 return root_.toTikZ();
215 @Override
public String toString() {
216 return root_.toString();
222 public void checkConsistency() {
224 root_.checkConsistency();
228 public static void main(String[] args) {
229 A234Tree<String> tree=
new A234Tree<String>();
232 tree.checkConsistency();
234 tree.checkConsistency();
236 tree.checkConsistency();
238 tree.checkConsistency();
240 tree.checkConsistency();
242 tree.checkConsistency();
244 tree.checkConsistency();
247 System.out.println(tree.toTikZ());
Simple viewer for Graphvizable.
static DotViewer displayWindow(Graphvizable object, String caption)
create new DotViewer (toplevel window) and display object
Interface for GraphViz rendering.
utilities (not related to AuD lecture)
AuD lecture: Data structures, algorithms, examples.