5.1~5.5二叉树的基本操作
代码实现
#pragma once
#include <iostream>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode* lchild, * rchild;
}BiTNode,*BiTree;
#define MAXSIZE 100
typedef struct
{
BiTree* base;
BiTree* top;
int stacksize;
}SqStack;
void InitBiTree(BiTree& T);
void createBiTree(BiTree& T);
void destroyBiTree(BiTree& T);
void ClearBiTree(BiTree& T);
bool BiTreeEmpty(BiTree T);
void PreOrder(BiTree T);
void InOrder(BiTree T);
void PostOrder(BiTree T);
void InOrderTraverse(BiTree T);
void InitStack(SqStack& S);
bool StackEmpty(SqStack S);
void Push(SqStack& S, BiTree e);
void Pop(SqStack& S, BiTree& e);
void copy(BiTree T, BiTree& NewT);
int Depth(BiTree T);
int NodeCount(BiTree T);
int main()
{
BiTree T;
InitBiTree(T);
cout << "按先序次序输入二叉树中结点的值" << endl;
createBiTree(T);
cout << "先序遍历:" << endl;
PreOrder(T);
cout << endl;
cout << "中序遍历:" << endl;
InOrder(T);
cout << endl;
cout << "中序遍历:" << endl;
PostOrder(T);
cout << endl;
cout << "中序遍历的非递归算法:" << endl;
InOrderTraverse(T);
cout << endl;
cout << "二叉树的复制" << endl;
BiTree NewT;
copy(T, NewT);
cout << "先序遍历:" << endl;
PreOrder(NewT);
cout << endl;
int res = Depth(T);
cout << "二叉树T的深度:" << res << endl;
int num = NodeCount(T);
cout<<"二叉树T节点的个数:"<<num<<endl;
system("pause");
return 0;
}
void InitBiTree(BiTree& T)
{
T = NULL;
}
void createBiTree(BiTree& T)
{
char data;
cin >> data;
if (data != '#')
{
T = new BiTNode;
T->data = data;
createBiTree(T->lchild);
createBiTree(T->rchild);
}
else
{
T = NULL;
}
}
void destroyBiTree(BiTree& T)
{
if (T != NULL)
{
destroyBiTree(T->lchild);
destroyBiTree(T->rchild);
delete T;
}
}
void ClearBiTree(BiTree& T)
{
if (T != NULL)
{
ClearBiTree(T->lchild);
if ((T)->rchild)
ClearBiTree(T->rchild);
delete T;
T = NULL;
}
}
bool BiTreeEmpty(BiTree T)
{
if (T)
{
return false;
}
else
{
return true;
}
}
void PreOrder(BiTree T)
{
if (T != NULL)
{
cout << T->data << " ";
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
void InOrder(BiTree T)
{
if (T != NULL)
{
InOrder(T->lchild);
cout << T->data << " ";
InOrder(T->rchild);
}
}
void PostOrder(BiTree T)
{
if (T != NULL)
{
PostOrder(T->lchild);
PostOrder(T->rchild);
cout << T->data << " ";
}
}
void InOrderTraverse(BiTree T)
{
SqStack S;
InitStack(S);
BiTNode* p = T;
BiTNode* q;
q = new BiTNode;
while (p || !StackEmpty(S))
{
if (p)
{
Push(S, p);
p = p->lchild;
}
else
{
Pop(S, q);
cout << q->data<<" ";
p = q->rchild;
}
}
}
void InitStack(SqStack& S)
{
S.base = new BiTree[MAXSIZE];
if (!S.base)
{
exit(1);
}
S.top = S.base;
S.stacksize = MAXSIZE;
}
bool StackEmpty(SqStack S)
{
if (S.top == S.base)
{
return true;
}
else
{
return false;
}
}
void Push(SqStack& S, BiTree e)
{
if (S.top - S.base == S.stacksize)
{
cout << "栈满" << endl;
exit(0);
}
*(S.top)= e;
*S.top++;
}
void Pop(SqStack& S, BiTree &e)
{
if (StackEmpty(S))
{
cout << "栈空" << endl;
exit(0);
}
S.top--;
e =*(S.top);
}
void copy(BiTree T, BiTree& NewT)
{
if (T == NULL)
{
NewT = NULL;
return;
}
else
{
NewT = new BiTNode;
NewT->data = T->data;
copy(T->lchild, NewT->lchild);
copy(T->rchild, NewT->rchild);
}
}
int Depth(BiTree T)
{
if (T == NULL)
{
return 0;
}
else
{
int m = Depth(T->lchild);
int n = Depth(T->rchild);
return (m > n) ? (m + 1) : (n + 1);
}
}
int NodeCount(BiTree T)
{
if (T == NULL)
{
return 0;
}
else
{
int m = NodeCount(T->lchild);
int n = NodeCount(T->rchild);
return (m + n + 1);
}
}
运行结果