-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathExercise25_03.java
27 lines (22 loc) · 894 Bytes
/
Exercise25_03.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package ch_25.exercise25_03;
import ch_25.exercise25_03.*;
import java.util.Scanner;
/**
* **25.3 (Implement inorder traversal without using recursion) Implement the inorder
* method in {@link BST} using a stack instead of recursion. Write a test program that
* prompts the user to enter 10 integers, stores them in a BST, and invokes the
* inorder method to display the elements.
*/
public class Exercise25_03 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter ten integers: ");
BST<Integer> binarySearchTree = new BST<>();
String[] line = input.nextLine().split(" ");
for (String str : line) {
binarySearchTree.insert(Integer.valueOf(str));
}
System.out.println("Root is: " + binarySearchTree.root.element);
binarySearchTree.inorder();
}
}