Binary Search Tree |
12 points
In this program you modify a Binary Search Tree program. The program is listed at the end of section 16.5 in the text and given to you below for download. You also work with the TreeSet class from the standard Java library. A TreeSet is a Set that is implemented as a Binary Search Tree.
Here is the output from my program:
BINARY SEARCH TREE PROGRAM
Add 9 Original Star Trek names to each tree Remove 'Sulu'
Print list in sorted order:
Chapel
Return 'Sulu' to each tree
Find 'smallest' element:
Print list in sorted order:
Do a preorder traversal of the tree
At each Node, call visit2() to modify the Node
Print list, using inorder traversal:
Do a postorder traversal of the tree
At each Node, call visit3() to modify the Node
Print list, using inorder traversal:
Set up a new binary search tree, and populate it with Integer
objects
Find the average of these integers
Check for the presence of a few names:
Is Kirk in my tree: true
Is Kirk in Java's tree: true
Is Roddenberry in my tree: false
Is Roddenberry in Java's tree: false
Chapel
Chekhov
Kirk
McCoy
Rand
Scott
Spock
Uhura
Chekhov
Kirk
McCoy
Rand
Scott
Spock
Uhura
Chapel
Chapel
Do an inorder traversal and call 'visit1()' to modify each Node
Original Star Trek character: Chapel
Original Star Trek character: Chekhov
Original Star Trek character: Kirk
Original Star Trek character: McCoy
Original Star Trek character: Rand
Original Star Trek character: Scott
Original Star Trek character: Spock
Original Star Trek character: Sulu
Original Star Trek character: Uhura
4 Original Star Trek character: Chapel
3 Original Star Trek character: Chekhov
2 Original Star Trek character: Kirk
1 Original Star Trek character: McCoy
0 Original Star Trek character: Rand
6 Original Star Trek character: Scott
5 Original Star Trek character: Spock
8 Original Star Trek character: Sulu
7 Original Star Trek character: Uhura
A 4 Original Star Trek character: Chapel
B 3 Original Star Trek character: Chekhov
C 2 Original Star Trek character: Kirk
D 1 Original Star Trek character: McCoy
I 0 Original Star Trek character: Rand
E 6 Original Star Trek character: Scott
H 5 Original Star Trek character: Spock
F 8 Original Star Trek character: Sulu
G 7 Original Star Trek character: Uhura
The average is 10.0
You can download BinarySearchTree.java and
BST_Main.java to use as starting points in writing your program.
(Right-click on the links, choose 'Save Target As' and add the '.java' extension to each file name.)
The Java files contain instructions on writing code.