Skip to content

Commit

Permalink
普通的二叉搜索树
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-XYS authored Oct 22, 2016
1 parent 3966cdb commit 09701d7
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Binary-Search-Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}

0 comments on commit 09701d7

Please sign in to comment.