Java (tree) assignment | Computer Science homework help

 

Question 1: Non-recursive In-order traverse of a binary tree

Using Stack is the obvious way to traverse tree without recursion. Below is an algorithm for traversing binary tree using stack. See this for step wise step execution of the algorithm.

1) Create an empty stack S.
2) Initialize current node as root
3) Push the current node to S and set current = current->left until current is NULL
4) If current is NULL and stack is not empty then
    a) Pop the top item from stack.
    b) Print the popped item, set current = popped_item->right
    c) Go to step 3.
5) If current is NULL and stack is empty then we are done.

Let us consider the below tree for example

           1
         /   
       2      3
     /  
   4     5

Step 1 Creates an empty stack: S = NULL

Step 2 sets current as address of root: current -> 1

Step 3 Pushes the current node and set current = current->left until current is NULL
    current -> 1
    push 1: Stack S -> 1
    current -> 2
    push 2: Stack S -> 2, 1
    current -> 4
    push 4: Stack S -> 4, 2, 1
    current = NULL

Step 4 pops from S
    a) Pop 4: Stack S -> 2, 1
    b) print "4"
    c) current = NULL /*right of 4 */ and go to step 3
Since current is NULL step 3 doesn't do anything.

Step 4 pops again.
    a) Pop 2: Stack S -> 1
    b) print "2"
    c) current -> 5/*right of 2 */ and go to step 3

Step 3 pushes 5 to stack and makes current NULL
    Stack S -> 5, 1
    current = NULL

Step 4 pops from S
    a) Pop 5: Stack S -> 1
    b) print "5"
    c) current = NULL /*right of 5 */ and go to step 3
Since current is NULL step 3 doesn't do anything

Step 4 pops again.
    a) Pop 1: Stack S -> NULL
    b) print "1"
    c) current -> 3 /*right of 5 */  

Step 3 pushes 3 to stack and makes current NULL
    Stack S -> 3
    current = NULL

Step 4 pops from S
    a) Pop 3: Stack S -> NULL
    b) print "3"
    c) current = NULL /*right of 3 */  

Traversal is done now as stack S is empty and current is NULL.

Write a non-recursive application for the in-order traverse for a binary tree.

Question 2: Level order traverse of a binary tree (breadth first traversal)

Level order traversal of the above tree is 1 2 3 4 5.

We can use a FIFO queue to implement the level order tranversal of a binary tree.

For each node, first the node is visited and then it’s child nodes are put in a FIFO queue.

Step 1:  Create an empty queue
Step 2:  Start from the root, enqueue the root
Step 3:  Loop whenever the queue is not empty
   a) dequeue a node from the front of the queue and print the data
   b) Enqueue the node's children (first left then right children) to the queue
   
Write a method to implement the level order traversal of a binary tree.

Notes: For both question, you can use the Binary Tree class and the Node class defined in the book, you can also define your own Node class and Binary Tree class.

Requirements: Submit the repl.it links of the two programs

Please note that you need to submit two repl.it links. You can copy and paste the two links in a .txt/.docx file and upload the file. You can also submit the second link as comments in the assignment submission.

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more