Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java question: Write a subclass of the BinaryTree class that adds the following

ID: 3772077 • Letter: J

Question

Java question:

Write a subclass of the BinaryTree class that adds the following method:

public int leafNodeCount()

The method should return the number of leaf nodes in a tree instance. If you need to change the visibility modifiers of any members in BinaryTree just make a note of this in your solution.

Explanation / Answer

The following function returns the number of leaf nodes in a tree public int leafNodeCount(){ int i = 0; i = leaf(root); return i; }//return leaf node count //help method to getNumberOfLeafNodes public int leaf(BSTNode root){ if(root==null) return 0; if((root.getleftchild()==null) && (root.getrightchild()==null)) return 1; return(leaf(root.getleftchild()) + leaf(root.getrightchild())); } The following program shows the implementation of a tree along with the leafcount: import java.lang.*; public class BSTree{ private BSTNode root; public BSTree(){} public void setroot(BSTNode root){ this.root = root; } public BSTNode getroot(){ return root; } // add a node public void add(Comparable toAdd){ BSTNode pooh = new BSTNode(toAdd); if(root ==null){ root = pooh; } else{ insert(root, pooh); } } // add a node helper method public void insert(BSTNode curr, BSTNode temp){ if(curr.compareTo(temp) > 0){ if(curr.getleftchild()==null){ curr.setleftchild(temp); } else{ insert(curr.getleftchild(), temp); } } else if(curr.compareTo(temp) < 0){ if(curr.getrightchild()==null){ curr.setrightchild(temp); } else{ insert(curr.getrightchild(), temp); } } else{ System.out.println("Already exists"); } } //delete a node public Comparable delete(Comparable toDelete){ BSTNode temp; Comparable if(root != null){ if(root.getData().compareTo(toDelete)==0){ if((root.getleftchild()== null) && (root.getrightchild()== null)) } else if((root.getleftchild()!= null) && (root.getrightchild() == null)){ } else if((root.getleftchild()== null) && (root.getrightchild() !=null)){ } else if((root.getleftchild()!= null) && (root.getrightchild()!=null)){ } } return one; } // return target node public Comparable find(Comparable toFind){ Comparable d = null; if(root!= null){ if(root.getData().compareTo(toFind) == 0){ d = root.getData(); } else if(root.getData().compareTo(toFind) > 0){ d = find2(root.getleftchild(), toFind); } else if(root.getData().compareTo(toFind)< 0){ d = find2(root.getrightchild(), toFind); } else{ d = null; } } return d; }//return Comparable // find helper method public Comparable find2(BSTNode root, Comparable toFind){ Comparable c= null; if(root.getData().compareTo(toFind)==0){ c =root.getData(); } else if(root.getData().compareTo(toFind) >0){ find2(root.getleftchild(), toFind); } else{ find2(root.getrightchild(), toFind); } return c; } // get number of all nodes public int getNumberOfNodes(){ int count = 0; count = allnodes(root); return count; }// returns count //getNumberOfNodes helper method public int allnodes(BSTNode root){ int w = 0; if(root==null){ w = 0; } else{ w = (1 + allnodes(root.getleftchild())+ allnodes(root.getrightchild())); } return w; } //end // get number of leaves in BST public int getNumberOfLeafNodes(){ int i = 0; i = leaf(root); return i; }//return leaf node count //help method to getNumberOfLeafNodes public int leaf(BSTNode root){ if(root==null) return 0; if((root.getleftchild()==null) && (root.getrightchild()==null)) return 1; return(leaf(root.getleftchild()) + leaf(root.getrightchild())); } //end // get the number of full nodes public int getNumberOfFullNodes(){ int t = 0; t = fullnode(root); return t; } //geNumberOfFullNodes helper method public int fullnode(BSTNode root){ if(root==null) return 0; if((root.getleftchild()!=null) && (root.getrightchild()!=null)) return(1+ fullnode(root.getleftchild()) + fullnode(root.getrightchild())); return(fullnode(root.getleftchild()) + fullnode(root.getrightchild())); } // end //find height of BST public int getHeight(){ int h = 0; h = height(root); return h; } //end //helper method to getHeight public int height(BSTNode root){ if(root == null) return -1; else return 1 + max(height(root.getleftchild()), height(root.getrightchild())); } //end //find max of right or left public int max( int a, int b){ if(a>b) return a; else return b; } //end max //Print PreOrder public void printPreOrder(){ printPreOrder(root); } //Print PreOrder helper method public void printPreOrder(BSTNode root){ if(root!= null) { System.out.println(root.getData().toString()); printPreOrder(root.getleftchild()); printPostOrder(root.getrightchild()); } } //print postOrder public void printPostOrder(){ printPostOrder(root); } //print PostOrder helper method public void printPostOrder(BSTNode root){ if(root!=null) { printPostOrder(root.getleftchild()); printPostOrder(root.getrightchild()); System.out.println(root.getData().toString()); } } // print in order public void printInOrder(){ printInOrder(root); } //print in order helper method public void printInOrder(BSTNode root){ if(root!= null) { printInOrder(root.getleftchild()); System.out.println(root.getData().toString()); printInOrder(root.getrightchild()); } } public static void main(String argv[]){ BSTree twix = new BSTree(); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote