数据结构实验二 树的应用.docxVIP

  • 6
  • 0
  • 约2.47千字
  • 约 6页
  • 2021-01-15 发布于湖北
  • 举报
数据结构实验二 树得应用(代码测试界面) //Cosmetics_Info、h #include stdio、h #include stdlib、h #include string、h typedef struct { //化妆品信息得结构体 char brandname[10]; //品牌名 double price; //价格 }datatype; typedef struct node{ //二叉排序树链表得结点结构 datatype data; //结点信息 struct node *lchild, *rchild; //指向左孩子与右孩子得指针 } bintnode; typedef bintnode *bintree; //结点指针类型 bintree root; //指向二叉树根结点得指针 void InsertBintree(bintree *t, datatype addnode) //创建新结点 { bintree f = NULL, p = *t; //p指向根结点 while(p) //每次从根结点开始比较,查找插入位置 { if ( strcmp(addnode、brandname,p-data、brandname)==0addnode、price==p-data、price ) { printf(已录入该化妆品信息,本次录入无效!\n); return; }//若二叉排序树中已含addnode,则无需插入,ruturn退出函数 f = p; //f用于保存新结点得最终插入位置 p = ((addnode、price) (p-data、price))? p-lchild:p-rchild; } p=(bintree)malloc(sizeof(bintnode)); //生成待插入得新结点 p-data=addnode; //将新结点加到树上 p-lchild=p-rchild=NULL; if(*t==NULL) *t=p; //原树为空,将值赋值给根结点 else { if(addnode、pricef-data、price) f-lchild=p; else f-rchild=p; } } bintree CreatBintree() //创建二叉排序树链表 { bintree t = NULL; datatype addnode; //录入化妆品信息 printf(\end\,\-1\为结束标记:\n); scanf(%s,addnode、brandname); scanf(%lf,addnode、price); while((strcmp(addnode、brandname,end)!=0)||(addnode、price!=-1)) { InsertBintree(t,addnode); //将addnode插入二叉排序树 scanf(%s,addnode、brandname); scanf(%lf,addnode、price); } printf(化妆品信息二叉排序树建立成功!\n); return t; //返回二叉排序树得根地址 } void inorder(bintree t) //递归实现二叉树中序遍历输出 { if(t){ inorder(t-lchild); printf(\t\t 品牌名:%s ,t-data、brandname); printf(\t价格:%g元\n,t-data、price); inorder(t-rchild); } } //main、c #include stdio、h #include stdlib、h #include string、h #include Cosmetics_Info、h int main() { int s=1; int set; while(s) { printf(\t\t╔───────────╗\n); printf(\t\t│简单化妆品信息处理系统│\n); printf(\t\t╠───────────╣\n); printf(\t\t│ 1、录入化妆品信息 │\n); printf(\t\t│ 2、输出化妆品信息 │\n); printf(\t\t│ 3、退出系统 │\n); printf(\t\t╚───────────╝\n); printf(选择操作:); scanf(%d,set); switch(set){

文档评论(0)

1亿VIP精品文档

相关文档