Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 2: K-ary Tree Outputs The following methods are required to output the tree using various standard tree representations NOTE IF YOU WANTT/NEED A STACK

image text in transcribed

Part 2: K-ary Tree Outputs The following methods are required to output the tree using various standard tree representations NOTE IF YOU WANTT/NEED A STACK OR QUEUE: For the iterators in this section you may want to use a stack and/or a queue class. If you do, you can use your dynamic array class from Project 1, your stack class from Project 2, or you may use Java's ArrayDeque class (already imported for you). Note that we are only allowing the use of ArrayDeque to assist those who couldn't complete Project 1 or Project 2, and you are not obligated to use it. YOU MAY NOT use ArrayDeque anywhere else in your project, just for these iterators. If you use your classes (or new classes), remember that must make them private nested classes in KTree (I would recommend private static nested classes) 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: "o 1 2 null 4 5 null" would print as: null 4 5 null public Iterator getLevelOrderIterator ) Returns an iterator which walks through the internal storage in level order, , e.g. the tree "o 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, c.g. "o 1 2 null 4 5 null" returns "o 1 2 4 5" Hint: use getLevelOrderIterator ) public Iterator getPreOrderIterator ) Returns an iterator which walks through the tree using a pre-order walk, e.g. the tree "0 1 2 null 4 5 null" returns the elements in the following order: o 1 4 2 5 Hint: You can build yourself an internal stack ifyou want! (Or not.) public String toStringPreOrder ) Returns a string of a pre order walk, e.g. "O 1 2 null 4 5 null" returns "o1 4 2 5". Hint: use getPreOrderIterator ) * public Iterator getPostOrderIterator () Returns an iterator which walks through the tree using a post-order walk, e.g. the tree 0 1 2 null 4 5 null" returns the elements in the following order: 4 1 5 2 0 Hint: You can build yourself an internal stack if you want! (Or not.) . public String tostringPostOrder ) e Returns a string of a post order walk, e.g. "O 1 2 null 4 5 null" returns "41 5 2 " Hint: use getPostorderIterator () tl:dr You need to write these methods too, please read the note about stacks/queues Part 2: K-ary Tree Outputs The following methods are required to output the tree using various standard tree representations NOTE IF YOU WANTT/NEED A STACK OR QUEUE: For the iterators in this section you may want to use a stack and/or a queue class. If you do, you can use your dynamic array class from Project 1, your stack class from Project 2, or you may use Java's ArrayDeque class (already imported for you). Note that we are only allowing the use of ArrayDeque to assist those who couldn't complete Project 1 or Project 2, and you are not obligated to use it. YOU MAY NOT use ArrayDeque anywhere else in your project, just for these iterators. If you use your classes (or new classes), remember that must make them private nested classes in KTree (I would recommend private static nested classes) 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: "o 1 2 null 4 5 null" would print as: null 4 5 null public Iterator getLevelOrderIterator ) Returns an iterator which walks through the internal storage in level order, , e.g. the tree "o 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, c.g. "o 1 2 null 4 5 null" returns "o 1 2 4 5" Hint: use getLevelOrderIterator ) public Iterator getPreOrderIterator ) Returns an iterator which walks through the tree using a pre-order walk, e.g. the tree "0 1 2 null 4 5 null" returns the elements in the following order: o 1 4 2 5 Hint: You can build yourself an internal stack ifyou want! (Or not.) public String toStringPreOrder ) Returns a string of a pre order walk, e.g. "O 1 2 null 4 5 null" returns "o1 4 2 5". Hint: use getPreOrderIterator ) * public Iterator getPostOrderIterator () Returns an iterator which walks through the tree using a post-order walk, e.g. the tree 0 1 2 null 4 5 null" returns the elements in the following order: 4 1 5 2 0 Hint: You can build yourself an internal stack if you want! (Or not.) . public String tostringPostOrder ) e Returns a string of a post order walk, e.g. "O 1 2 null 4 5 null" returns "41 5 2 " Hint: use getPostorderIterator () tl:dr You need to write these methods too, please read the note about stacks/queues

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

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

Recommended Textbook for

Inference Control In Statistical Databases From Theory To Practice Lncs 2316

Authors: Josep Domingo-Ferrer

2002nd Edition

3540436146, 978-3540436140

More Books

Students also viewed these Databases questions

Question

How can we visually describe our goals?

Answered: 1 week ago

Question

2. How will the team select a leader?

Answered: 1 week ago