| preferred AP College board partner for AP classes
hard Solved by 3 students
Binary Search Tree In-Order Traversal
< Prev
Next >

What is the output of the inOrder traversal when the program is executed?

public class TreeTest {
    static class Node {
        int data;
        Node left, right;
        Node(int value) { data = value; }
    }
    public static Node insert(Node root, int value) {
        if(root == null) {
            return new Node(value);
        }
        if(value < root.data) {
            root.left = insert(root.left, value);
        } else if(value > root.data) {
            root.right = insert(root.right, value);
        }
        return root;
    }
    public static void inOrder(Node root) {
        if(root == null) return;
        inOrder(root.left);
        System.out.print(root.data + " ");
        inOrder(root.right);
    }
    public static void main(String[] args) {
        Node root = null;
        int[] vals = {10, 5, 15, 12, 18};
        for(int v : vals) {
            root = insert(root, v);
        }
        inOrder(root);
    }
}
A

5 10 12 15 18

B

5 10 15 12 18

C

10 5 15 12 18

D

10 5 12 15 18

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: [email protected]|Privacy Policy