#include<iostream>
#include<queue>
using namespace std;
typedef struct BinTree
{
char data; //节点信息
struct BinTree *lchild, *rchild; //左右孩子
} BiTNode, *BiTree;
//*BiTree的意思是给 struct node*起了个别名,叫BiTree,
//故BiTree为指向节点的指针。
//初始化二叉树,可以省略。
void InitBiTree(BiTree &T)
{
T = NULL;
}
//判断树是否为空
int EmptyBiTree(BiTree T)
{
if(!T) return 1;
else return 0;
}
//求树的深度
int DepthBiTree(BiTree T)
{
int count1, count2;
if(!T) return 0;
count1 = DepthBiTree(T->lchild); //数左子树深度
count2 = DepthBiTree(T->rchild); //数右子树深度
if(count1 > count2) return count1 + 1; //深度大的加上根节点
else return count2 + 1;
}
//求节点数
int NodeCount(BiTree T)
{
if(!T) return 0;
//左加右加根
else return NodeCount(T->lchild) + NodeCount(T->rchild) + 1;
}
//销毁二叉树
void DestoryBiTree(BiTree &T)
{
if(!T)
{
if(T->lchild)
DestoryBiTree(T->lchild);
if(T->rchild)
DestoryBiTree(T->rchild);
delete(T);
T = NULL;
}
}
void ClearBiTree(BiTree &T)
{
if(T)
{
ClearBiTree(T->lchild);
ClearBiTree(T->rchild);
delete(T);
T = NULL;
}
}
//创建二叉树
void CreateBiTree(BiTree &T)
{
//&的意思是传进来节点指针的引用,括号内等价于 BiTreeNode* &T,
//目的是让传递进来的指针发生改变
char ch; //输入二叉树结点值
cin >> ch;
if(ch == '#')
T = NULL; //当遇到#时,令树的根节点为NULL,
//从而结束该"分支"的递归(如 a##表示a无子孩子)
else
{
T = new BiTNode; //先序遍历建立二叉树链表
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
//统计叶子节点
void CountLeaf(BiTree T, int &count)
{
if (T)
{
if ((!T->lchild) && (!T->rchild)) //左右子树都无
{
count++;
}
CountLeaf(T->lchild, count);
CountLeaf(T->rchild, count);
}
}
void PreTree(BiTree T) //先序遍历
{
if (T)
{
cout << T->data << ","; //根
PreTree(T->lchild); //左
PreTree(T->rchild); //右
}
}
void MidTree(BiTree T) //中序遍历
{
if (T)
{
MidTree(T->lchild); //左
cout << T->data << ","; //根
MidTree(T->rchild); //右
}
}
void PostTree(BiTree T)
{
if (T)
{
PostTree(T->lchild); //左
PostTree(T->rchild); //右
cout << T->data << ","; //根
}
}
//层次遍历
void LevelOrder(BiTree T)
{
queue<BiTree> q; //建立队列Q
q.push(T); //根节点入队
while(!q.empty())
{
BiTree t = q.front();
if (t->lchild) //将左子树入队
q.push(t->lchild);
if (t->rchild) //将右子树入队
q.push(t->rchild);
cout << t->data << " "; //输出队首
q.pop(); //队首出队
}
}
//查找
int FindBiTree(BiTree T, int e)
{
if(!T) return 0;
if((T->data == e)) return 1;
if(FindBiTree(T->lchild, e) || FindBiTree(T->rchild, e))
return 1;
}
int main()
{
BiTree tree;
int count = 0;
cout << "初始化树\n";
InitBiTree(tree);
cout << "(1)This BiTree is " << (EmptyBiTree(tree) ? "Empty" : "Not Empty") << " now\n";
cout << "(2)请输入二叉树(如ab##c##,即根节点为a,左孩子b,又孩子c):\n";
CreateBiTree(tree);
cout << "(3)树已建立。This BiTree is " << (EmptyBiTree(tree) ? "Empty" : "Not Empty") << " now\n";
cout << "(4)树的节点总数为:" << NodeCount(tree) << endl;
CountLeaf(tree, count);
cout << "(5)树的叶子节点数为:" << count;
cout << "\n(6)树的深度为:" << DepthBiTree(tree);
cout << "\n(7)树的先序遍历为:";
PreTree(tree);
cout << "\n(8)树的中序遍历为:";
MidTree(tree);
cout << "\n(9)树的后序遍历为:";
PostTree(tree);
cout << "\n(10)树的层次遍历为:";
LevelOrder(tree);
cout << "\n(11)This BiTree is " << (EmptyBiTree(tree) ? "Empty" : "Not Empty") << " now\n";
char x;
cout << "(12)请输入要查找的字符X:";
cin >> x;
cout << (FindBiTree(tree, x) ? "Bingo" : "owl") << endl;
ClearBiTree(tree);
cout << "(13)已调用Clear()";
cout << "(14)This BiTree is " << (EmptyBiTree(tree) ? "Empty" : "Not Empty") << endl;
return 0;
}
/*
请输入二叉树(如ab##c##,即根节点为a,左孩子b,又孩子c):
abd##e##c##
树的叶子节点数为:3
树的先序遍历为:a,b,d,e,c,
树的中序遍历为:d,b,e,a,c,
树的后序遍历为:d,e,b,c,a,
*/
结果如下: