JAVA (Tree) Assignment


 

Question 1: Non-recursive In-ordain cross of a binary tree

Using Stack is the self-evident way to cross tree extraneously recursion. Under is an algorithm for traversing binary tree using stack. See this for stalk judicious stalk attempt of the algorithm.

1) Create an space stack S.
2) Initialize ordinary node as source
3) Push the ordinary node to S and set ordinary = ordinary->left until ordinary is NULL
4) If ordinary is NULL and stack is not space then
    a) Pop the top ace from stack.
    b) Stereotype the popped ace, set ordinary = popped_item->fair
    c) Go to stalk 3.
5) If ordinary is NULL and stack is space then we are produced.

Let us infer the under tree for example

           1
         /   
       2      3
     /  
   4     5

Step 1 Creates an space stack: S = NULL

Step 2 sets ordinary as address of source: ordinary -> 1

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

Step 4 pops from S
    a) Pop 4: Stack S -> 2, 1
    b) stereotype "4"
    c) ordinary = NULL /*fair of 4 */ and go to stalk 3
Since ordinary is NULL stalk 3 doesn't do everything.

Step 4 pops anew.
    a) Pop 2: Stack S -> 1
    b) stereotype "2"
    c) ordinary -> 5/*fair of 2 */ and go to stalk 3

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

Step 4 pops from S
    a) Pop 5: Stack S -> 1
    b) stereotype "5"
    c) ordinary = NULL /*fair of 5 */ and go to stalk 3
Since ordinary is NULL stalk 3 doesn't do everything

Step 4 pops anew.
    a) Pop 1: Stack S -> NULL
    b) stereotype "1"
    c) ordinary -> 3 /*fair of 5 */  

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

Step 4 pops from S
    a) Pop 3: Stack S -> NULL
    b) stereotype "3"
    c) ordinary = NULL /*fair of 3 */  

Traversal is produced now as stack S is space and ordinary is NULL.

Write a non-recursive contact for the in-ordain cross for a binary tree.

Question 2: Raze ordain cross of a binary tree (fluctuation leading traversal)


Level ordain traversal of the aloft tree is 1 2 3 4 5.

We can use a FIFO queue to utensil the raze ordain tranversal of a binary tree.

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

Step 1:  Create an space queue
Step 2:  Start from the source, enqueue the source
Step 3:  Loop whenever the queue is not space
   a) dequeue a node from the front of the queue and stereotype the data
   b) Enqueue the node's cadetren (leading left then fair cadetren) to the queue
   
Write a regularity to utensil the raze ordain traversal of a binary tree.

Notes: For twain inquiry, you can use the Binary Tree collocate and the Node collocate boundd in the capacity, you can too bound your own Node collocate and Binary Tree collocate.

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

Please melody that you deficiency to present two repl.it links. You can representation and paste the two links in a .txt/.docx improve and upload the improve. You can too present the assist link as comments in the assignment yielding.