Question
Make the class BinaryTree iterable by filling in the missing codes marked xxx After your program is complete, fill in the number that is output
Make the class BinaryTree iterable by filling in the missing codes marked xxx
After your program is complete, fill in the number that is output by the program.
BinaryTree.java
class TreeNode implements Comparable { private int data; private TreeNode left; private TreeNode right; TreeNode(int data) { this.data = data;} void setLeft(TreeNode left) { this.left = left; } void setRight(TreeNode right) { this.right = right; } TreeNode getLeft() { return left; } TreeNode getRight() { return right; } boolean isLeaf () { // xxx fill in the unfinished codes // test if a node is a leaf TreeNode a = getLeft(); TreeNode b = getRight(); return a==null && b == null; } @Override public int compareTo (TreeNode o) { // xxx fill in the unfinished codes int diff = this.data - o.data; if ( diff > 0 ) { diff = 1; } else if (diff <0) { diff = -1; } return diff; } @Override public boolean equals (Object o) { // xxx fill in the unfinished codes return this.compareTo ( (TreeNode) o ) == 0 ; } @Override public int hashCode () { // xxx fill in the unfinished codes return Integer.hashCode ( this.data ) ; } @Override public String toString () { return Integer.toString (this.data); } } // xxx implements Iterable public class BinaryTree { private TreeNode root = null; // xxx Add iterator(); @Override public String toString () { String s = "Tree: "; s += toStringRecurse (root, "") ; return s; } private String toStringRecurse (TreeNode curr, String indent) { // xxx fill in unfinished codes if (curr==null) { return ""; } String s = " " + indent + curr.toString () ; indent += " "; TreeNode a = curr.getLeft() ; TreeNode b = curr.getRight() ; if( a != null ) { s += toStringRecurse (a, indent) ; } if( b != null ) { s += toStringRecurse (b, indent) ; } return s ; } public ArrayList collectSortedTreeNodes() { // xxx fill in unfinished codes // xxx hints: convert ArrayList to TreeSet ArrayList nodes = collectTreeNodes( ) ; TreeSet set = new TreeSet (nodes); ArrayList ans = new ArrayList ( set ); return ans; } /* The topmost node of a binary tree is the root node. The level of a node is the number of edges along the unique path between it and the root node. Therefore, the root node has a level of 0 and its children are at level 1. Tree height is equal to the largest of all nodes' levels.. */ public int findTreeHeight() { // xxx fill in unfinished codes int height = findTreeHeightRecurse ( root, -1) ; return height; } private int findTreeHeightRecurse ( TreeNode curr, int height) { if (curr==null) return height; TreeNode a = curr.getLeft() ; TreeNode b = curr.getRight() ; height ++; int h1=height, h2=height; if( a != null ) { h1 = findTreeHeightRecurse ( a, height) ; } if( b != null ) { h2 = findTreeHeightRecurse ( b, height) ; } return (h1>h2)? h1: h2 ; } public ArrayList collectTreeNodes() { // xxx fill in unfinished codes ArrayList nodes = new ArrayList<> (); collectTreeNodesRecurse ( this.root, nodes) ; return nodes; } private void collectTreeNodesRecurse ( TreeNode curr, ArrayList nodes) { // xxx fill in unfinished codes if (curr==null) { return ; } nodes.add ( curr ); TreeNode a = curr.getLeft() ; TreeNode b = curr.getRight() ; if( a != null ) { collectTreeNodesRecurse (a, nodes) ; } if( b != null ) { collectTreeNodesRecurse (b, nodes) ; } } public int countLeafNodes() { // xxx fill in unfinished codes int n = 0 ; if (root!= null) { n = countLeafNodesRecurse(root); } return n; } public int countLeafNodesRecurse(TreeNode node) { // xxx fill in unfinished codes if(node.isLeaf() ) { return 1; } return countLeafNodesRecurse(node.getLeft()) + countLeafNodesRecurse(node.getRight()); } private void populateForTesting( int[] ints ) { ArrayList nodes = new ArrayList<> (); for (int i: ints ) { TreeNode node = new TreeNode(i); nodes.add (node); } int size = nodes.size() ; if ( size==0 ) return ; root = nodes.get(0); TreeNode n1 = null; TreeNode n2 = null; TreeNode n3 = null; TreeNode n4 = null; TreeNode n5 = null; TreeNode n6 = null; if (size>1) n1 = nodes.get(1); if (size>2) n2 = nodes.get(2); if (size>3) n3 = nodes.get(3); if (size>4) n4 = nodes.get(4); if (size>5) n5 = nodes.get(5); if (size>6) n6 = nodes.get(6); if (size>1) root.setLeft( n1 ); if (size>2) root.setRight(n2); if (size>3) n2.setLeft(n3); if (size>4) n2.setRight(n4); if (size>5) n3.setLeft(n5); if (size>6) n3.setRight(n6); } private static ArrayList genTrees() { int[][] arr = { {}, {0,}, {0,1,2,3,6}, {0,1,2,3,4,5,6}, {0,2,1,3,-4,5,-6}, } ; ArrayList trees = new ArrayList<> (); for (int[] ints: arr ) { BinaryTree tree = new BinaryTree(); tree.populateForTesting(ints); trees.add(tree); } return trees; } public static void testToString (BinaryTree tree) { System.out.println( "---testToString---"); System.out.println(tree); System.out.println(); } public static void testCountLeafNodes (BinaryTree tree) { System.out.println( "---testCountLeafNodes---"); int n= tree.countLeafNodes() ; String s = "Number of leaves is " + n ; System.out.println( s ); System.out.println(); } public static void testCollectTreeNodes (BinaryTree tree) { System.out.println( "---testCollectTreeNodes---"); ArrayList nodes = tree.collectTreeNodes() ; System.out.println(nodes); System.out.println(); } public static void testCollectSortedTreeNodes (BinaryTree tree) { System.out.println( "---testCollectSortedTreeNodes---"); ArrayList nodes = tree.collectSortedTreeNodes() ; System.out.println(nodes); System.out.println(); } public void testFindTreeHeight () { System.out.println( "---testFindTreeHeight---"); int h = findTreeHeight() ; System.out.println( "Tree height = " + h); System.out.println(); } public static void main(String[] args) { ArrayList trees = genTrees() ; System.out.println("Your codes are not finished yet!!! " ); // xxx un-comment out the following code after class is revised to be iterable /* int sum = 0 ; for ( BinaryTree tree : trees) { for (TreeNode i: tree) { int j = i.getData(); if ( j%2==0 ) sum+= j; } } System.out.println(sum); } */ } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started