0

I have this method:

static int countLeaves(BinaryNode node) {
        if (node == null) {
            return 0;
        } 
        else if (node.left == null && node.right == null) {
            return 1;
        }
        return countLeaves(node.left) + countLeaves(node.right);
}

and i want to Write a client program that constructs a binary search tree whose elements are given by the user. Then, find and print the number of leaf nodes in the constructed tree.

I need ur help plz :)

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Lulie
0

Then create instances of the BinaryNode class and construct the tree by giving values to the .left and .right nodes. Then call that method with argument the root node.

For reading input use the Scanner class

0

aha Thnx :)
but plz do me a favor and check my code from errors

import java.util.*;
public class ClientCodeTest
{
  static Scanner console=new Scanner (System.in);
  
    public static void main(String[] args)
	 {
	    BinarySearchTree testTree=new BinarySearchTree();
		 int num,count;
 System.out.println("Enter number of  elements to build your tree");
		 num=console.nextInt();
		 while(num!=null)
		 {
		    tree.add(num);
		    num=console.nextInt();
		  }
		  
		count=leafCount(tree);
		System.out.print("The number of Leaves in your tree is:");
		System.oyt.println(count);
		
		}

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

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.