Click to See Complete Forum and Search --> : student needs help with Mway Tree implementation


pmercado
August 27th, 2004, 08:26 AM
Hi, very new to programming and badly need help with this project. I know what I've written ain't the best code, but this is what I have so far... If anybody can help me with this, that would be great.
I am getting a nullpointerexception error when I run my program and haven't figured out what it is I'm doing wrong. Here's my implementation of a m-way tree, very similar to binary search trees (m=2), where m is the # of children and (m-1) represents the # of keys in the node. I am generating 2K #'s and inserting them into the tree as they are generated. Thanks for any help in advance...

class QuadTreeTest {
public static void main(String[] args) {

QuadTree quad = null;

for (int counter = 1; counter <= 2000; counter++) {
int value = 1 + (int) (Math.random() * 10000);
quad.insert(value);
}
quad.print(quad.root);
}
}

class QuadNode {

public int element1;
public int element2;
public int element3;
public int elemcount = 0;
public QuadNode child1;
public QuadNode child2;
public QuadNode child3;
public QuadNode child4;

// Constructors
public QuadNode() {
child1 = child2 = child3 = child4 = null;
}

public QuadNode(int it1) {
element1 = it1;
elemcount++;
child1 = child2 = child3 = child4 = null;
}

//Return and set elements
public int element1(){
return element1;
}

public int setElement1(int it1){
elemcount++;
return element1 = it1;
}

public int element2(){
return element2;
}

public int setElement2(int it2){
elemcount++;
return element2 = it2;
}

public int element3(){
return element3;
}

public int setElement3(int it3){
elemcount++;
return element3 = it3;
}

//return and set children
public QuadNode child1(){
return child1;
}

public QuadNode setChild1(QuadNode child1){
//nodecount++;
return child1 = child1;
}

public QuadNode child2(){
return child2;
}

public QuadNode setChild2(QuadNode child2){
//nodecount++;
return child2 = child2;
}

public QuadNode child3(){
return child3;
}

public QuadNode setChild3(QuadNode child3){
//nodecount++;
return child3 = child3;
}

public QuadNode child4(){
return child4;
}

public QuadNode setChild4(QuadNode child4){
//nodecount++;
return child4 = child4;
}

public int elemcount(){
return elemcount;
}


public boolean isFull(){
if (elemcount == 3);
return true;
}

public boolean isEmpty(){
if (elemcount == 0);
return true;
}

public boolean isLeaf(){
return (child1 == null) && (child2 == null) &&
(child3 == null) && (child4 == null);
}

}//end class QuadNode


class QuadTree {

public QuadNode root;
int nodecount;//counts the number of nodes

//constructors
public QuadTree() {
root = null;
}

public QuadTree(int it){
root = new QuadNode(it);
nodecount++;
}


//methods
public void clear(){
root = null;
}

public boolean isEmpty(){
return root == null;
}//method isEmpty

public int getNodeCount(){
return nodecount;
}//method getNodeCount

public void print(QuadNode QT){//Prints out the tree using
//inorder traversal
if (QT.isLeaf()) System.out.println("Leaf");
System.out.println(QT.element1() + QT.element2() + QT.element3());
QuadNode temp = QT.child1();
while (temp != null){
print(temp);
temp = QT.child2();
print(temp);
temp = QT.child3();
print(temp);
temp = QT.child4();
print(temp);
}
}//method print

public void insert(int it){
root = inserthelp(root, it);
}//method insert

public QuadNode inserthelp(QuadNode node, int it){
if (node == null){
nodecount++;
return new QuadNode(it);
}
else if ((node.element1 > it) &&(node.elemcount == 1)){
node.setElement2(node.element1);
node.setElement1(it);
}
else if ((node.element1 > it) &&(node.elemcount == 2)){
node.setElement3(node.element2);
node.setElement2(node.element1);
node.setElement1(it);
}
else if ((node.element1 < it) && (node.elemcount == 1)){
node.setElement2(it);
}

else if ((node.element1 < it) && (it < node.element2) &&
(node.elemcount == 2)){
node.setElement3(node.element2);
node.setElement2(it);
}
else if (root.element2 < it){
node.setElement3(it);
}
else if (node.isFull()){
if (node.element1 > it){
node.setChild1(inserthelp(node.child1, it));
nodecount++;
}
else if ((node.element1 < it) && (it < node.element2)){
node.setChild2(inserthelp(node.child2, it));
nodecount++;
}
else if ((node.element2 > it) && (it < node.element3)){
node.setChild3(inserthelp(node.child3, it));
nodecount++;
}
else //(node.element3 < it){
node.setChild4(inserthelp(node.child4, it));
nodecount++;
}
return node;
}//method inserthelp

}//end class QuadTree