二叉排序树-C语言程序

蚊子前端博客
发布于 2012-03-14 07:00
曾经在数据结构的时候做过的实验,最近忽然发现了这个程序,闲来无事就发到网上吧

曾经在数据结构的时候做过的实验,最近忽然发现了这个程序,闲来无事就发到网上吧。

COPYC

///创建一个由多张化妆品品牌价格组成的二叉排序树,并按照价格从低到高的顺序打印输出 ///定义二叉排序树链表的结点结构 ///依次输入各类化妆品品牌的价格并按二叉排序树的要求创建一个二叉排序树链表 ///对二叉排序树进行中序遍历输出,打印按价格从低到高顺序排列的化妆品品牌信息 ///system("color 16进制16进制");第一个16进制数表示背景色,第二个表示前景色 ///0 1 2 3 4 5 6 7 8 9 a b c d e f ///黑 蓝 绿 湖蓝 红 紫 黄 白 灰 淡蓝 淡绿 淡浅绿 淡红 淡紫 淡黄 亮白 #include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> typedef struct node { char name[20]; //名称 float price; //价格 char productor[50]; //生产商 char pdate[15]; //生产日期 int day; //有效天数 char fuction[200]; //化妆品功能,暂未添加 struct node *lchild; struct node *rchild; } toiletry; toiletry *typein(toiletry *t); ///插入操作 void inorder(toiletry *t); ///中序遍历输出 toiletry *del(toiletry *t); ///删除某个化妆品的信息 void error(); ///输入错误处理 struct tm *newtime; char tmpbuf[128]; time_t lt1; int main() { toiletry *head=NULL; int n; while(1) { system("cls"); ///刷新 system("color 0e"); time( <1 ); newtime=localtime(<1); strftime( tmpbuf, 128, " 今天: %Y %m月%d日 %Ann", newtime); printf(tmpbuf); printf("n 欢迎使用化妆品系统nn"); printf(" ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n"); printf(" ☆ 【1】 化妆品信息输入 ☆n"); printf(" ☆ 【2】 化妆品信息输出 ☆n"); printf(" ☆ 【3】 化妆品信息删除 ☆n"); printf(" ☆ 【0】 退出 ☆n"); printf(" ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆n"); scanf("%d", &n); if(n==1) head=typein(head); else if(n==2) inorder(head); else if(n==3) head=del(head); else if(n==0) break; else error(); } system("cls"); system("color 02"); printf("n * *谢谢使用* *n"); } toiletry *typein(toiletry *t) { toiletry *f, *q, *p=(toiletry *)malloc(sizeof(toiletry)); p->lchild=p->rchild=NULL; char na[20], pro[50], pdate[15]; float price; int day, k=1; system("cls"); system("color 0f"); time( <1 ); newtime=localtime(<1); strftime( tmpbuf, 128, " 今天: %Y %m月%d日 %Ann", newtime); printf(tmpbuf); printf(" ☆☆☆☆☆现在开始插入操作☆☆☆☆☆nn"); printf(" 请输入化妆品的信息:名称,价格,生产商,生产日期,有效天数:n"); q=t; scanf("%s %f %s %s %d", p->name, &p->price, p->productor, p->pdate, &p->day); while(q) { if(p->price==q->price) { k=0; break; } f=q; q=(p->price<q->price)?q->lchild:q->rchild; } if(k) { if(t==NULL) t=p; else { if(p->price<f->price) f->lchild=p; else f->rchild=p; } printf("n 插入成功,按回车返回……"); } else printf("n 化妆品价格已存在,按回车返回……"); fflush (stdin); getchar (); return t; } void inorder(toiletry *t) { system("cls"); system("color 0d"); toiletry *p=t; toiletry *stack[1000]; int top=-1; time( <1 ); newtime=localtime(<1); strftime( tmpbuf, 128, " 今天: %Y %m月%d日 %Ann", newtime); printf(tmpbuf); printf(" 名称 价格 生产商 生产日期 有效天数nn"); while((p!=NULL) || (top!=-1)) { while(p) { top++; stack[top]=p; p=p->lchild; } if(top!=-1) { p=stack[top]; top--; printf(" %-10s%-8.2f%-15s%-12s%-5dn", p->name, p->price, p->productor, p->pdate, p->day); p=p->rchild; } } if(t) printf("n 输出完毕,按回车返回……"); else printf("n 文件为空,按回车返回……"); fflush (stdin); getchar (); } toiletry *del(toiletry *t) { toiletry *p, *q=NULL, *f; float price; char c; system("cls"); system("color 0a"); printf(" ☆☆☆☆☆现在开始删除操作☆☆☆☆☆nn"); if(t==NULL) { printf(" 对不起,文件为空,不能删除nn"); printf(" 请按回车返回……"); fflush (stdin); getchar (); return t; } printf(" 请输入您要删除的化妆品信息的价格 price = "); scanf("%f", &price); getchar(); p=t; while(p)//查找要删除的结点 { if(p->price==price) break; q=p; p=(price<p->price)?p->lchild:p->rchild; } if(p==NULL)//没有找到价格对应的化妆品的信息 { printf("n 对不起,不存在该价格的化妆品nn"); printf(" 请按回车返回……"); fflush (stdin); getchar (); return t; } printf("n 您要删除的化妆品的信息为:nn"); printf(" 名称 价格 生产商 生产日期 有效天数nn"); printf(" %-10s%-8.2f%-15s%-12s%-5dn", p->name, p->price, p->productor, p->pdate, p->day); printf("nn 您确定要删除吗?Y/Nn"); scanf("%c", &c); if(c=='Y' || c=='y') { if(p->lchild==NULL && p->rchild==NULL)//待删结点的左右孩子都为空 { if(q)//如果待删结点有双亲 { if(p==q->lchild) q->lchild=NULL; else q->rchild=NULL; } else t=NULL; free(p); } else if(p->lchild!=NULL && p->rchild==NULL)//待删结点的左孩子不为空 { if(q)//如果待删结点有双亲 { if(p==q->lchild) q->lchild=p->lchild; else q->rchild=p->lchild; } else t=p->lchild; free(p); } else if(p->lchild==NULL && p->rchild!=NULL)//待删结点的右孩子不为空 { if(q)//如果待删结点有双亲 { if(p==q->lchild) q->lchild=p->rchild; else q->rchild=p->rchild; } else t=p->rchild; free(p); } else { f=p->rchild; while(f->lchild) f=f->lchild; f->lchild=p->lchild; if(q) { if(q->lchild==p) q->lchild=p->rchild; else q->rchild=p->rchild; } else t=p->rchild; free(p); } printf("n 已经删除成功,按回车返回……"); } else if(c=='N' || c=='n') printf("n 已经取消删除,按回车返回……"); fflush (stdin); getchar (); return t; } void error() { system("cls"); printf("nn 您的输入有误,按回车后请重新输入……"); fflush (stdin); getchar (); }
标签:
阅读(535)

公众号:

qrcode

微信公众号:前端小茶馆

公众号:

qrcode

微信公众号:前端小茶馆