Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(Include your modified BinaryTree.java source code file in your homework solution zip archive) Add a method int getSizeO to Binary Tree that returns the size

image text in transcribed

(Include your modified BinaryTree.java source code file in your homework solution zip archive) Add a method int getSizeO 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 Week3 Objects and Classes II Section 2 in method pablic int getSize) named Counter which implements the BinaryTree Visitor visit (pData: E): void m Count: int +Counter(): ctor +getCount): int +visit (pData: E): void The Counter constructor initializes the counter variable mCoant to 0, and the Coanter.visit) method will be called each a time a node is visited either from BinaryTree.traverse(int, Node, BinaryTreeVisitor) or BinaryTree traverseLevelOrdeT(Node, BinaryTreeVisitor) depending on whether you performed a level order traversal or one of the three other types of traversals as described below. Within Counter.visit), we simply increment mCount to count the node as visited Once the local class is completed, we can count the nodes in the tree by performing a traversal (it does not matter which type of traversal we perform because each node 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() I // Implement 1local class named Counter here // Create a Counter object to count the nodes Counter counter = new Counter(); // I have chosen to perform a level order traversal, but try the other ones to verify they work // the same way as well, e.g. , traverse (PRE ORDER, counter. travers (LEVEL_ORDER, counter); // After we finish the traverse, Counter.mCount is equal to the number of nodes visited, which // is the size of the tree return counter.getCountO

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

Excel caculation on cascade mental health clinic

Answered: 1 week ago