Java Reference
In-Depth Information
Display 15.41
Demonstration Program for the Binary Search Tree
1
import
java.util.Scanner;
2
public class
BinarySearchTreeDemo
3 {
4
public static void
main(String[] args)
5 {
6 Scanner keyboard =
new
Scanner(System.in);
7 IntTree tree =
new
IntTree( );
8 System.out.println("Enter a list of nonnegative integers.");
9 System.out.println("Place a negative integer at the end.");
10
int
next = keyboard.nextInt( );
11
while
(next >= 0)
12
{
13
tree.add(next);
14
next = keyboard.nextInt( );
15
}
16 System.out.println("In sorted order:");
17 tree.showElements( );
18 }
19 }
Sample Dialogue
Enter a list of nonnegative integers.
Place a negative integer at the end.
40
30
20
10
11
22
33
44
-1
In sorted order:
10 11 20 22 30 33 40 44