二叉排序树

 二叉排序树,包括建树的整个过程,还有他的数据结构,应仔细体会。
// 2418-二叉排序树.cpp : 定义控制台应用程序的入口点。


#include
"stdafx.h"

#include
<iostream>
using namespace std;

struct node
{
node
*left;
node
*right;
double num;
char str[59];
}
*head;//这里建了头指针
double sum=0;

void insert(char str[])
{
node
*p1,*p2;
p1
=head;
int t;
while(p1!=NULL)
{
p2
=p1;
t
=strcmp(p1->str,str);
if(t==0)
{
p1
->num++;
return;

}
else if(t<0)
{
p1
=p1->right;
continue;
}
else
{
p1
=p1->left;
continue;
}
}
node
* add=new node;
add
->num=1;
add
->left=add->right=NULL;
strcpy(add
->str,str);
if(t>0)
p2
->left=add;
else
p2
->right=add;
return ;

}



void pprint(node *p)
{
if(p!=NULL)
{
pprint(p
->left);
printf(
"%s %.4f\n",p->str,(p->num)*100/sum);
pprint(p
->right);
}

}
int main()
{
char str[59];

head=new node;//这里是新建了个节点,并让头指针指向他
head
->left=NULL;
head
->right=NULL;
head
->num=1;
sum
=1;
gets(str);
strcpy(head
->str,str);
head
->num=1;

while(gets(str)!=NULL)
{
sum
++;
insert(str);
}
pprint(head);
system(
"pause");
return 0;
}



posted @ 2011-05-25 15:07  张兰云  阅读(256)  评论(0编辑  收藏  举报