void main() { BiTree dt, p; int i; KeyType j; ElemType r[N] = { {45,1},{12,2},{53,3},{3,4},{37,5},{24,6},{100,7},{61,8},{90,9},{78,10} }; /* 以教科书图9.7(a)为例 */ InitDSTable(&dt); /* 构造空表 */ for (i = 0; i < N; i++) InsertBST(&dt, r[i]); /* 依次插入数据元素 */ TraverseDSTable(dt, print); printf("\n请输入待查找的值: "); scanf("%d", &j); p = SearchBST(dt, j); if (p) { printf("表中存在此值。"); DeleteBST(&dt, j); printf("删除此值后:\n"); TraverseDSTable(dt, print); printf("\n"); } else printf("表中不存在此值\n"); DestroyDSTable(&dt); }
void main() { DLTree t; int i; char s[MAX_KEY_LEN+1]; KeysType k; Record *p; Record r[N]={{{"CAI"},1},{{"CAO"},2},{{"LI"},3},{{"LAN"},4},{{"CHA"},5},{{"CHANG"},6}, {{"WEN"},7},{{"CHAO"},8},{{"YUN"},9},{{"YANG"},10},{{"LONG"},11}, {{"WANG"},12},{{"ZHAO"},13},{{"LIU"},14},{{"WU"},15},{{"CHEN"},16}}; /* 数据元素(以教科书式9–24为例) */ InitDSTable(&t); for(i=0;i<N;i++) { r[i].key.num=strlen(r[i].key.ch); p=SearchDLTree(t,r[i].key); if(!p) /* t中不存在关键字为r[i].key的项 */ InsertDSTable(&t,&r[i]); } printf("按关键字符串的顺序遍历双链键树:\n"); TraverseDSTable(t,print); printf("\n请输入待查找记录的关键字符串: "); scanf("%s",s); k.num=strlen(s); strcpy(k.ch,s); p=SearchDLTree(t,k); if(p) print(*p); else printf("没找到"); printf("\n"); DestroyDSTable(&t); }
void TraverseDSTable(BTree DT, void (*Visit)(BTNode, int)) { int i; if (DT) { for (i = 0; i <= DT->keynum; i++) { if (i > 0) Visit(*DT, i); if (DT->ptr[i]) TraverseDSTable(DT->ptr[i], Visit); } } }
void TraverseDSTable(TrieTree T,void(*Vi)(Record*)) { /* 初始条件:Trie键树T存在,Vi是对记录指针操作的应用函数 */ /* 操作结果:按关键字的顺序输出关键字及其对应的记录 */ TrieTree p; int i; if(T) for(i=0;i<LENGTH;i++) { p=T->a.bh.ptr[i]; if(p&&p->kind==LEAF) Vi(p->a.lf.infoptr); else if(p&&p->kind==BRANCH) TraverseDSTable(p,Vi); } }
//访问数据data int main(int argc,char *argv[]) { BSTree BST,ptr; KeyType key; int i; TElemType R[MAX]={{1012,"0304110512","胡菲菲","女",21,"生物工程"}, {1006,"0304110506","李志昌","男",21,"微电子工程"}, {1005,"0304110505","李经国","男",21,"工程设计"}, {1010,"0304110510","刘京","女",21,"计算机"}, {1003,"0304110503","单加猛","男",21,"通信工程"}, {1004,"0304110504","韩少军","男",23,"软件工程"}, {1011,"0304110511","朱紫婧","女",21,"信息安全"}, {1008,"0304110508","刘高鑫","男",21,"物联网"}, {1001,"0304110501","毕大成","男",21,"动画"}, {1007,"0304110507","刘春功","男",21,"英语"}, {1002,"0304110502","程学涵","男",21,"材料工程"}, {1009,"0304110509","罗金伟","男",21,"机械工程"}}; InitDSTable(BST); for(i=0;i<MAX;i++) { if(!(SearchDSTable(BST,R[i].key))) InsertDSTable(BST,R[i]); } while(1) { printf("输入搜索关键字,0结束:"); scanf("%d%*c",&key); if(key==0) break; if((ptr=SearchDSTable(BST,key))) Visit(ptr->data); else printf("搜索失败!\n"); } TraverseDSTable(BST,Visit); DestroyDSTable(BST); system("pause"); return 0; }
void main() { BSTree dt, p; Status k; int i; KeyType j; ElemType r[N] = { {13,1},{24,2},{37,3},{90,4},{53,5} }; /* (以教科书图9.12为例) */ InitDSTable(&dt); /* 初始化空树 */ for (i = 0; i < N; i++) InsertAVL(&dt, r[i], &k); /* 建平衡二叉树 */ TraverseDSTable(dt, print); /* 按关键字顺序遍历二叉树 */ printf("\n请输入待查找的关键字: "); scanf("%d", &j); p = SearchBST(dt, j); /* 查找给定关键字的记录 */ if (p) print(p->data); else printf("表中不存在此值"); printf("\n"); DestroyDSTable(&dt); }
int main(void) { DLTree t; int i,j=0; KeyType k; Status flag; Record *p,r[N]; FILE *f; InitDSTable(t); f=fopen("1.txt","r"); do { i=fscanf(f,"%s%d",&r[j].key.ch,&r[j].others.order); if(i!=-1) { r[j].key.num=strlen(r[j].key.ch); flag=InsertDSTable(t,&r[j]); if(flag==FALSE) printf("��t���Ѵ��ڹؼ���Ϊ%s�ļ�¼����(%s,%d)�����롣\n", r[j].key.ch,r[j].key.ch,r[j].others.order); j++; } }while(!feof(f)&&j<N); fclose(f); printf("���ؼ��ַ�����˳�������t��\n"); TraverseDSTable(t,Visit); printf("\n����������Ҽ�¼�Ĺؼ��ַ�����"); InputKey(k.ch); k.num=strlen(k.ch); p=SearchDLTree(t,k); if(p) Visit(p); else printf("δ�ҵ�"); printf("\n"); DestroyDSTable(t); return 0; }
void main() { TrieTree t; int i; char s[MAXKEYLEN+1]; KeysType k; Record *p; Record r[N]={{{"CAI"},1},{{"CAO"},2},{{"LI"},3},{{"LAN"},4}, {{"CHA"},5},{{"CHANG"},6},{{"WEN"},7},{{"CHAO"},8}, {{"YUN"},9},{{"YANG"},10},{{"LONG"},11},{{"WANG"},12}, {{"ZHAO"},13},{{"LIU"},14},{{"WU"},15},{{"CHEN"},16}}; /* 数据元素(以教科书式9-24为例) */ InitDSTable(&t); for(i=0;i<N;i++) { r[i].key.num=strlen(r[i].key.ch)+1; r[i].key.ch[r[i].key.num]=Nil; /* 在关键字符串最后加结束符 */ p=SearchTrie(t,r[i].key); if(!p) InsertTrie(&t,&r[i]); } printf("按关键字符串的顺序遍历Trie树(键树):\n"); TraverseDSTable(t,pr); printf("\n请输入待查找记录的关键字符串: "); scanf("%s",s); k.num=strlen(s)+1; strcpy(k.ch,s); k.ch[k.num]=Nil; /* 在关键字符串最后加结束符 */ p=SearchTrie(t,k); if(p) pr(p); else printf("没找到"); printf("\n"); DestroyDSTable(&t); }