Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed

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

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_2

Step: 3

blur-text-image_3

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

Question

=+How are the first copy costs and distribution costs comprised?

Answered: 1 week ago