Data Structure Final Homework

profileSiper
hw.printTreeByLevel.docx

A class of binary search tree MyTree is defined in which the following methods are implemented.

MyTree() // constructor: an empty tree is created

addNode(int v) //Value v is added to the tree following the rule for a binary search tree

print() //The values in the tree are printed following in-order traversal.

The method that you are asked to implement is a method called “printByLevel”

public void printByLevel()

This method prints the values in the tree by the levels. For example, if the tree is as below,

Image result for binary search treethe method prints 8, 3, 10, 1, 6, 14, 4, 7, 13

Note: You can use class ArrayDeque in JAVA API. Only two methods from this class are needed: To add an object into the end of the queue, use add method; to remove an object from the front of the queue, use remove method.