-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.java
More file actions
187 lines (171 loc) · 3.46 KB
/
BinarySearchTree.java
File metadata and controls
187 lines (171 loc) · 3.46 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
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
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Node
{
int key;
int value;
Node left = null;
Node right = null;
Node(int key,int value)
{
this.key = key;
this.value = value;
}
}
public class BinarySearchTree
{
public Node root;
public void put(int key,int value)
{
root = put(root, key,value);
}
private Node put(Node root,int key,int value)
{
if(root == null)
{
Node node =new Node(key, value);
return node;
}
else if(key<root.key) root.left = put(root.left,key, value);
else root.right = put(root.right,key, value);
return root;
}
public int get(int key)
{
int value = -1;
Node temp = root;
while(temp!=null)
{
if(key<temp.key) temp = temp.left;
else if(key>temp.key) temp = temp.right;
else
{
value= temp.value;
break;
}
}
return value;
}
public void delete(int key)
{
}
public void setNode(Node node,int key,int value)
{
node.key = key;
node.value = value;
}
public static void main(String[] args)
{
BinarySearchTree bst = new BinarySearchTree();
int[] a = {5,2,8,4,3,6};
for(int i=0;i<a.length;i++)
{
bst.put(a[i], i);
}
System.out.println(bst.get(8));
}
}
//class Key implements Comparable<Key>{
//
// private String s;
// public Key (String s) {
// this.s = s;
// }
// @Override
// public int compareTo(Key that) {
// // TODO Auto-generated method stub
// return this.s.compareTo(that.s);
// }
//
//}
//
//class Value{
// private String s;
// public Value(String string) {
// this.s=string;
// }
// public String getString () {
// return s;
// }
//}
//
//public class BinarySearchTree {
//
// private class Node{
// Key key;
// Value value;
// Node left;
// Node right;
// public Node(Key key,Value value) {
// this.key = key;
// this.value = value;
// }
// }
//
// private Node root;
//
// public void put(Key key,Value value)
// {
// root = put(root, key,value);
// }
//
// private Node put(Node root,Key key,Value value)
// {
//
// if(root == null) return new Node(key, value);
// int cmp = root.key.compareTo(key);
// if(cmp>0)
// root.left=put(root.left, key,value);
// else if(cmp<0)
// root.right=put(root.right,key,value);
// else {
// root.value = value;
// }
// return root;
// }
//
// public Value get(Key key)
// {
// if(root==null)
// return null;
// Node t = root;
// while(t!=null)
// {
// int cmp = root.key.compareTo(key);
// if(cmp<0) t=t.right;
// else if(cmp>0) t=t.left;
// else {
// return t.value;
// }
//
// }
// return null;
//
// }
//
// public static void main(String[] args) throws IOException {
// // TODO Auto-generated method stub
// BinarySearchTree bst = new BinarySearchTree();
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//
// System.out.println("ÇëÊäÈë4¸öÊý×Ö£º");
// //for (int i = 0; i <4; i++) {
// String k = br.readLine();
// String v = br.readLine();
// Key key1 = new Key(k);
// Value value1 = new Value(v);
// bst.put(key1, value1);
//
// k = br.readLine();
// v = br.readLine();
// Key key2 = new Key(k);
// Value value2 = new Value(v);
// bst.put(key2, value2);
// //}
//
// System.out.println(bst.get(key2).getString());
// }
//
//}