#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedefchar* itemType;
typedefchar* keyType;
/*先查找字符串是否存在树中,如果不存在,就插入,如果存在,就不插入*/
static int N;
static int inc;
static itemType a[100];//数据
staticint left[100];//左边数组
staticint right[100];//右边数组
//#define realKey(a[A]) (a[A])
#define key(A) (a[A])
#define realKey(A) A
#define KNull -3
#define KContain -2
#define KEqual -4
#define KLess -1
#define KMore -5
void STinit(int maxN)
{
N = maxN;
for (int i = 0;i < N;++i)
{
left[i] = 0;
right[i] = 0;
}
}
int STcount()
{
return inc;
}
int isContain(keyType aFirst,keyType aSecond)
{
if (strcmp(aFirst,aSecond) == 0)
{
return KEqual;
}
if (strlen(aFirst) <= strlen(aSecond))
{
for (int i = 0;i < strlen(aSecond); ++i)
{
int temp = i;
int j = 0;
for (;j < strlen(aFirst);++j)
{
if (aFirst[j] == aSecond[temp])
{
++temp;
continue;
}
else
{
break;
}
}
if (j == strlen(aFirst))
{
return KContain;
}
}
}
if (strcmp(aFirst,aSecond) < 0)
{
return KLess;
}
else if (strcmp(aFirst,aSecond) > 0)
{
return KMore;
}
return KNull;
}
int searchR(keyType aKey)
{
int i = 0;
int count = 0;
while (a[i])
{
int tmp = isContain(aKey,key(i));
if (tmp == KEqual||tmp == KContain)
{
return tmp;
}
else if (tmp == KLess)
{
i = left[i];
if (i == 0)
{
break;
}
}
else if (tmp == KMore)
{
i = right[i];
if (i == 0)
{
break;
}
}
count = i;
}
return count;
}
int STsearch(keyType aKey)
{
return searchR(aKey);
}
void STinsert(itemType aItem)
{
int i = STsearch(realKey(aItem));
if (i >= 0)
{
int j = 0;
while (a[j])
{
j +=1;
}
while (!a[j])
{
a[j] = malloc(1+sizeof(char)*strlen(realKey(aItem)));
}
strcpy(a[j],realKey(aItem));
if (inc)
{
int tmp = isContain(realKey(aItem),a[i]);
if(tmp == KLess)
{
right[i] = j;
}
else if(tmp == KMore)
{
left[i] = j;
}
}
inc++;
}
}