打印树

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26760433/article/details/84259523

在学习二叉树时,是不是感觉很抽象,其实我们可以写个程序将二叉树打印在屏幕上。

void print(BTree *root,int h)   
{
	if(root!=NULL)
	{
		print(root->rchild,h+1);
		for(int i=0;i<h;i++)
			cout<<"     ";
		cout<<root->value;
		print(root->lchild,h+1);
	}
	cout<<endl;
}

int main()
{
	print(root,0);
	return 0;
}

输出效果:
在这里插入图片描述
治好了我多年的颈椎病。

猜你喜欢

转载自blog.csdn.net/qq_26760433/article/details/84259523