1

I have implemented a code which adds elements in a tree and prints them in increasing order. However my aim is to learn iterators and want to replace the inOrder() function with an iterator function. How can I do this?

import java.util.InputMismatchException;
import java.util.Scanner;
import javax.xml.soap.Node;

class Tree 
{
    public final int mVal;
    public Tree mLeft;
    public Tree mRight;
    public Node next;
    public Tree(int val) 
    {
        mVal = val;
    }
    public void add(int val) 
    {
        if (val < mVal) 
        {
            if (mLeft == null)
            mLeft = new Tree(val);
        else
            mLeft.add(val);
        }   
        else 
        {
            if (val > mVal) 
            {
            if (mRight == null)
            mRight = new Tree(val);
            else
            mRight.add(val);
            }
        }
    }

    public String inOrder() 
    {
          return ((mLeft == null) ? "" : mLeft.inOrder())
            + mVal + " "
            + ((mRight == null) ? "" : mRight.inOrder());
    }

    public static void main(String[] args) 
    {
        Tree t = new Tree(8);
        Scanner scanner = new Scanner(System.in);
        boolean continueLoop = true; // determines if more input is needed
        for (int i = 1; i < 9; ++i) 
           { 
            try // read two numbers and calculate quotient 
            {
            System.out.print("Please enter a random integer : ");
            int stackInt = scanner.nextInt();
            t.add(Integer.valueOf(stackInt));
            } // end try
            catch (InputMismatchException inputMismatchException){
            System.err.printf("\nException: %s\n", inputMismatchException);
            scanner.nextLine(); //discard input so user can try again
            System.out.println("You must enter integers. Please try again.\n");
           } // end catch
        }
        System.out.println("Values in order = "+ t.inOrder());
    }
}
1

1 Answer 1

1

look at this picture

InOrder

First Step: if node has a left child, visit left child and do the first step with the child

Second Step: node has no left child (or we visited the left child already), add it to the inorder list

Third Step: first step with right child

i didnt test it

@Override
public String toString() {
    return String.valueOf(mVal);
}
public String inOrder(Tree root) {
    List<Tree> inOrder = new ArrayList<>();
    inOrderRecursively(root, inOrder);
    return inOrder.toString();
}

private void inOrderRecursively(Tree Node, List<Tree> inOrder) {
    if (Node.mLeft != null) {
        inOrderIt(Node.mLeft, inOrder);
    }
    inOrder.add(Node);
    if (Node.mRight != null) {
        inOrderIt(Node.mRight, inOrder);
    }
}

greetings

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.