-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeDemo.java
More file actions
34 lines (26 loc) · 1.52 KB
/
TreeDemo.java
File metadata and controls
34 lines (26 loc) · 1.52 KB
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
28
29
30
31
32
33
34
import java.util.Random;
public class TreeDemo {
public static void main(String[] args){
//Library of words to be added to the tree.
String[] words = {"Amok", "Nirvana", "Levin", "Minotaur", "Naif",
"Brevet", "Dehort", "Costive", "Boffin", "Hoyle",
"Scion", "Pissoir", "Looby", "Kvell", "Redact", "Pi" };
//For easier readability, swap words for numbers.
String[] numbers = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"};
Random rand = new Random(); // Initialize Random
BinarySearchTree myTree = new BinarySearchTree(); // Initialize the Tree (make sure your tree class is named BinarySearchTree)
for (int addLoop = 0; addLoop < 30; addLoop++){ // Loop to add items to the Tree
int r = rand.nextInt(16);
System.out.println("Adding: " + words[r]);
myTree.insert(words[r]); // Method call to the tree to insert nodes
}
System.out.println("---Searches---"); // Start multiple searches
myTree.search(words[rand.nextInt(16)]);
myTree.search(words[rand.nextInt(16)]);
myTree.search(words[rand.nextInt(16)]);
System.out.println("---Printing---"); // Print the tree using all
myTree.printPreOrder(); // three type of order
myTree.printInOrder();
myTree.printPostOrder();
}
}