目录
二叉树需要明确的是其左右子树的位置是不可改变的;
下面是一些二叉树的基本操作:
二叉树的构造:输入二叉树的先序遍历,空子树用#代替
Bitree Createtree(Bitree T) //根据先序遍历的序列构造二叉树
{
char s;
cin >> s;
int i = 0;
if (s == '#')
T = NULL;
else {
T = (Bitnode*)malloc(sizeof(Bitnode));
T->data = s;
T->lchild = Createtree(T->lchild);
T->rchild = Createtree(T->rchild);
}
return T;
}
前序遍历(递归、非递归)
void Preorder(Bitree T)//前序遍历
{
if (T != NULL)
{
cout<<T->data<<" ";
Preorder(T->lchild);
Preorder(T->rchild);
}
}
void Preorder_1(Bitree T)//前序遍历(非递归)
{
Bitree p = T;
stack<Bitree> S;
while (p || !S.empty()) {
if (p) {
cout << p->data << " ";
S.push(p);
p = p->lchild;
}
else {
p=S.top();
p = p->rchild;
S.pop();
}
}
}
中序遍历(递归、非递归)
void Inorder(Bitree T)//中序遍历
{
if (T != NULL)
{
Inorder(T->lchild);
cout << T->data << " ";
Inorder(T->rchild);
}
}
void Inorder_1(Bitree T)//中序遍历(非递归)
{
stack<Bitree> S;
Bitree p = T;
while (p || !S.empty())
{
if (p) {
S.push(p);
p = p->lchild;
}
else {
cout << S.top()->data << " ";
p = S.top();
p = p->rchild;
S.pop();
}
}
}
后序遍历(递归、非递归)
void Postorder(Bitree T)//后序遍历
{
if (T != NULL)
{
Postorder(T->lchild);
Postorder(T->rchild);
cout << T->data << " ";
}
}
void Postorder_1(Bitree T)//后序遍历(非递归)
{
stack<Bitree> S;
Bitree p = T,m=NULL; //m用来标记上次访问过的右子树
while (p || !S.empty())
{
if (p) {
S.push(p);
p = p->lchild;
}
else {
p = S.top();
if (p->rchild && p->rchild != m) //右子树存在,且未被访问
p = p->rchild;
else {
cout << S.top()->data << " ";
S.pop();
m = p;
p = NULL;
}
}
}
}
层次遍历
void Levelorder(Bitree T) //层次遍历
{
queue<Bitree> Q;
Bitree p;
Q.push(T);
while (!Q.empty())
{
p = Q.front();
cout << p->data<< " ";
Q.pop();
if (p->lchild != NULL)
Q.push(p->lchild);
if(p->rchild != NULL)
Q.push(p->rchild);
}
}
二叉树的深度、结点
int Depthtree(Bitree T, int &depth) //树的深度
{
if (T == NULL)
depth = 0;
else
{
int l = Depthtree(T->lchild,depth);
int r = Depthtree(T->rchild,depth);
l > r ? depth = l+1 : depth = r+1;
}
return depth;
}
int numtree(Bitree T) //树的结点
{
int num = 0;
if (T == NULL)
num == 0;
else
{
if (T->lchild == NULL && T->rchild == NULL)
num = 1;
else
num = numtree(T->lchild) + numtree(T->rchild)+1;
}
return num;
}
完整代码如下
#include<bits/stdc++.h>
using namespace std;
typedef struct Bitnode {
char data;
struct Bitnode* lchild, * rchild;
}Bitnode,*Bitree;
void Inittree(Bitree &T) //二叉树的初始化
{
T->data = '\0';
T->lchild = NULL;
T->rchild = NULL;
}
Bitree Createtree(Bitree T) //根据先序遍历的序列构造二叉树
{
char s;
cin >> s;
int i = 0;
if (s == '#')
T = NULL;
else {
T = (Bitnode*)malloc(sizeof(Bitnode));
T->data = s;
T->lchild = Createtree(T->lchild);
T->rchild = Createtree(T->rchild);
}
return T;
}
void Preorder(Bitree T)//前序遍历
{
if (T != NULL)
{
cout<<T->data<<" ";
Preorder(T->lchild);
Preorder(T->rchild);
}
}
void Preorder_1(Bitree T)//前序遍历(非递归)
{
Bitree p = T;
stack<Bitree> S;
while (p || !S.empty()) {
if (p) {
cout << p->data << " ";
S.push(p);
p = p->lchild;
}
else {
p=S.top();
p = p->rchild;
S.pop();
}
}
}
void Inorder(Bitree T)//中序遍历
{
if (T != NULL)
{
Inorder(T->lchild);
cout << T->data << " ";
Inorder(T->rchild);
}
}
void Inorder_1(Bitree T)//中序遍历(非递归)
{
stack<Bitree> S;
Bitree p = T;
while (p || !S.empty())
{
if (p) {
S.push(p);
p = p->lchild;
}
else {
cout << S.top()->data << " ";
p = S.top();
p = p->rchild;
S.pop();
}
}
}
void Postorder(Bitree T)//后序遍历
{
if (T != NULL)
{
Postorder(T->lchild);
Postorder(T->rchild);
cout << T->data << " ";
}
}
void Postorder_1(Bitree T)//后序遍历(非递归)
{
stack<Bitree> S;
Bitree p = T,m=NULL; //m用来标记上次访问过的右子树
while (p || !S.empty())
{
if (p) {
S.push(p);
p = p->lchild;
}
else {
p = S.top();
if (p->rchild && p->rchild != m) //右子树存在,且未被访问
p = p->rchild;
else {
cout << S.top()->data << " ";
S.pop();
m = p;
p = NULL;
}
}
}
}
void Levelorder(Bitree T) //层次遍历
{
queue<Bitree> Q;
Bitree p;
Q.push(T);
while (!Q.empty())
{
p = Q.front();
cout << p->data<< " ";
Q.pop();
if (p->lchild != NULL)
Q.push(p->lchild);
if(p->rchild != NULL)
Q.push(p->rchild);
}
}
int Depthtree(Bitree T, int &depth) //树的深度
{
if (T == NULL)
depth = 0;
else
{
int l = Depthtree(T->lchild,depth);
int r = Depthtree(T->rchild,depth);
l > r ? depth = l+1 : depth = r+1;
}
return depth;
}
int numtree(Bitree T) //树的结点
{
int num = 0;
if (T == NULL)
num == 0;
else
{
if (T->lchild == NULL && T->rchild == NULL)
num = 1;
else
num = numtree(T->lchild) + numtree(T->rchild)+1;
}
return num;
}
int main()
{
//测试:ABDF###E##C##
Bitnode* T;
T = (Bitnode*)malloc(sizeof(Bitnode));
Inittree(T);
cout << "输入二叉树的先序遍历,空子树用#代替:" << endl;
T=Createtree(T);
cout << "前序遍历结果为:";
Preorder(T);
cout << endl;
cout << "前序遍历(非递归)结果为:";
Preorder_1(T);
cout << endl<<endl;
cout << "中序遍历结果为:";
Inorder(T);
cout << endl;
cout << "中序遍历(非递归)结果为:";
Inorder_1(T);
cout << endl<<endl;
cout << "后序遍历结果为:";
Postorder(T);
cout << endl;
cout << "后序遍历(非递归)结果为:";
Postorder_1(T);
cout << endl<<endl;
cout << "层次遍历结果为:";
Levelorder(T);
cout << endl<<endl;
cout << "树的深度为:";
int depth = 0;
Depthtree(T,depth);
cout << depth << endl;
cout << "树的结点为:";
cout << numtree(T) << endl;
return 0;
}