Question: JAVA: provided************************************* //For reference only public class NumTree{ private TreeNode root; public NumTree(){ root=null; } public TreeNode getRoot(){ return root; } public void setRoot(TreeNode root){

JAVA: JAVA: provided************************************* //For reference only public class NumTree{ private TreeNode root; public

provided*************************************

//For reference only public class NumTree{

private TreeNode root; public NumTree(){ root=null; }

public TreeNode getRoot(){ return root; } public void setRoot(TreeNode root){ this.root=root; } public boolean isEmpty(){ return root==null;} public int size(){ return sizeHelper(root); } private int sizeHelper(TreeNode node){ if(node==null) return 0; else return 1+sizeHelper(node.getLeft())+sizeHelper(node.getRight()); } public boolean search(int data){ return searchHelper(root, data); } private boolean searchHelper(TreeNode node, int data){ if(node==null) return false; if(node.getData()==data) return true; else if(node.getData()

public void display(){ displayHelper2(root, 0); }

private void displayHelper(TreeNode t, int level){ if(t==null) return ; displayHelper(t.getRight(), level + 1); for(int k = 0; k 0){ tree.setRight(remove(tree.getRight(), node)); }else{ tree = removeNode(tree); //found=true; } return tree; } private TreeNode removeNode(TreeNode tree){ if(tree.getLeft()==null){ return tree.getRight(); }else if(tree.getRight()==null){ return tree.getLeft(); }else{ //if this node has both left and right child; we decided to use the largest node on the left TreeNode maxNodeOnLeft=getMaxNode(tree.getLeft()); tree.setData(maxNodeOnLeft.getData()); tree.setLeft(remove(tree.getLeft(), new TreeNode(maxNodeOnLeft.getData()))); return tree; } } private TreeNode getMaxNode(TreeNode tree){ while(tree.getRight() !=null) tree=tree.getRight(); return tree; } public void buildTree(int[] data){ for(int i=0; i

and*************************

//source code is provided for reference only public class TreeNode implements Comparable{

private int data; private TreeNode left; private TreeNode right;

public TreeNode(int data){ this.data=data; left=right=null; }

public int getData(){ return data; } public TreeNode getLeft(){ return left; } public TreeNode getRight(){ return right; } public void setData(int data){ this.data = data; } public void setLeft(TreeNode left){ this.left = left; } public void setRight(TreeNode right){ this.right = right; } public int compareTo(TreeNode node){ return data-node.getData(); } }

******I tried to attempt this (my code posted below) not sure how to approach this problem. The method printLeaves has an argument of "NumTree tree", it passes a tree? not sure how to work with this. Can anyone help and structure the code similar to mine and explain?

my code: (i cant indent in chegg, not sure if theres a trick or if i just cant, please work with it if you can)

public class ManipulatingTree{ public static void printLeaves(NumTree tree){ TreeNode node=tree.getRoot(); printHelper(node); } public static void printHelper(TreeNode node){ if(node == null) System.out.print("no leaves"); else if(node.getLeft()==null && node.getRight()==null) System.out.print("leaves:"+" "+node.getData()); else{ printHelper(node.getLeft()); printHelper(node.getRight()); } }

public static void printLeaves). This method outputs the leaves of a binary tree from right to left. More specifically, the leaves should be printed in the reverse order that they would be printed using any of the standard traversals. For example, if a variable tree stores a reference to the following tree: Then the call of t.printLeaves0; should produce the following output: leaves: 940 If the tree does not have any leaves (an empty tree), simply print: no leaves

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!