在链式存储结构上建立一棵二叉排序树

代码如下:

#define n 10
typedef struct node{int key;struct node *lchild,*rchild;}Bitree;
void bstinsert(Bitree *&bt,int key)
{
	if(bt==0)
	{
		bt=(struct node *)malloc(sizeof(struct node));
		bt->key=key;
		bt->lchild=bt->rchild=0;
	}else if(bt->key>key)
	{
		bstinsert(bt->lchild,key);	
	}else
	{
		bstinsert(bt->rchild,key);
	}
}
void creatbitree(Bitree *&bt)
{
	int i;
	randomize();//这里是为了保证随机生成不同的随机数
	for(i=0;i<n;i++)
	{
		bstinsert(bt,random(100));
	}
}
发布了67 篇原创文章 · 获赞 25 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_41104871/article/details/101447752
今日推荐