Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

BinarySearchTree .java public class BinarySearchTree> { private BinaryNode root; public BinaryNode getRoot() { return root; } public void add(E node) { if (node == null)

BinarySearchTree.java

public class BinarySearchTree>
{

   private BinaryNode root;

   public BinaryNode getRoot()
   {
       return root;
   }

   public void add(E node)
   {
       if (node == null) {
           return; // do nothing
       }
       else if (root == null) // first time, just set the root
       {
           root = new BinaryNode(node);
       }
       else {
           add(root, node);  // call method below
       }
   }

   public void add(BinaryNode treeRoot, E node)
   {
       if (node.compareTo(treeRoot.getValue()) <= 0) {
           if (treeRoot.getLeft() == null) {
               treeRoot.setLeft(new BinaryNode(node));
           }
           else {
               add(treeRoot.getLeft(), node);
           }
       }
       else if (node.compareTo(treeRoot.getValue()) > 0) {
           if (treeRoot.getRight() == null) {
               treeRoot.setRight(new BinaryNode(node));
           }

           else {
               add(treeRoot.getRight(), node);
           }
       }
   }

   public int computeHeight()
   {
       return computeHeight(getRoot());
   }

   private int computeHeight(BinaryNode root)
   {
       int x, y;
       if (root != null) {
           if (root.getLeft() == null && root.getRight() == null) {
               return 0;
           }
           x = computeHeight(root.getLeft());
           y = computeHeight(root.getRight());
           if (x >= y) {
               return x + 1;
           }
           else {
               return y + 1;
           }
       }
       return 0;
   }

   public int countLeaves()
   {
       return countLeaves(getRoot());
   }

   private int countLeaves(BinaryNode root)
   {
       int x, y;
       if (root != null) {
           x = countLeaves(root.getLeft());
           y = countLeaves(root.getRight());
           if (root.getLeft() == null && root.getRight() == null) {
               return x + y + 1;
           }
           else {
               return x + y;
           }
       }
       return 0;
   }

   public int countNodes()
   {
       return countNodes(getRoot());
   }

   private int countNodes(BinaryNode root)
   {
       int x, y;
       if (root != null) {
           x = countNodes(root.getLeft());
           y = countNodes(root.getRight());
           return x + y + 1;
       }
       return 0;
   }

   public int countInternalNodes()
   {
       return countInternalNodes(getRoot());
   }

   private int countInternalNodes(BinaryNode root)
   {
       if (root == null) {
           return 0;
       }
       if (root.getLeft() == null && root.getRight() == null) {
           return 0;
       }
       return 1 + countInternalNodes(root.getLeft()) + countInternalNodes(root.getRight());
   }

}
 

 

I need help with my methods which I have bolded below. I need help to create javadoc comments, which are missing, but I am not sure what to comment.



 


Step by Step Solution

3.48 Rating (155 Votes )

There are 3 Steps involved in it

Step: 1

Javadoc comments are used to provide documentation for your code and explain how the methods work He... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Java Programming, Comprehensive Version

Authors: Y. Daniel Liang

10th Edition

133761312, 978-0133761313

More Books

Students also viewed these Programming questions