0

Hello! We have an assignment regarding binary tree and I am having a hard time debugging it. The countNode and countLeaves method do not work. It does not display output and instead remain blank. Please help me and check if what is really wrong. Thanks in advance. It will be a great help if you'll solve my problem.
Thanks in advance..

package binaryTree;

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 root){
		this.root = root;
	}
	
	public BinaryTree getLeftSubtree(){
		return leftSubtree; 
	}

	public void setLeftSubtree(BinaryTree leftSubtree){
		this.leftSubtree = leftSubtree;
	}
	
	public BinaryTree getRightSubtree(){
		return rightSubtree; 
	}

	public void setRightSubtree(BinaryTree rightSubtree){
		this.rightSubtree = rightSubtree;
	}
	
	public static void main(String[] args){
		new BinaryTree(0).run();
	}

	public BinaryTree(int value) {
		root=value;
		leftSubtree = null;
		rightSubtree = null;
		this.value = value;
	}

	public void run() {
		BinaryTree node = new BinaryTree(50);
		System.out.println("Binary Tree Example");
		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.println("\n\nCOUNT NODES:");
		countNode(node);
		System.out.println("\n\nCOUNT LEAVES:");
		countLeaves(node);
		System.out.println("\nTraversing tree in order:");
		inorderTraversal(node);
		System.out.println("\n\nTraversing tree preorder:");
		preorderTraversal(node);
		System.out.println("\n\nTraversing tree postorder:");
		postorderTraversal(node);
	}

	public void insert(BinaryTree root, int value) {
		if (value < root.value) {
			if (root.leftSubtree != null) {
				insert(root.leftSubtree, value);
			} else {
				System.out.println("  Inserted " + value + " to left of " + root.value);
				root.leftSubtree = new BinaryTree(value);
			}
		} else if (value > root.value) {
			if (root.rightSubtree != null) {
				insert(root.rightSubtree, value);
			} else {
				System.out.println("  Inserted " + value + " to right of "
						+ root.value);
				root.rightSubtree = new BinaryTree(value);
			}
		}
	}

	public void inorderTraversal(BinaryTree root) {
		if (root != null) {
			inorderTraversal(root.leftSubtree);
			System.out.print(root.value+" ");
			inorderTraversal(root.rightSubtree);
		}
	}

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

	public void postorderTraversal(BinaryTree root){
		if(root !=null){
			postorderTraversal(root.leftSubtree);
			postorderTraversal(root.rightSubtree);
			System.out.print(root.value+" ");
		}
	}

	public void levelorderTraversal(BinaryTree root){
		if(root!=null){
		}
	}

	public int countNode(BinaryTree root) {
		if(root==null){
			return 0;
		}
		else{
			return 1 + countNode(root.leftSubtree) + countNode(root.rightSubtree);
		}
	}
	
	public int countLeaves(BinaryTree root){
	if(root == null)
		return 0;
		else if(root.leftSubtree == null && root.rightSubtree == null)
		return 1;
		else if(root.leftSubtree == null && root.rightSubtree != null)
		return countLeaves(root.leftSubtree);
		else if(root.leftSubtree != null && root.rightSubtree == null)
		return countLeaves(root.rightSubtree);
		else
		return countLeaves(root.leftSubtree) + countLeaves(root.rightSubtree);
	}
	
//	public int countLeaves(BinaryTree root){
//		if(root==null){
//			return 0;
//		}
//		else if(leftSubtree==null && rightSubtree==null){
//			return 1;
//		}
//		else{
//			return countLeaves(leftSubtree) + countLeaves(rightSubtree);
//		}
//	}
	
	
	public boolean isEmpty(){
		if(root == null){
			return true;
		}else
			return false;
	}
	
}
2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by __avd
0

countNode and countLeaves return int value (count of nodes).

System.out.println(countNode(node));
System.out.println("\n\nCOUNT LEAVES:");
System.out.println(countLeaves(node));
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.