Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the methods treePrint and leafCount. Sample results from the completed programs are shown below. The treePrint method uses reverse inorder traversal and uses an

Implement the methods treePrint and leafCount. Sample results from the completed programs are shown below. The treePrint method uses reverse inorder traversal and uses an extra parameter to keep track of the depth of a node below the root (root is at depth 0, child(ren) of a node at level k are at level k + 1). Printing of a node is done by:

recursively printing the right subtree (its depth is one more than the depth of current node)

indenting appropriate for the depth of the current node, then printing the node's data and end-of-line;

recursively printing the left subtree (its depth is one more than the depth of current node) image text in transcribed

public class ICA07 { static final Random rand = new Random(); public static void main(String[] args) { // TODO code application logic here IntBSTNoDup tree = new IntBSTNoDup(); out.println("CPS 151 ICA 07 by _____Your name here_______"); for (int Lcv = 1; Lcv  r.data, so go right if (r.right != null) // recurse return add(r.right, data); // r.right == null so add a new node as right child of r r.right = new TreeNode(data); return true; } // end private add public String toString() { if (root == null) return "()"; return toString(root); } // end public toString private String toString(TreeNode r) { if (r == null) return ""; return "(" + toString(r.left) + r.data + toString(r.right) + ")"; } // end private toString public String preOrder() { if (root == null) return "Empty"; return preOrder(root); } // end public preOrder private String preOrder(TreeNode n) { if (n == null) return ""; return " " + n.data + preOrder(n.left) + preOrder(n.right); } // end private preOrder public String inOrder() { if (root == null) return "Empty"; return inOrder(root); } // end public inOrder private String inOrder(TreeNode n) { if (n == null) return ""; return inOrder(n.left) + " " + n.data + inOrder(n.right); } // end private inOrder int size() { return size(root); } int leafCount() { return leafCount(root); } private int size(TreeNode n) { if (n == null) return 0; return 1 + size(n.left) + size(n.right); } private int leafCount(TreeNode n) { return 0; // stub } // end private leafCount public void treePrint() { treePrint(root, 0); } // end public treePrint private void treePrint(TreeNode n, int level) { } // end private treePrint private static void indent(int level) { for(int Lcv = 1; Lcv   Sample output Output - ICA07_Key (run) % run: CPS 151 ICA 07 by KEY The tree generated is ((((120) 139 (250)) 285) 353 (398((((439) 547) 706) 880))) SF The tree in preorder: 353 285 139 120 250 398 880 706 547 439 The tree in inorder: 120 139 250 285 353 398 439 547 706 880 Tree printed by treePrint: 280 106 541 439 398 353 285 250 139 120 The number of nodes in tree is: 10 The number of leaves in tree is: 3 CPS 151 ICA 07 complete BUILD SUCCESSFUL (total time: 0 seconds)  Sample output Output - ICA07_Key (run) % run: CPS 151 ICA 07 by KEY The tree generated is ((((120) 139 (250)) 285) 353 (398((((439) 547) 706) 880))) SF The tree in preorder: 353 285 139 120 250 398 880 706 547 439 The tree in inorder: 120 139 250 285 353 398 439 547 706 880 Tree printed by treePrint: 280 106 541 439 398 353 285 250 139 120 The number of nodes in tree is: 10 The number of leaves in tree is: 3 CPS 151 ICA 07 complete BUILD SUCCESSFUL (total time: 0 seconds)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

More Books

Students also viewed these Databases questions