-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTMain.java
242 lines (209 loc) · 8.42 KB
/
BSTMain.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Random;
import java.util.Scanner;
/**
*
* @author LeeAnna Ewing
*/
public class BSTMain {
public static void main(String[] args) {
//create instance of Random class
Random r = new Random();
//create Scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
int menuOption;
int minValue = 0;
int maxValue = 0;
//get desired amount of nodes from user
System.out.print("Please enter your desired number of nodes : ");
int numNode = keyboard.nextInt();
while (numNode <= 0)
{
System.out.print("Error: Please enter a positive integer greater than 0 for number of nodes");
numNode = keyboard.nextInt();
}
//instantiate array based on user input
int[] integers = new int [numNode];
int n = integers.length;
//add desire number of nodes too array
for(int i = 0; i < n; i++) {
integers[i] = r.nextInt(100) + 1;
}
//For testing purposes
/*
System.out.print("Unsorted: ");
printArray(integers);
System.out.println();
*/
// Initial call to quicksort
quicksort(integers, 0, n - 1);
//For testing purposes
/*
System.out.print("Sorted: ");
printArray(integers);
System.out.println();
*/
BSTFunctions tree = new BSTFunctions();
tree.root = tree.sortedArrayToBST(integers, 0, n -1 );
do{
System.out.println();
System.out.println("Please select from the following Binary Search Tree menu options:");
System.out.println("1. Find the minimum value \n" +
"2. Find the maximum value \n" +
"3. Remove the minimum value \n" +
"4. Remove the maximum value \n" +
"5. Find a certain value \n" +
"6. Print inorder tree traversal \n" +
"7. Exit" );
System.out.print("Enter your numerical menu selection: ");
menuOption = keyboard.nextInt();
System.out.println();
while(menuOption < 1 || menuOption > 7){
System.out.println("Invalid menu selction. Please try again: ");
System.out.println("Enter your numerical menu selection: ");
menuOption = keyboard.nextInt();
System.out.println();
}
//Find the minimum value
if (menuOption == 1){
if (tree.root == null){
System.out.println("The tree is empty. There are no min values.");
}
else if (tree.root != null){
minValue = tree.minValue(tree.root);
System.out.println("The lowest value in the tree is: " + minValue);
}
}
//Find the maximum value
else if (menuOption == 2){
if (tree.root == null){
System.out.println("The tree is empty. There are no max values.");
}
else if (tree.root != null){
maxValue = tree.maxValue(tree.root);
System.out.println("The highest value in the tree is: " + maxValue);
}
}
//delete min value
else if (menuOption == 3){
if (tree.root == null){
System.out.println("The tree is empty. There are no nodes to delete");
}
else if (tree.root != null){
tree.root = tree.deleteNode(tree.root,(minValue = tree.minValue(tree.root)));
System.out.println("The minimum value of " + minValue + " has been deleted.");
if (tree.root == null)
System.out.println("The tree is now empty");
}
}
//delete max value
else if (menuOption == 4){
if (tree.root == null){
System.out.println("The tree is empty. There are no nodes to delete");
}
else if (tree.root != null){
tree.root = tree.deleteNode(tree.root,(maxValue = tree.maxValue(tree.root)));
System.out.println("The maximum value of " + maxValue + " has been deleted.");
if (tree.root == null)
System.out.println("The tree is now empty");
}
}
//Find a given value
else if (menuOption == 5){
if (tree.root == null){
System.out.println("The tree is empty. There is nothing to find");
}
else if (tree.root != null){
System.out.print("Please enter the value between 1 - 100 that you would like me to search for: ");
int key = keyboard.nextInt();
while (key < 1 || key > 100){
System.out.println("Error: Value not within specified range.");
System.out.print("Please enter the value between 1 - 100 that you would like me to search for: ");
key = keyboard.nextInt();
}
boolean test = tree.BSTSearch(tree.root, key);
if (test == true)
System.out.println("Your value of " + key + " was found in the tree!");
else if (test == false)
System.out.println("Your value of " + key + " was not found in the tree.");
}
}
//show in order traversal
else if (menuOption == 6){
if (tree.root == null)
{
System.out.println("The tree is empty. There is nothing to traverse.");
}
else{
System.out.print("InOrder traversal: ");
tree.inOrder(tree.root);
System.out.println();
}
}
}while(menuOption != 7);
}//main
/*****************************************************************************/
static int partition(int integers[], int low, int high){
int l = 0;
int h = 0;
int midpoint = 0;
int pivot = 0;
int temp = 0;
boolean done = false;
//Pick middle element as pivot
midpoint = low + (high - low) /2;
pivot = integers[midpoint];
l = low;
h = high;
while (!done) {
// Increment l while integers[l] < pivot
while (integers[l] < pivot){
++l;
}//inner while (low)
// Decrement h while pivot < integers[h]
while(pivot < integers[h]) {
--h;
}
// If there are zero or one elements remaining, all numbers are
// partitioned. Return h
if (l >= h){
done = true;
}//if
else {
// Swap intergers[l] and integers [h] udate l and h
temp = integers[l];
integers[l] = integers[h];
integers[h] = temp;
++l;
--h;
}//else
}
return h;
}//partition
static void quicksort(int integers[], int low, int high){
int j = 0;
// Base case: If there are 1 or zero elements to sort,
// partition is already sorted
if(low >= high){
return;
}
//Partition the data within the array, Value j returned from partition is
//location of last element in low partition.
j = partition(integers, low, high);
// Recursively sort low partition (low to mid(midpoint)) and
// high partition (mid + 1 to high)
quicksort(integers, low, j);
quicksort(integers, j + 1, high);
}//quicksort
//this method was only for testing purposes
/*
static void printArray(int integers[]){
int n = integers.length;
for (int i = 0; i < n; ++i)
System.out.print(integers[i] + " ");
}//printArray */
}//fine