forked from Dev-XYS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinary-Search-Tree.cpp
More file actions
68 lines (61 loc) · 868 Bytes
/
Binary-Search-Tree.cpp
File metadata and controls
68 lines (61 loc) · 868 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
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
#include <cstdio>
#define TREE_SIZE 100000
using namespace std;
struct node
{
int lch, rch, key, satellite;
}T[TREE_SIZE];
int tc = 1;
int _satellite;
void insert(int rt, int x)
{
if (T[rt].lch == 0 && T[rt].rch == 0)
{
T[rt].lch = tc++;
T[rt].rch = tc++;
T[rt].key = x;
T[rt].satellite = _satellite;
}
else
{
if (x < T[rt].key)
{
insert(T[rt].lch, x);
}
else
{
insert(T[rt].rch, x);
}
}
}
int query(int rt, int key)
{
if (T[rt].key == key)
{
return T[rt].satellite;
}
else
{
return key < T[rt].key ? query(T[rt].lch, key) : query(T[rt].rch, key);
}
}
int main()
{
int ord, key, sat;
while (true)
{
scanf("%d", &ord);
if (ord == 1)
{
scanf("%d%d", &key, &sat);
_satellite = sat;
insert(0, key);
}
else if (ord == 2)
{
scanf("%d", &key);
printf("%d\n", query(0, key));
}
}
return 0;
}