AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
A234Tree.java
Go to the documentation of this file.
1package aud;
2
4
38public class A234Tree<Key extends Comparable<Key>> implements Graphvizable {
39
40 KTreeNode<Key> root_;
41 boolean top_down_ = true;
42
44 public A234Tree() {
45 root_=new KTreeNode<Key>();
46 }
48 public A234Tree(boolean top_down) {
49 this();
50 top_down_=top_down;
51 }
52
53
58 public Key find(Key key) {
59 return root_.find(key);
60 }
61
65 public boolean insert(Key key) {
66 if (top_down_)
67 return insert_top_down(root_,key);
68 else
69 return insert_bottom_up(root_,key);
70 }
71
85 protected void split_bottom_up(KTreeNode<Key> node, int pos) {
86 if (node.getK()>4) { // CHEATING: we created and split a 5-node!
87
88 onSplit(node);
89
90 node.split(2 + (pos<2 ? 1 : 0));
91
92 if (node.parent_!=null) {
93 int i=node.parent_.getIndexOfChild(node);
94 assert (i>=0);
95 node.parent_.mergeChild(i);
96 node.parent_.checkConsistency();
97
98 split_bottom_up(node.parent_,i); // eventually split parent
99 }
100 }
101 }
102
111 protected KTreeNode<Key> split_top_down(KTreeNode<Key> node) {
112 if (node.getK()>3) {
113
114 onSplit(node);
115
116 node.split(2);
117
118 if (node.parent_!=null) {
119 int i=node.parent_.getIndexOfChild(node);
120 assert (i>=0);
121 node.parent_.mergeChild(i);
122 node.parent_.checkConsistency();
123
124 assert(node.parent_.getK()<=4) :
125 "parent was not a 4-node (top-down split)";
126
127 return node.parent_; // split: continue inserting at parent
128 }
129 }
130 return node; // no split: just continue
131 }
132
136 protected void onSplit(KTreeNode<Key> node) {
137 }
138
139 protected boolean insert_bottom_up(KTreeNode<Key> node, Key key) {
140 // recursive find/insert (similar to KTreeNode#find)
141 for (int i=1; i<node.getK(); ++i) {
142 int cmp=node.compareKeys(key,node.getKey(i));
143 if (cmp==0)
144 return false; // key exists in tree
145 else if (cmp<0) {
146 if (node.getChild(i-1)==null) {
147
148 // Note: We pass the insertion index i-1 (and likewise k below)
149 // such that split_bottom_up() can determine correct the index
150 // of the element that is to be "pulled up":
151 // In a classic implementation this happens (recursively) *before*
152 // inserting. Here, we allow a temporary 5-node and merge after
153 // insertion at the price that not always the second element
154 // will be "pulled up".
155
156 node.insert(key,i-1);
157 split_bottom_up(node,i-1);
158 return true;
159
160 } else
161 return insert_bottom_up(node.getChild(i-1),key); // recursive find
162 }
163 }
164
165 int k=node.getK()-1;
166 KTreeNode<Key> right=node.getChild(k);
167 if (right!=null)
168 return insert_bottom_up(right,key); // recursive find
169
170 node.insert(key,k);
171 split_bottom_up(node,k);
172
173 return true;
174 }
175
176 protected boolean insert_top_down(KTreeNode<Key> node, Key key) {
177 node=split_top_down(node);
178
179 // recursive find/insert (similar to KTreeNode#find)
180 for (int i=1; i<node.getK(); ++i) {
181 int cmp=node.compareKeys(key,node.getKey(i));
182 if (cmp==0)
183 return false; // key exists in tree
184 else if (cmp<0) {
185 if (node.getChild(i-1)==null) {
186 // ASSERT
187 node.insert(key,i-1);
188 return true;
189
190 } else {
191 return insert_top_down(node.getChild(i-1),key); // recursive find
192 }
193 }
194 }
195
196 KTreeNode<Key> right=node.getChild(node.getK()-1);
197 if (right!=null)
198 return insert_top_down(right,key); // recursive find
199
200 node.insert(key,node.getK()-1);
201
202 return true;
203 }
204
205 @Override
206 public String toDot() {
207 return root_.toDot();
208 }
209
211 public String toTikZ() {
212 return root_.toTikZ();
213 }
214
215 @Override public String toString() {
216 return root_.toString();
217 }
218
222 public void checkConsistency() {
223 // missing: check particular properties of 2-3-4-tree!
224 root_.checkConsistency();
225 }
226
228 public static void main(String[] args) {
229 A234Tree<String> tree=new A234Tree<String>();
230
231 tree.insert("b");
232 tree.checkConsistency();
233 tree.insert("c");
234 tree.checkConsistency();
235 tree.insert("a");
236 tree.checkConsistency();
237 tree.insert("d");
238 tree.checkConsistency();
239 tree.insert("e");
240 tree.checkConsistency();
241 tree.insert("f");
242 tree.checkConsistency();
243 tree.insert("g");
244 tree.checkConsistency();
245
246 aud.util.DotViewer.displayWindow(tree,"234 tree");
247 System.out.println(tree.toTikZ());
248
249 // aud.util.DotViewer.displayWindow(tree,"234 tree");
250 }
251}
Simple viewer for Graphvizable.
Definition: DotViewer.java:47
static DotViewer displayWindow(Graphvizable object, String caption)
create new DotViewer (toplevel window) and display object
Definition: DotViewer.java:139
Interface for GraphViz rendering.
utilities (not related to AuD lecture)
Definition: Colormap.java:1
AuD lecture: Data structures, algorithms, examples.
Definition: A234Tree.java:1