The fact thats it an AVL tree probably doesn't matter. I have a printTree method in my tree class and I'm trying to print the nodes in the form of the tree. I've successfully printed the nodes the correct way but now I'm trying to label the nodes by their relative height (i.e. equalheight, right hi, left hi).

The problem is my root, which is right hi, won't change to that. Everything else is working for every node except the root, but the counter I made that determines the label is the right number. I can't for the life of me figure out why it is staying at equal height.

There were tabs below but dani doesn't like that too much.
The first thing is the label, then the node, and the end number is the count.

EDIT: I know you can't tell because the tabs got taken out but alstoto01 is the root, thus the first node to go through the methods.

This is what I'm outputting right now (the Wrong way):
EQUALHT:aaronha01: 0
EQUALHT:agganha01: 0
EQUALHT:alliega01: 0
EQUALHT:alstoto01: -1
EQUALHT:bollifr01: 0
EQUALHT:cronera01: 0
EQUALHT:grammal01: 0
EQUALHT:henlega01: 0
RIGHTHI:herrito01: -1
EQUALHT:jacobsp01: 0

The Correct output should be:
EQUALHT:aaronha01:0
EQUALHT:agganha01:0
EQUALHT:alliega01:0
RIGHTHI:alstoto01:-1
EQUALHT:bollifr01:0
EQUALHT:cronera01:0
EQUALHT:grammal01:0
EQUALHT:henlega01:0
RIGHTHI:herrito01:-1
EQUALHT:jacobsp01:0

Here is the code for the printTree method

private ArrayList<AnyType> printTree( AvlNode<AnyType> t, String buffer )
    {
        if( t != null )
        {
        	int count = counter(t);
            if(count == 0)
            	freq = "EQUALHT:";
            if(count < 0)
            	freq = "RIGHTHI:";
            else if(count > 0)
            	freq = "LEFTHI:";

        	printTree( t.left, buffer + "   ");
            pTree.add((AnyType) (buffer + freq + t.element + ":") );
            System.out.println(buffer + freq + t.element + ":" + count);
            printTree( t.right, buffer + "   ");
            return pTree;
        }
        return null;
    }

Code for my counter method:

private int counter(AvlNode<AnyType> t){
    	int count = 0, left = 0, right = 0;
    	AvlNode<AnyType> temp = t;
    	while(temp != null){
    		temp = temp.right;
    		right++;
    	}
    	while(t != null){
    		t = t.left;
    		left++;
    	}
    	count = left - right;
    	return count;
    }

It's probably a simple fix but I just can't see it. Any help would be appreciated since this is a really stupid problem to have in the grand scheme of things.

Edited 7 Years Ago by vtsmokey88: n/a

Never mind, it was a simple fix: just needed to bring my freq variable down into the method.

private ArrayList<AnyType> printTree( AvlNode<AnyType> t, String buffer )
    {
        if( t != null )
        {
        	int count = counter(t);
                String freq = "";
            if(count == 0)
            	freq = "EQUALHT:";
            if(count < 0)
            	freq = "RIGHTHI:";
            else if(count > 0)
            	freq = "LEFTHI:";

        	printTree( t.left, buffer + "   ");
            pTree.add((AnyType) (buffer + freq + t.element + ":") );
            System.out.println(buffer + freq + t.element + ":" + count);
            printTree( t.right, buffer + "   ");
            return pTree;
        }
        return null;
    }
This question has already been answered. Start a new discussion instead.