-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchTreeAlg.java
184 lines (156 loc) · 4.1 KB
/
SearchTreeAlg.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
/*
* 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.
*/
package searchtreealg;
import java.util.Random;
/**
*
* @author Keevah
*/
public class SearchTreeAlg {
static long startTime;
static long endTime;
private static int[] A;
public static Node root;
//Constructor
public SearchTreeAlg(){
this.root = null;
A = new int[1000];
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int m= 1000;
startTime = System.nanoTime();
SearchTreeAlg t = new SearchTreeAlg();
t.generateRandomArray(m);
for(int j=0;j<A.length;j++){
t.insert(A[j]);
System.out.println(A[j]);
}
t.search(888);
endTime = System.nanoTime();
System.out.println("AVG RUN TIME " + (endTime - startTime));
}
class Node{
int key;
Node left;
Node right;
public Node(int key){
this.key = key;
left = null;
right = null;
}
}
public boolean search(int a){
Node p = root;
while(p!=null && p.key!=a){
if(p.key>a){
p = p.left;
}else if(p.key==a){
return true;
}
else{
p = p.right;
}
}
return false;
}
public void insert(int a){
Node newNode = new Node(a);
if(root==null){
root = newNode;
return;
}
Node p = root;
Node q = null;
while(p!=null && p.key!=a){
q = p;
if(p.key>a){
p = p.left;
}else{
p = p.right;
}
}
if(p==null){
p = newNode;
p.key = a;
p.left = null;
p.right = null;
if(q.key>a){
q.left = p;
} else{
q.right = p;
}
}
}
public void eliminate(Node p, Node q){
Node r;
if(p.left==null){
r= p.right;
}else{
r= p.left;
}
if(q.left==p){
q.left =r;
}else{
q.right =r;
}
erase(p);
}
public void erase(Node p){
p = null;
}
public void delete(int a){
Node p = root;
while(p!=null && p.key!=a){
if(p.key>a){
p = p.left;
}else{
p = p.right;
}
}
//delete
if(p!=null){
Node q=null;
Node r;
if(p.left==null || p.right==null){
eliminate(p,q);
}
else{
r=p;
q=p;
p=p.right;
while(p.left!=null){
q=p;
p=p.left;
}
r.key=p.key;
eliminate(p,q);
}
}
}
public int height(Node p){
int h;
if(p.left!=null){
h = height(p.left)+1;
}
if(p.right!=null){
h = max(h, height(p.left)+1);
}
if(p.left==null && p.right==null){
h=0;
}
return h;
}
public void generateRandomArray(int m) {
for (int i = 0; i < 1000; i++) {
Random rand = new Random();
A[i] = rand.nextInt(m);
}
}
} //Finall closing brace