-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree.c
57 lines (54 loc) · 1.27 KB
/
tree.c
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
#include "tree.h"
Node *SearchNode(Node *T, char *key, Node *f)
{
if(!T)
return NULL;
else if(!strcmp(key, T->key))
return T;
else if(strcmp(key, T->key)>0)
return SearchNode(T->lchild, key, T);
else
return SearchNode(T->rchild, key, T);
}
Node *InsertNode(Node **T, char *key, char *data)
{
if(*T == NULL)
{
*T = calloc(1, sizeof(Node));
if(*T == NULL)
return NULL;
else
{
(*T)->key = key;
(*T)->data = data;
(*T)->lchild = NULL;
(*T)->rchild = NULL;
}
}
else if(strcmp(key, (*T)->key)>0)
(*T)->lchild = InsertNode(&((*T)->lchild), key, data);
else if(strcmp(key, (*T)->key)<0)
(*T)->rchild = InsertNode(&((*T)->rchild), key, data);
else if(!strcmp(key, (*T)->key))
{
(*T)->data = data;
}
return *T;
}
/*int main()
{
char *key1 = "1";
char *key2 = "2";
char *key3 = "3";
char *data1 = "dfs";
char *data2 = "sdfa";
char *data3 = "ds";
Node *p = NULL;
Node *s = NULL;
InsertNode(&p, &key1, &data1);
InsertNode(&p, &key2, &data2);
InsertNode(&p, &key3, &data3);
s = SearchNode(p, "4", NULL);
if(s)
printf("%s\n", *(s->data));
}*/