| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 647 人关注过本帖
标题:[原创]搜索树代码
只看楼主 加入收藏
xtxei2468
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-1-4
收藏
 问题点数:0 回复次数:0 
[原创]搜索树代码

很早以前的了 看打家有用没



#include<iostream>

#include<iomanip>

#include<malloc.h>

#include<string>

using namespace std;

struct Date{

int year;

int mon;

int day;

};

typedef struct{

double key;

char name[20];

char sex[8];

Date date1;

char adress[50];

double tel;

}ElemType;

typedef struct BiTNode{

ElemType data;

struct BiTNode *lchild,*rchild;

}BiTNode,*BiTree;

#define EQ(a,b) ((a)==(b))

#define LT(a,b) ((a)< (b))

#define LQ(a,b) ((a)> (b))

BiTree p=NULL,s=NULL;

bool SearchBST(BiTree T,double key,BiTree f,BiTree &p){

if(!T){

p=f;return false;

}

else if EQ(key,T->data.key){

p=T;return true;

}

else if LT(key,T->data.key)return SearchBST(T->lchild,key,T,p);

else return SearchBST(T->rchild,key,T,p);

}//SearchBST

BiTree Search_BST(BiTree T,double key){

if(!T||EQ(key,T->data.key))return(T);

else if LT(key,T->data.key)return(Search_BST(T->lchild,key));

else return Search_BST(T->rchild,key);

}//SearchBST

void Input_Information(BiTree &T,ElemType e);

bool InsertBST(BiTree &T,ElemType e){

if(!SearchBST(T,e.key,NULL,p)){

s=(BiTree)malloc(sizeof(BiTNode));

Input_Information(s,e);

s->lchild=NULL;s->rchild=NULL;

if(!p)T=s;

else if LT(e.key,p->data.key)p->lchild=s;

else p->rchild=s;

return true;

}

else return false;

}//insert

void Input_Information(ElemType &e){

cout<<"intput telphone NO:";

cin>>e.key;

cout<<"intput name:";

cin>>e.name;

cout<<"intput sex:";

cin>>e.sex;

cout<<"intput date(Year/month/day):";

cin>>e.date1.year

>>e.date1.mon

>>e.date1.day;

cout<<"intput adress:";

cin>>e.adress;

cout<<"intput other Telphone NO:";

cin>>e.tel;

}//information

void Input_Information(BiTree &T,ElemType e){

T->data.key=e.key;

strcpy(T->data.name,e.name);

strcpy(T->data.sex,e.sex);

T->data.date1.year=e.date1.year;

T->data.date1.mon=e.date1.mon;

T->data.date1.day=e.date1.day;

strcpy(T->data.adress,e.adress);

T->data.tel=e.tel;

}//information

void print(BiTree T){

cout<<"姓名:"<<T->data.name<<endl

<<"电话:"<<setiosflags(ios::fixed)<<T->data.key<<endl

<<"性别:"<<T->data.sex<<endl

<<"生日:"<<T->data.date1.year<<""

<<T->data.date1.mon<<""

<<T->data.date1.day<<""<<endl

<<"地址:"<<T->data.adress<<endl

<<"其他常用号码:"<<setiosflags(ios::fixed)<<T->data.tel;

}//print

void main(){

ElemType e;

int loop=1;

double key;

string str;

BiTree Root,q;

Root=(BiTree)malloc(sizeof(BiTNode));

Root->lchild=NULL;

Root->rchild=NULL;

//Input_Information(Root);//

while(loop){

Input_Information(e);

if(!InsertBST(Root,e))return;

cout<<"是否继续输入电话薄联系人信息?(Y OR N)";

cin>>str;

if(str=="Y"||str=="y")

loop=1;

else loop=0;

}

cout<<"是否查找联系人详细信息?(Y OR N)";

cin>>str;

loop=(str=="Y"||str=="y")?1:0;

while(loop){

cout<<"输入查找人的电话号码:";

cin>>key;

if(q=Search_BST(Root,key))

print(q);

else cout<<"查找失败!"<<endl;

cout<<"是否继续查找其他联系人?(Y OR N)";

cin>>str;

loop=(str=="Y"||str=="y")?1:0;

}

}//main

搜索更多相关主题的帖子: 搜索 代码 
2006-01-04 20:29
快速回复:[原创]搜索树代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017268 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved