二叉树的高度用递归的方式求解,二叉树的树形显示利用其对应满二叉树的位置输出。
代码:
#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(