Question: K-ary Tree Outputs public String toString() Output the tree where each level is printed on its own line, and each node in the level is

K-ary Tree Outputs

public String toString()

Output the tree where each level is printed on its own line, and each node in the level is printed space separated.

For example, the binary tree: 0 1 2 null 4 5 null would print as: 0 1 2 null 4 5 null

public Iterator getLevelOrderIterator()

Returns an iterator which walks through the internal storage in level order, , e.g. the tree 0 1 2 null 4 5 null returns the elements in the following order: 0 1 2 4 5

Example usage: Integer[] input = {0, 1, 2, null, 4, 5, null};

KTree tree = new KTree<>(input, 2);

Iterator it = tree.getLevelOrderIterator(); it.next(); //returns 0 it.next(); //returns 1

Hint: This should be easy for array-based storage, but for link structures you may want to use an internal queue (you can write your own!).

public String toStringLevelOrder()

Returns a string of a level order walk, e.g. 0 1 2 null 4 5 null returns 0 1 2 4 5

Hint: use getLevelOrderIterator()

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!