-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBBTGenAlgorithm.java
More file actions
38 lines (32 loc) · 999 Bytes
/
BBTGenAlgorithm.java
File metadata and controls
38 lines (32 loc) · 999 Bytes
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
import java.util.*;
class BBTGenAlgorithm {
public static int[] GenerateBBSTArray(int[] a) {
int[] tree = new int[a.length];
arraySort(a);
int index = 0;
int start = 0;
int end = a.length-1;
func(a,tree,start,end, index);
return tree;
}
public static void arraySort(int[] array){
for (int i = array.length-1; i >= 0; i--){
for (int j = 0; j < i; j++){
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
public static void func(int[] array, int[] finalArray, int start, int end, int index){
if (start > end){
return;
}
int mid = (start+end)/2;
finalArray[index] = array[mid];
func(array, finalArray, start, mid-1, 2*index+1);
func(array, finalArray,mid+1, end, 2*index+2);
}
}