Please help finish this code. Comments are what is needed. Thank you in advance import java.util.ArrayList; public class

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Please help finish this code. Comments are what is needed. Thank you in advance import java.util.ArrayList; public class

Post by answerhappygod »

Please help finish this code. Comments are what isneeded. Thank you in advance
import java.util.ArrayList;
public class BinarySearchTree { private Node root; public BinarySearchTree() { this.root = null; }
public BinarySearchTree(Node root) { this.root = root; } public boolean isEmpty() { if(root==null) returntrue; else return false; } public Node root() { return this.root; } public Node left(Node n) { return n.getLeft(); } public Node right(Node n) { return n.getRight(); } public int numChildren(Node n) { int count=0; if(n.getLeft()!=null)count++; if(n.getRight()!=null)count++; return count; } public boolean isInternal(Node n) { if(numChildren(n)!=0) returntrue; else return false; } public boolean isExternal(Node n) { if(numChildren(n)==0) returntrue; else return false; } public boolean isRoot(Node n) { if(n==root) returntrue; else return false; } public void set(Node n, Integer newElement){ if(n!=null)n.setElement(newElement); } public void inOrder(Node currRoot) { if(currRoot.getLeft()!=null)inOrder(currRoot.getLeft()); System.out.print(currRoot+"\t"); if(currRoot.getRight()!=null)inOrder(currRoot.getRight()); } /**********************************************************************************************/ //implement size() public int size(Node currRoot) { //if tree is empty, return0--base case //else --recursive //return 1+size(lefttree)+size(right tree) }//Implement search to see if a targetvalue is stored on any node of the tree, return true for yes,otherwise, return no public boolean search(Node currRoot, inttargetItem) { //if currRoot is notnull //ifcurrRoot has the target value, return true --base case //ifcurrRoot has left child, return search(left subtree rooted at leftchild) --recursive //ifcurrRoot has right child, return search(right subtree rooted atright child) --recursive //return false if notfound } //Implement insert() public boolean insert(Node currRoot,int newItem) { // if currRoot is null, pack newItem into a nodeand add as root then return true //if currRoot hold an element greater thannewItem //if the currRoot does nothave a leftChild //pack thenewItem into a node and add it as leftchild, then return true //else recursive call inserton the leftSubtree //if currRoot hold an element less thannewItem //if the currRoot does nothave a rightChild //pack thenewItem into a node and add it as rightChild, then returntrue //else recursive call inserton the rightSubtree //return false if failed to insert at thispoint }//Implement findmax() private Node findmax(Node currRoot) { //if currRoot is null, returnnull //else currRoot does not have arightChild, return currRoot //else recursive findMax onrightSubtree }
//Implement findMin() private Node findMin(Node currRoot) { //if currRoot is null, returnnull //else currRoot does not have aleftChild, return currRoot //else recursive findMin onleftSubtree }}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply