Question: Part 2 [ 6 0 marks ] - Binary Tree Traversals and extra operations. For this part of the assignment, you will be working with

Part 2[60 marks]- Binary Tree Traversals and extra operations.
For this part of the assignment, you will be working with the BinaryTree class provided in the zip file. It contains four uncompleted functions that you are supposed to complete. For full marks, each of these functions should run in \(\boldsymbol{O}(\boldsymbol{n})\) time and must NOT use recursion. See the function traverse2() in BinaryTree.java that we also discussed in class for an example of how to do tree traversal without recursion. Note that this is just one example and there could be other functions that you may take inspiration from.
You need to implement the following operations:
1. The method leafCounter (mode) returns the following:
- if mode \(0\), it returns the number of left leaves.
- if mode \(>0\), it returns the number of right leaves.
- if mode \(==0\), it returns the total number of leaves in the tree.
- The single root is a special case. Return 1 regardless of the mode value.
- Return 0 if the tree has no nodes at all.
2. The method smallestLeaf() should return the minimum depth in the tree that has leaves in it, or -1 if the tree has no nodes.
3. The method maxWidth () should return the number of nodes in the most populated depth (the depth with the largest number of nodes).
4. The method treeCode () should return a string that gives the string-encoded representation of the binary tree. The string-encoded representation of a binary tree can be defined as follows:
- the encoded representation of a tree with no nodes is the string "O"(big O character).
- the encoded representation of a binary tree with root node \( r \) consists of an open bracket followed by the encoded representation of \( r \). left followed by the encoded representation of \( r \).right followed by a closing bracket >
Part 2 [ 6 0 marks ] - Binary Tree Traversals and

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 Programming Questions!