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
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started