47public class KTreeNode<Key
extends Comparable<Key> >
55 Entry(Key key,KTreeNode<Key> child) {
61 this.child=other.child;
67 return key!=
null ? key.toString() :
"null";
71 KTreeNode<Key> parent_ =
null;
76 protected int compareKeys(Key a,Key b) {
77 if (a==
null)
return -1;
78 if (b==
null)
return +1;
79 else return a.compareTo(b);
86 entries_=
new Vector<Entry>();
87 entries_.push_back(
new Entry(
null,
null));
91 public KTreeNode(Key key) {
92 entries_=
new Vector<Entry>();
93 entries_.push_back(
new Entry(
null,
null));
94 entries_.push_back(
new Entry(key,
null));
97 public int getK() {
return entries_.size(); }
103 public Key getKey(
int k) {
104 assert(0<k && k<getK());
105 return entries_.at(k).key;
112 public KTreeNode<Key> getChild(
int k) {
113 assert(0<=k && k<getK());
114 return entries_.at(k).child;
122 public Key find(Key key) {
123 for (
int i=1;i<entries_.size();++i) {
124 int cmp=compareKeys(key,entries_.at(i).key);
126 return entries_.at(i).key;
128 if (entries_.at(i-1).child==
null)
131 return entries_.at(i-1).child.find(key);
134 return entries_.back().child!=
null ? entries_.back().child.find(key) :
null;
141 int getIndexOfChild(KTreeNode<Key> child) {
143 for (
int i=0;i<entries_.size();++i)
144 if (entries_.at(i).child==child)
155 Entry insert(Key key,
int k) {
157 assert(0<=k && k<=getK());
159 Entry entry=
new Entry(key,
null);
160 entries_.insert(k+1,entry);
162 assert(entries_.at(k+1)==entry);
163 assert(k==0 || compareKeys(entries_.at(k-1).key,key)<0);
164 assert(k+2>=getK() || compareKeys(key,entries_.at(k+2).key)<0);
176 assert(1<k && k<getK()-1);
177 KTreeNode<Key> tmpRoot=
new KTreeNode<Key>(getKey(k));
180 KTreeNode<Key> left=
new KTreeNode<Key>(getKey(1));
181 left.entries_.at(0).child=entries_.at(0).child;
182 left.entries_.at(1).child=entries_.at(1).child;
183 for (
int i=2;i<k;++i)
184 left.entries_.push_back(entries_.at(i));
185 tmpRoot.entries_.front().child=left;
186 left.updateChildrensParent();
188 KTreeNode<Key> right=
new KTreeNode<Key>(getKey(k+1));
189 right.entries_.at(0).child=entries_.at(k).child;
190 right.entries_.at(1).child=entries_.at(k+1).child;
191 for (
int i=k+2;i<getK();++i)
192 right.entries_.push_back(entries_.at(i));
193 tmpRoot.entries_.back().child=right;
194 right.updateChildrensParent();
197 this.entries_=tmpRoot.entries_;
198 left.parent_=right.parent_=
this;
205 void mergeChild(
int k) {
206 KTreeNode<Key> child=getChild(k);
210 for (
int i=child.getK()-1;i>0;--i)
211 entries_.insert(k+1,child.entries_.at(i));
214 for (
int i=0;i<child.getK();++i)
215 if (child.entries_.at(i).child!=
null) {
216 assert(child.entries_.at(i).child.parent_==child);
217 child.entries_.at(i).child.parent_=
this;
220 entries_.at(k).child=child.entries_.at(0).child;
224 private void updateChildrensParent() {
225 for (Entry e : entries_) {
227 e.child.parent_=
this;
234 public void checkConsistency() {
236 int i=parent_.getIndexOfChild(
this);
238 throw new RuntimeException(
"parent-child mismatch");
240 for (
int i=0; i<entries_.size(); ++i) {
241 if (entries_.at(i).child!=
null) {
242 if (entries_.at(i).child.parent_!=
this)
243 throw new RuntimeException(
"invalid parent");
246 if (compareKeys(entries_.at(i-1).key,entries_.at(i).key)>=0) {
247 throw new RuntimeException(
"invalid order of entries");
252 if (entries_.at(i).child!=
null)
253 entries_.at(i).child.checkConsistency();
257 @Override
public String toString() {
258 java.util.Iterator<Entry> ii=entries_.iterator();
259 assert(ii.hasNext());
260 Entry entry=ii.next();
261 assert(entry.key==
null);
263 while (ii.hasNext()) {
264 rv+=ii.next().toString();
271 @Override
public GraphvizDecorator getDecorator() {
275 @Override
public String toDot() {
276 String dot=
"graph BinaryTree {\n";
278 GraphvizDecorator decorator=getDecorator();
279 if (decorator!=
null) {
280 String d=decorator.getGlobalStyle();
281 if (d!=
null) dot+=
" "+d+
";\n";
282 d=decorator.getGraphDecoration(
this);
283 if (d!=
null) dot+=
" graph ["+d+
"];\n";
285 dot+=treeToDot(
null);
292 private String dotRef() {
293 return toString()+
"-"+hashCode();
296 protected String dotLabel() {
return toString(); }
298 private String treeToDot(KTreeNode<Key> parent) {
301 GraphvizDecorator decorator=getDecorator();
302 String dot =
" \""+dotRef()+
"\" [label=\""+dotLabel()+
"\",";
303 dot+=(decorator!=
null) ? decorator.getFullNodeDecoration(
this) :
"shape=box";
307 for (Entry entry : entries_) {
308 if (entry.child!=
null)
309 dot+=entry.child.treeToDot(
this);
318 dot+=
" \""+parent.dotRef()+
"\" -- \""+
320 dot+=(decorator!=
null) ? decorator.getFullEdgeDecoration(
this) :
"";
328 private String dotLeaf(String side) {
329 String dummy=
"\""+dotRef()+
"-"+side+
"\"";
330 String dot=
" "+dummy+
" [shape=point];\n";
331 dot+=
" \""+dotRef()+
"\" -- "+dummy+
" [];\n";
336 public String toTikZ() {
337 return "\\"+toTikZ(0)+
";\n";
339 protected String tikzNodeStyle() {
return ""; }
340 protected String toTikZ(
int level) {
341 String spaces=Sys.indent(level);
342 String tex=spaces+
"node "+tikzNodeStyle()+
" {"+toString()+
"}\n";
345 for (Entry entry : entries_) {
346 if (entry.child!=
null)
347 tex+=spaces+
" child {\n"+entry.child.toTikZ(level+1)+
" }\n";
355 public static void main(String[] args) {
356 KTreeNode<String> node=
new KTreeNode<String>(
"a");
362 System.out.println(node.find(
"b"));
363 System.out.println(node.find(
"d"));
364 System.out.println(node.find(
"x"));
374 System.out.println(node.find(
"b"));
375 System.out.println(node.find(
"d"));
376 System.out.println(node.find(
"x"));
378 System.out.println(node.toTikZ());
384 System.out.println(node.find(
"b"));
385 System.out.println(node.find(
"d"));
386 System.out.println(node.find(
"x"));
392 System.out.println(node.find(
"b"));
393 System.out.println(node.find(
"d"));
394 System.out.println(node.find(
"x"));
396 node.checkConsistency();
Entry in a KTreeNode with reference to key and left child.
Implementation of an array-based vector.
Simple viewer for Graphvizable.
static DotViewer displayWindow(Graphvizable object, String caption)
create new DotViewer (toplevel window) and display object
Decorator for items of Graphvizable objects.
System related utilities.
Interface for decorating items of Graphvizable objects.
Interface for GraphViz rendering.
utilities (not related to AuD lecture)
AuD lecture: Data structures, algorithms, examples.