📄 2503.cpp
字号:
#define MAX_WORD_LENGTH 11
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
typedef struct BSTNode
{
char key[MAX_WORD_LENGTH];
char value[MAX_WORD_LENGTH];
BSTNode *lchild;
BSTNode *rchild;
}BSTNode,*BSTree;
BSTree t=NULL;
void insert_tree(char * key,char * value)
{
BSTree new_node=(BSTree)malloc(sizeof(BSTNode));
new_node->lchild=NULL;
new_node->rchild=NULL;
strcpy(new_node->key,key);
strcpy(new_node->value,value);
BSTree y=NULL;
BSTree x=t;
while(x)
{
y=x;
x=strcmp(x->key,new_node->key)>0?x->lchild:x->rchild;
}
if(t==NULL)
t=new_node;
else
{
if(strcmp(y->key,key)>=0)
y->lchild=new_node;
else
y->rchild=new_node;
}
}
char* search(char * key)
{
//char * c=(char*)malloc(sizeof())
BSTree x=t;
while(x!=NULL&&strcmp(x->key,key)!=0)
{
if(strcmp(key,x->key)<0)
x=x->lchild;
else if(strcmp(key,x->key)>0)
x=x->rchild;
/*else
return x;*/
}
if(x!=NULL&&strcmp(x->key,key)==0)
return x->value;
else
return "eh";
}
int main()
{
char key[MAX_WORD_LENGTH],value[MAX_WORD_LENGTH];
while(1)
{
value[0]=getchar();
if(value[0]=='\n')
break;
scanf("%s %s",value+1,key);
getchar();
insert_tree(key,value);
}
while(scanf("%s",key)!=-1)
{
printf("%s\n",search(key));
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -