测试用例
8 7 5 1 –1 –1 –1 4 –1 –1 6 3 –1 –1 2 –1 -1
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
typedef struct node
{
int data;
struct node* lchild, * rchild;
}*Bitree,Binode;
//创建一颗二叉树
void CreatBitree(Bitree& T)
{
int x;
cin >> x;
if (x == -1)
T = nullptr;
else
{
T = new Binode;
T->data = x;
CreatBitree(T->lchild);
CreatBitree(T->rchild);
}
}
//进行递归的中序遍历
void inTraverse(Bitree T)
{
if (T)
{
inTraverse(T->lchild);
cout << T->data << " ";
inTraverse(T->rchild);
}
}
//进行递归的前序遍历
void preTraverse(Bitree& T)
{
if (T)
{
cout << T->data << " ";
preTraverse(T->lchild);
preTraverse(T->rchild);
}
}
//非递归实现中序遍历
void inTraverse2(Bitree T)
{
stack<Bitree>s;
Bitree p = T;
while (p || !s.empty())
{
if(p)
{
s.push(p);
p = p->lchild;
}
else
{
p = s.top();
cout << p->data << " ";
s.pop();
p = p->rchild;
}
}
}
//进行递归的后续遍历
void endTraverse(Bitree& T)
{
if (T)
{
endTraverse(T->lchild);
endTraverse(T->rchild);
cout << T->data << " ";
}
}
//非递归实现后续遍历
void endTraverse2(Bitree T)
{
Bitree p = T,last_v=nullptr;
stack<Bitree>s;
while (p || !s.empty())
{
if (p)
{
s.push(p);
p = p->lchild;
}
else
{
Bitree topT = s.top();//取出顶部元素
if (topT->rchild && topT->rchild != last_v)//如果存在并且还没有访问过
{
p = topT->rchild;
}
else
{
cout << topT->data << " ";
s.pop();
last_v = topT;//访问了右边这个节点
}
}
}
}
//二叉树的层次遍历
void levelTraverse(Bitree T)
{
queue<Bitree>q;
Bitree p = T;
if (p)
{
q.push(p);
while (!q.empty())
{
p = q.front();
cout << p->data << " ";
q.pop();
if (p->lchild)
q.push(p->lchild);
if (p->rchild)
q.push(p->rchild);
}
}
}
//复制一个二叉树
void copyBitree(Bitree T, Bitree& newT)
{
if (!T)
{
newT = nullptr; return;
}
else
{
newT = new Binode;
newT->data= T->data;
copyBitree(T->lchild, newT->lchild);
copyBitree(T->rchild, newT->rchild);
}
}
//求该二叉树的深度
int deptree(Bitree T)
{
int n, m;//左孩子的深度和右孩子的深度
if (!T)
return 0;
else
{
n = deptree(T->lchild);
m = deptree(T->rchild);
return n > m ? (n + 1) : (m + 1);//最后还要加上根节点
}
}
//求二叉树的节点总数
int nodetree(Bitree T)
{
if (!T)
return 0;
else
{
return nodetree(T->lchild) + nodetree(T->rchild) + 1;//左节点加上右节点再加上根节点
}
}
//叶子节点的数量
int leaftree(Bitree T)
{
if (!T)
return 0;
else
{
if (!T->lchild && !T->rchild)//左子树和右子树都不存在
return 1;
else
return leaftree(T->lchild) + leaftree(T->rchild);//左右叶子节点相加
}
}
//从二叉树的叶子节点追踪到根节点
void printtree(Bitree T, int path[],int pathlen)
{
if (T)
{
path[pathlen] = T->data;
if (!T->lchild && !T->rchild)//叶子节点
{
for (int i = pathlen; i >= 0; i--)
cout << path[i] << " ";
cout << "\n";
}
else
{
printtree(T->lchild,path,pathlen+1);
printtree(T->rchild,path,pathlen+1);
}
}
}
int main()
{
Bitree T;
cout << "先序遍历输入二叉树(以-1结束)" << endl;
CreatBitree(T);
cout << "先序遍历输出" << endl;
preTraverse(T);
cout << endl;
cout << "中序递归遍历输出" << endl;
inTraverse(T);
cout << endl;
cout << "中序非递归遍历输出" << endl;
inTraverse2(T);
cout << endl;
cout << "后序递归遍历输出" << endl;
endTraverse(T);
cout << endl;
cout << "后序非递归遍历输出" << endl;
endTraverse2(T);
cout << endl;
cout << "二叉树的层次遍历" << endl;
levelTraverse(T);
cout << endl;
cout << "树的深度" << endl<<deptree(T)<<endl;
cout << "节点的个数" << endl << nodetree(T) << endl;
cout << "叶子节点的数量" << endl << leaftree(T) << endl;
cout << "二叉树到根节点的所有路径" << endl;
int path[256],pathlen=0;
printtree(T, path, pathlen);
cout << endl;
return 0;
}