forked from ChenyuGao/Crawler-Parallel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathternaryTree.c
172 lines (167 loc) · 3.32 KB
/
ternaryTree.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
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
#include <stdlib.h>
#include <string.h>
#include "common.h"
TREE *tr_alloc()
{
TREE *tree;
if ((tree = malloc(sizeof(TREE))) == NULL)
return NULL;
memset(tree, 0, sizeof(TREE));
if ((tree->root = malloc(sizeof(TNode))) == NULL)
{
free(tree);
return NULL;
}
memset(tree->root, 0, sizeof(TNode));
return tree;
}
// 将url添加进三叉树
int tr_add_string(TREE *tree, char *P, int M, int id)
{
if(M == 0) // 防止空串插入影响id
{
return -1;
}
int i = 0, j, flag;
TNodePtr currentNode, preNode, newNode;
if (tree->uNum == 0) //还未添加url
{
currentNode = tree->root;
for (i; i<M; i++)
{
if ((newNode = malloc(sizeof(TNode))) == NULL)
return -1;
memset(newNode, 0, sizeof(TNode));
newNode->data = P[i];
if (i == M - 1)
newNode->ID = id;
else
newNode->ID = -1;
currentNode->next = newNode;
currentNode = currentNode->next;
}
tree->uNum++;
return 0;
}
currentNode = preNode = tree->root->next;
while (currentNode != NULL && i<M)
{
if (P[i] == currentNode->data)
{
i++;
preNode = currentNode;
currentNode = currentNode->next;
flag = 0;
}
else if (P[i]<currentNode->data)
{
preNode = currentNode;
currentNode = currentNode->left;
flag = 1;
}
else
{
preNode = currentNode;
currentNode = currentNode->right;
flag = 2;
}
}
//同样的链接以前一个的编号为准
if (i == M)
{
if(preNode->ID == -1)
{
tree->uNum++;
preNode->ID = id;
return 0;
}
else
return -1;
}
if ((newNode = malloc(sizeof(TNode))) == NULL)
return -1;
memset(newNode, 0, sizeof(TNode));
newNode->data = P[i];
if (i == M - 1)
newNode->ID = id;
else
newNode->ID = -1;
currentNode = preNode;
if (flag == 0)
{
currentNode->next = newNode;
currentNode = currentNode->next;
i++;
}
else if (flag == 1)
{
currentNode->left = newNode;
currentNode = currentNode->left;
i++;
}
else
{
currentNode->right = newNode;
currentNode = currentNode->right;
i++;
}
//插入完毕则返回
if (i == M)
{
tree->uNum++;
return 0;
}
//将剩余字符插入
for (i; i<M; i++)
{
if ((newNode = malloc(sizeof(TNode))) == NULL)
return -1;
memset(newNode, 0, sizeof(TNode));
newNode->data = P[i];
if (i == M - 1)
newNode->ID = id;
else
newNode->ID = -1;
currentNode->next = newNode;
currentNode = currentNode->next;
}
tree->uNum++;
return 0;
}
// 在三叉树中搜索url,若存在返回id,若不存在返回-1
int tr_search(TREE* tree, int uLen, char* searchURL)
{
int i;
TNodePtr currentNode, child;
currentNode = tree->root;
child = currentNode->next;
for (i = 0; i<uLen; i++)
{
if(child == NULL)
return -1;
if (searchURL[i] == child->data)
{
currentNode = child;
child = currentNode->next;
if (i == uLen-1)
return currentNode->ID;
continue;
}
else if (searchURL[i] > child->data)
{
child = child->right;
if(child == NULL)
return -1;
i--;
continue;
}
else if (searchURL[i] < child->data)
{
child = child->left;
if(child == NULL)
return -1;
i--;
continue;
}
}
}