C語言實現(xiàn)樹的動態(tài)查找實例代碼
C語言實現(xiàn)樹的動態(tài)查找實例代碼
本例演示一種樹數(shù)據(jù)結(jié)構(gòu)存儲記錄集合時的動態(tài)查找方法。首先程序通過construct()函數(shù),利用已經(jīng)存在的結(jié)構(gòu)體數(shù)組數(shù)據(jù)建立一個二叉樹,建立樹的過程中,要保證每個節(jié)點的值都大于它的左子樹上節(jié)點的值而小于它右子樹所有節(jié)點的值,該函數(shù)返回建立樹的根指針;然后通過函數(shù)Search(root,name)查找,如果找到相應(yīng)的數(shù)據(jù),將其打印出來,如果沒有找到,則用戶可以選擇是否將該數(shù)據(jù)插入到樹中。
具體代碼如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 4
struct tree
{
char name[20];
char city[20];
char sex[10];
char age[10];
char job[10];
struct tree *left;
struct tree *right;
};
struct tree Datas[NUM]=
{
"Willing","Tianjing","Female","21","worker",NULL,NULL,
"Tom","Beijing","Male","31","doctor",NULL,NULL,
"Sun","Weifang","Male","24","student",NULL,NULL,
"Marry","Shanghai","Female","19","techer",NULL,NULL
};
struct tree *construct(
struct tree *root,
struct tree *r,
struct tree *Data)
{
if(!r)
{
r = (struct tree *)malloc(sizeof(struct tree));
if(!r)
{
printf("內(nèi)存分配失?。?);
exit(0);
}
r->left = NULL;
r->right = NULL;
strcpy(r->name,Data->name);
strcpy(r->city,Data->city);
strcpy(r->sex,Data->sex);
strcpy(r->age,Data->age);
strcpy(r->job,Data->job);
if(!root)
return r;
if(strcmp(Data->name,root->name)<0)
root->left = r;
else
root->right = r;
return r;
}
if(strcmp(Data->name,r->name)<0)
construct(r,r->left,Data);
else
construct(r,r->right,Data);
return root;
}
struct tree *Search(root,name)
struct tree *root;
char name[];
{
struct tree *p;
if(root == NULL)
printf("該樹為空\n");
p = root;
while(strcmp(p->name,name)!=0)
{
if(strcmp(p->name,name)>0)
p = p->left;
else
p = p->right;
if(p == NULL)
break;
}
return(p);
}
void print(struct tree *r)
{
if(!r)
return;
print(r->left);
printf("%s\n",r->name);
print(r->right);
}
void print_currentData(struct tree *point)
{
if(point == NULL)
return;
printf(" 姓名:%s\n",point->name);
printf(" 城市:%s\n",point->city);
printf(" 性別:%s\n",point->sex);
printf(" 年齡:%s\n",point->age);
printf(" 工作:%s\n",point->job);
}
int main(void)
{
int i;
char c[10];
char swap[20];
char name[20];
struct tree *root,*p;
struct tree *temp;
p = NULL;
temp = NULL;
root = NULL;
for(i = 0;i<NUM;i++)
root =construct(root,root,&Datas[i]);
printf("現(xiàn)有人員資料:\n");
print(root);
printf("請輸入要查找的人的名字\n");
scanf("%s",name);
p = Search(root,name);
if(p == NULL)
{
printf("沒有該人資料\n");
printf("是否要插入該人資料[y/n]\n");
scanf("%s",c);
if(strcmp(c,"y")==0)
{
temp = (struct tree *)malloc(sizeof(struct tree));
if(!temp)
{
printf("內(nèi)存分配失敗!");
exit(0);
}
printf("請輸入該人姓名:\n");
scanf("%s",swap);
strcpy(temp->name,swap);
printf("請輸入該人所在城市:\n");
scanf("%s",swap);
strcpy(temp->city,swap);
printf("請輸入該人性別[Male/Female]:\n");
scanf("%s",swap);
strcpy(temp->sex,swap);
printf("請輸入該人年齡:\n");
scanf("%s",swap);
strcpy(temp->age,swap);
printf("請輸入該人工作:\n");
scanf("%s",swap);
strcpy(temp->job,swap);
temp->left = NULL;
temp->right = NULL;
root =construct(root,root,temp);
print_currentData(temp);
printf("現(xiàn)有人員資料:\n");
root = root;
print(root);
}
else
return 0;
}
print_currentData(p);
return 1;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
C++ OpenCV學(xué)習(xí)之圖像像素值統(tǒng)計
在圖像分析的時候,我們經(jīng)常需要對單通道圖像的像素進行統(tǒng)計。本文將主要介紹利用C++ OpenCV實現(xiàn)的圖像像素值統(tǒng)計的幾種方法,需要的可以參考一下2022-01-01
詳解C語言中fseek函數(shù)和ftell函數(shù)的使用方法
這篇文章主要介紹了C語言中fseek函數(shù)和ftell函數(shù)的使用方法,兩個函數(shù)分別用于設(shè)置和返回文件指針stream的位置,需要的朋友可以參考下2016-03-03
C++20 新特性 協(xié)程 Coroutines(2)
上篇文章簡單給大介紹了 C++20 特性 協(xié)程 Coroutines co_yield 和 co_return 那么這篇文章繼續(xù)給大家介紹C++20 的新特性協(xié)程 Coroutines co_await,需要的朋友可以參考一下2021-10-10
C語言pow()函數(shù)實現(xiàn)求x的y次方的值
這篇文章主要介紹了C語言pow()函數(shù)實現(xiàn)求x的y次方的值,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
C/C++內(nèi)存管理之new與delete的使用及原理解析
這篇文章主要介紹了C/C++內(nèi)存管理之new與delete的使用及原理解析,本文通過實例代碼圖文相結(jié)合給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-08-08
c與c++之間的相互調(diào)用及函數(shù)區(qū)別示例詳解
這篇文章主要為大家介紹了c與c++相互調(diào)用的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06

