package t234;
/**
* This is an object-oriented implementation 2-3-4 trees using the state design
* pattern.
* The classic discussion for 2-3-4 trees is outlined in Sedgewick's
* "Algorithms in C++", Addison-Wesley 1992, pp. 215-219. Other elucidating
* discussions of balanced trees can be found in Stubbs/Webre's
* "Data Structures", and Aho/Hopcroft/Ullman's "Data Structures and Algorithms".
* For the sake of definiteness, we design the tree to store Integer only.
* @since 11/13/01
* @author Dung X. Nguyen, Copyright 2001 - All Rights Reserved.
*/
public class Tree234 {
/** @SBGen Variable (,,,64) */
private ANode234 _rootNode;
/**
* Initializes this Tree234 to empty.
*/
public Tree234() {
_rootNode = EmptyNode.Singleton;
}
/**
* Initializes this Tree234 to contain exactly one data element, an empty
* left subtree, and an empty right subtrees. The subtrees are Tree234.
This constructor is not intrinsically needed. It exists as a convenience only.
* @param n != null.
*/
public Tree234(Integer n){
_rootNode = new Node2(n);
}
public final boolean isEmpty() {
return _rootNode.isEmpty(this);
}
/**
* Inserts an Integer into this Tree234 without duplication.
* @param n != null.
*/
public final void insert(Integer n) {
_rootNode.insert(this,n);
}
/**
* For debugging purpose.
* EXERCISE FOR STUDENTS: Write the toString() method instead.
* Hint: see the binary tree example.
*/
public final void draw() {
drawAtLevel(0);
}
///////////////////////////////////////
//Not visible outside of package
Tree234(ANode234 root) {
_rootNode = root;
}
Tree234(Tree234 lTree, Integer n, Tree234 rTree) {
_rootNode = new Node2 (lTree, n, rTree);
}
final void drawAtLevel(int level) {
_rootNode.drawAtLevel (level);
}
final void changeRoot(ANode234 newRoot) {
_rootNode = newRoot;
}
final ANode234 root() {
return _rootNode;
}
final void insertHelper(Tree234 parent, Integer n) {
_rootNode.insertHelper (parent, this, n);
}
final void attach (Tree234 lTree, Integer n, Tree234 rTree) {
_rootNode.attach (this, lTree, n, rTree);
}
}