Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
hard Solved by 2 students

Binary Search Tree In-Order Traversal

Mark
< Prev
Next >

Consider the TreeTest class shown below. What is printed when its main method 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 15 12 18

B

10 5 12 15 18

C

10 5 15 12 18

D

5 10 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.

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|