Question: Please help with this code. Add a method int getSize() to BinaryTree that returns the size of the binary tree where the size of the

Please help with this code.

Add a method int getSize() to BinaryTree that returns the size of the binary tree where the size of the tree is defined to be the number of nodes. Here is how I want you to implement this method. Write a local class named Counter which implements the BinaryTree Visitor interface:

Please help with this code. Add a method int getSize() to BinaryTree

Here is the BinaryTreeNode.java code to modify/edit:

public interface BinaryTreeNode { E getData(); void setData(E data); BinaryTreeNode getParent(); BinaryTreeNode getLeft();

void setLeft(BinaryTreeNode child); /** * Returns the right child of this node, or null if it does * not have one. */ BinaryTreeNode getRight(); /** * Removes child from its current parent and inserts it as the * right child of this node. If this node already has a right * child it is removed. * @exception IllegalArgumentException if the child is * an ancestor of this node, since that would make * a cycle in the tree. */ void setRight(BinaryTreeNode child); void removeFromParent(); /** * Visits the nodes in this tree in preorder. */ void traversePreorder(Visitor visitor); void traversePostorder(Visitor visitor); /** * Visits the nodes in this tree in inorder. */ void traverseInorder(Visitor visitor); /** * Simple visitor interface. */ public interface Visitor { void visit(BinaryTreeNode node); } }

5.3 (Include your modified BinaryTree.java source code file in your homework solution zip archive) Add a method int getSize C) to Binary Tree that returns the size of the binary tree where the size of the tree is defined to be the number of nodes. Here is how I want you to implement this method. Write a local class (see Week 9 Objects and Classes II Section 2) in getSize named Counter which implements the Binary Tree Visitor interface: nterface Counter implements Binary TreeVisitor -m Count: int visit (pData: E): void getCount(): int +visit(pData: E): void The Counter constructor initializes m Count to 0. visit simply increments mCount when it is called. Once the local t() class is completed, we can count the nodes in the tree by performing a traversal (it does not matter which type of traversal we performe because each mode will be visited during the traversal; the order in which we visit them does not matter for this application). To perform the traversal write: public int getsize Implement local class named Counter here '777 Counter counter new Counter traverse (LEVEL ORDER, counter); return counter. getCount O

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!