Hi. There is something wrong with the codes of my levelOrder method. Can anybody help me identify the wrong stuff out here? By the way, I also have another problem,what are the uses of setters? I teacher require us to put getters and setters but then I am totally confused if what is its use. I don't know if I use the getters correctly. Can anybody explain their importance and uses here?
You're help will be highly appreciated.
Thank you very much....

package binaryTree;

import sun.misc.Queue;

public class BinaryTree {
	Object root;
	int value;
	BinaryTree leftSubtree;
	BinaryTree rightSubtree;

	public BinaryTree() {
		root = null;
		leftSubtree = null;
		rightSubtree = null;
	}

	public BinaryTree(int value, BinaryTree left, BinaryTree right){
		root = value;
		leftSubtree = left;
		rightSubtree = right;
	}

	public Object getRoot(){
		return root;
	}

	public void setRoot(int value){
		root = new BinaryTree(value);
	}

	public BinaryTree getLeftSubtree(){
		return leftSubtree; 
	}

	public void setLeftSubtree(int value){
		leftSubtree = new BinaryTree(value);
	}

	public BinaryTree getRightSubtree(){
		return rightSubtree; 
	}

	public void setRightSubtree(int value){
		rightSubtree = new BinaryTree(value);
	}

	public BinaryTree(int value) {
		root=value;
		leftSubtree = null;
		rightSubtree = null;
		this.value = value;
	}
	
	public int getHeight(BinaryTree root){
		if(root == null){
			return 0;
		}
		else{
			return 1 + Math.max(getHeight(root.getLeftSubtree()),getHeight(root.getRightSubtree()));
		}
	}
	
	public boolean isEmpty(){
		if(root == null){
			return true;
		}else
			return false;
	}

	public int countNode(BinaryTree root) {
		if(root==null){
			return 0;
		}
		else{
			return 1 + countNode(root.getLeftSubtree()) + countNode(root.getRightSubtree());
		}
	}

	public int countLeaves(BinaryTree root){
		if(root==null){
			return 0;
		}
		else if(root.getLeftSubtree()==null && root.getRightSubtree()==null){
			return 1;
		}
		else{
			return countLeaves(root.getLeftSubtree()) + countLeaves(root.getRightSubtree());
		}
	}
	
	public void inorderTraversal(BinaryTree root) {
		if (root != null) {
			inorderTraversal(root.getLeftSubtree());
			System.out.print(root.value+" ");
			inorderTraversal(root.getRightSubtree());
		}
	}

	public void preorderTraversal(BinaryTree root){
		if(root !=null){
			System.out.print(root.value+" ");
			preorderTraversal(root.getLeftSubtree());
			preorderTraversal(root.getRightSubtree());
		}
	}

	public void postorderTraversal(BinaryTree root){
		if(root !=null){
			postorderTraversal(root.getLeftSubtree());
			postorderTraversal(root.getRightSubtree());
			System.out.print(root.value+" ");
		}
	}
	
	public void levelorderTraversal(BinaryTree root) throws InterruptedException{
		Queue queue = new Queue();
		while(root!=null){
			queue.enqueue(root);
			while(!queue.isEmpty()){
				System.out.println(queue.dequeue());
				if(queue.leftSubtree()!=null){
					queue.enqueue(root.getLeftSubtree().root);
				}
				else{
					queue.enqueue(root.getRightSubtree().root);
				}
			}
		}
	}

//	public void levelorderTraversal(BinaryTree root){
//		Queue queue = new Queue();
//		queue.enqueue(root);
//		while(!queue.isEmpty()){
//			System.out.println(root.value+" ");
//				if(root.leftSubtree !=null){
//					queue.enqueue(root.leftSubtree);
//		        if ( root.rightSubtree != null )
//		            queue.enqueue( root.rightSubtree );
//				}
//			}
//		}
	
	public void insert(BinaryTree root, int value) {
		if (value < root.value) {
			if (root.getLeftSubtree() != null) {
				insert(root.getLeftSubtree(), value);
			} else {
				root.leftSubtree = new BinaryTree(value);
			}
		} else if (value > root.value) {
			if (root.getRightSubtree() != null) {
				insert(root.getRightSubtree(), value);
			} else {
				root.rightSubtree = new BinaryTree(value);
			}
		}
	}
	
	public static void main(String[] args){
		new BinaryTree(0).run();
	}

	public void run() {
		BinaryTree node = new BinaryTree(50);
		System.out.println("Building tree with root value " + node.value);
		insert(node, 17);
		insert(node, 76);
		insert(node, 54);
		insert(node, 23);
		insert(node, 9);
		insert(node, 14);
		insert(node, 19);
		insert(node, 72);
		insert(node, 12);
		insert(node, 67);
		System.out.print("\nHeight: " + getHeight(node));
		System.out.print("\nCount nodes: " + countNode(node));
		System.out.print("\nCount leaves: "+ countLeaves(node));
		System.out.print("\nInorder: ");
		inorderTraversal(node);
		System.out.print("\nPreorder: ");
		preorderTraversal(node);
		System.out.print("\nPostorder: ");
		postorderTraversal(node);
		System.out.println("\nLevel Order:");
	}

			
}

Problem : if(queue.leftSubtree!=null)
PS:Don't forget to check all threads titled with binary tree.

public void levelorderTraversal(BinaryTree root) throws InterruptedException{
Queue queue = new Queue();
while(root!=null){
 queue.enqueue(root);
 while(!queue.isEmpty()){
 System.out.println(queue.dequeue());
  if(root.leftSubtree!=null){
   queue.enqueue(root.getLeftSubtree().root);
  }
else{
   queue.enqueue(root.getRightSubtree().root);
  }
 }
 }
}

But I guess it still does not work ,sir. It does not output the right answer. maybe it is on my algorithm or what. I dunno.

I changed the levelOrder method. Is this correct? Can anybody check it?

public void levelorderTraversal(BinaryTree root) throws Exception{
    Queue queue = new Queue();
    BinaryTree temp;
    queue.enqueue(getRoot());
    while(!queue.isEmpty()){
        temp = (BinaryTree)queue.dequeue();
        if(temp.getLeftSubtree()!=null)
            queue.enqueue(temp.getLeftSubtree());
        if(temp.getRightSubtree()!=null)
            queue.enqueue(temp.getRightSubtree());
        System.out.println(temp.getRoot()+" ");
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.