Binary Sorting Tree_Cosmetics_v1.0

来源:互联网 发布:数据库管理器 编辑:程序博客网 时间:2024/06/07 06:47
#include<stdio.h>
#include<stdlib.h>
typedef int datatype;
typedef struct node
{
char brand[20];
datatype pride;
struct node *lchild, *rchild;
}bsnode;
typedef bsnode *bstree;


void bssearch(bstree t, datatype x, bstree *f, bstree *p)
{
*f = NULL;
*p = t;
while (*p)
{
if (x == (*p)->pride)
return;
*f = *p;
*p = (x < (*p)->pride ? (*p)->lchild : (*p)->rchild);


}
}


void InsertBstree(bstree *t, datatype x,char b[])
{
bstree f = NULL, p;
int i;
p = *t;
while (p)
{
if (x == p->pride)
return;
f = p;
p = (x < p->pride) ? p->lchild : p->rchild;
}
p = (bstree)malloc(sizeof(bsnode));
p->pride = x;
for (i = 0; b[i] != '\0'; i++)
p->brand[i] = b[i];
p->brand[i] = '\0';
p->lchild = p->rchild = NULL;
if (*t == NULL)
*t = p;
else
if (x < f->pride)
f->lchild = p;
else
f->rchild = p;
}


bstree CreatBstree(bstree t)
{
char b[20];
datatype pr;
printf("输入品牌:");
scanf_s("%s", b, 20);
printf("输入%s价格:",b);
scanf_s("%d", &pr, 20);
InsertBstree(&t, pr, b);
return t;
}


void inorder(bstree t)
{
int i = 1;
if (t)
{
inorder(t->lchild);
printf("%s-%d\n",t->brand, t->pride);
inorder(t->rchild);
}
}


void main()
{
bstree t = NULL;
int a,i;
printf("您要输入几条数据:");
scanf_s("%d", &a, 1);
for(i=0;i<a;i++)
t = CreatBstree(t);
printf("********************************\n");
inorder(t);
printf("********************************\n");
}
原创粉丝点击