求二叉树的高度以及二叉树的树形显示

该博客介绍了如何使用递归方法计算二叉树的高度,并详细讲解了如何根据满二叉树的性质进行二叉树的树形显示,提供了相应的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉树的高度用递归的方式求解,二叉树的树形显示利用其对应满二叉树的位置输出。

代码:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;
typedef struct BinTreeNode * BinTree;
typedef struct BinTreeNode
{
	char data;
	BinTree lChild,rChild;
} BinTreeNode;

int max(int a,int b)
{
	return a>b?a:b;
}
void BuildBinTree(BinTree *T)
{
	char item;
	cin>>item;
	if(item=='#')
	{
		*T = NULL;
	}
	else {
		*T = new BinTreeNode;
		(*T)->data = item; 
		BuildBinTree(&((*T)->lChild));
		BuildBinTree(&((*T)->rChild));
	}
}
int Height(BinTree T)
{
	if (!T)
		return 0;
	return 1 + max(Height(T->lChild),Height(T->rChild));
	
}
void DestroyBinTree(BinTree *T)
{
	if(*T)
	{
		DestroyBinTree(&((*T)->lChild));
		DestroyBinTree(&((*T)->rChild));
		delete (*T);
		*T = NULL;
	}	
} 

void ShowTreeMarker(int **m,int row,int col,char *s)
{
	int cnt = 0;
	for(int i=0;i<row;i++)
	{
		for(int j=0;j<col;j++)
		{
			if(m[i][j]) 
				cout<<setw(2)<<s[cnt++];
			else
				cout<<setw(2)<<' ';		
		}
		cout<<endl;
	}
}
void MatCreate(int *c,int **m,int level)
{
	int i,j;
	int start,step;
	int len = (1<<level) - 1;
	m[0][len>>1] = 1;
	int d = len;
	int cnt = 0;
	for(i=1;i<level;i++)
	{
		//the (i-1)th row
		d = d>>1;
		start = (d>>1);
//		if (c[++cnt])
//			m[i][start] = 1;
		for(j=1;j<=(1<<i);j++)
			if(c[++cnt])
			  	m[i][start+(d+1)*(j-1)] = 1;
	}
		
	
}

void Pave(BinTree T,int *c,int start)
{
	if(T)
	{
		c[start] = 1;
		Pave(T->lChild, c, 2 * start + 1);
		Pave(T->rChild, c, 2 * start + 2);
	}
}
int main()
{
	BinTree T = NULL;
	BuildBinTree(&T);
	int h = Height(T);
	int len = (1<<h) - 1;
	int *c = new int[len];
	memset(c,0,len*sizeof(int));
	Pave(T,c,0);
	for(int i = 0; i < len; i++)
		cout<<c[i]<<" ";
	cout<<endl; 
	cout<<"Height of BinTree : "<<h<<endl;
	cout<<(1<<h) - 1<<endl;
	
	int **m = new int*[h];
	for(int i= 0;i<h;i++){
		m[i] = new int[len];
		memset(m[i],0,len*sizeof(int));	
	}
	MatCreate(c,m,h);
	for(int i=0;i<h;i++)
	{
		for(int j=0;j<len;j++)
			cout<<setw(2)<<m[i][j];
		cout<<endl;
	}
	ShowTreeMarker(m,h,len,"ABCDEFGHIJK");	
	
	DestroyBinTree(&T);
	
} 
测试输入:

ABD##E##CF###

测试输出:

1 1 1 1 1 1 0 
Height of BinTree : 3
7
 0 0 0 1 0 0 0
 0 1 0 0 0 1 0
 1 0 1 0 1 0 0
       A      
   B       C  
 D   E   F    


1、树状显示二叉树: 编写函数displaytree(二叉树的根指针,数据值宽度,屏幕的宽度)输出树的直观示意图。输出的二叉树是垂直打印的,同层的节点在同一行上。 问题描述: 假设数据宽度datawidth=2,而屏幕宽度screenwidth为64=26,假设节点的输出位置用 (层号,须打印的空格数)来界定。 第0层:根在(0,32)处输出; 第1层:因为根节点缩进了32个空格,所以下一层的偏移量(offset)为32/2=16=screenwidth/22。即第一层的两个节点的位置为(1,32-offset),(1,32+offset)即(1,16),(1,48)。 第二层:第二层的偏移量offset为screenwidth/23。第二层的四个节点的位置分别是(2,16-offset),(2,16+offset),(2,48-offset),(2,48+offset)即(2,8),(2,24),(2,40),(2,56)。 …… 第i层:第i层的偏移量offset为screenwidth/2i+1。第i层的每个节点的位置是访问第i-1层其双亲节点时确定的。假设其双亲的位置为(i-1,parentpos)。若其第i层的节点是其左孩子,那末左孩子的位置是(i,parentpos-offset),右孩子的位置是(i,parentpos+offset)。 提示:利用二叉树的层次遍历算法实现。利用两个队列Q,QI。队列Q中存放节点信息,队列QI中存相应于队列Q中的节点的位置信息,包括层号和需要打印节点值时需要打印的空格数。当节点被加入到Q时,相应的打印信息被存到QI中。二叉树本身采用二叉链表存储。 2、完全二叉树判断 用一个二叉链表存储的二叉树,判断其是否是完全二叉树
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值