Homework Help Question & Answers

public static int nbLeaf(BT bt, T e): This method counts and returns the number of leaf...

public static int nbLeaf(BT bt, T e):

This method counts and returns the number of leaf nodes that contain the data e. You should use the find, isLeaf and maybe otherBT methods for counting

method isLeaf

public boolean isLeaf (){
if (root==null) return false;

else

if ((current.left==null)&&(current.right==null))
return true;

else return false; }

method find :

public boolean find(Relative rel){
switch (rel) {
   case Root: // Easy case
   current = root;
   return true;
   case Parent:
   if(current == root) return false;
   current = current.parent;
   return true;
   case LeftChild:
   if(current.left == null) return false;
   current = current.left;
   return true;
   case RightChild:
   if(current.right == null) return false;
   current = current.right;
   return true;
   default:
   return false;
}
   }

0 0
Report
Answer #1

public static int nbLeaf(BT bt, T e)

{

    // if tree is empty

    if( bt == null )

        return 0;

    // if tree is not empty

    else

    {

        // get no of required leaf in left subtree

        int l = nbLeaf( bt.left , e );

       

        // get no of required leaf in right subtree

        int r = nbLeaf( bt.right , e );

       

        // if the current node is the required leaf node

        // assuming date is the feied storing date

        if( isLeaf(bt) && bt.date == e )

            return l + r + 1;

      // if the current node is not the required leaf node

        else

            return l + r;

    }

}

Know the answer?
Add Answer to:
public static int nbLeaf(BT bt, T e): This method counts and returns the number of leaf...
Your Answer: Your Name: What's your source?
Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
Free Homework App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.