二叉树的建立、遍历、图形化表示
分别实现递归和非递归的 前序遍历、中序遍历、后序遍历
图形化表示
#include<iostream>
#include<stdio.h>
#include<string>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct BTree* BTreePointer;
static bool EXITCURSOR = true;
typedef struct SqStack {
BTreePointer* base;
BTreePointer* top;
int stacksize;
void Init(SqStack& S);
void Push(SqStack& S, BTreePointer e);
void Pop(SqStack& S, BTreePointer& e);
bool empty(SqStack stck);
};
typedef struct BTree {
char data;
bool isVisited ;
int depth;
BTreePointer leftChild;
BTreePointer rightChild;
bool CreateTree(BTreePointer& T);
void Traverse_Pre(BTreePointer T);
void Traverse_Post(BTreePointer T);
void Traverse_Inorder(BTreePointer T);
void Notraverse_Pre(BTreePointer T, SqStack stack);
void Notraverse_Post(BTreePointer T, SqStack stack);
void Notraverse_Inorder(BTreePointer T, SqStack stack);
void getDepth(BTreePointer T,int depth);
void Traverse_RDL(BTreePointer T);
};
bool SqStack::empty(SqStack stck) {
if (stck.base == stck.top)return true;
else
{
return false;
}
}
void SqStack::Init(SqStack& S) {
S.base = (BTreePointer*)malloc(sizeof(BTreePointer) * STACK_INIT_SIZE);
if (!S.base) std::cout << "OVERFLOW";
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
}
void SqStack::Push(SqStack& S, BTreePointer e) {
if (S.top - S.base >= S.stacksize) {
S.base = (BTreePointer*)realloc(S.base, stacksize + STACKINCREMENT * sizeof(BTreePointer));
}
if (!S.base) std::cout << "OVERFLOW";
*S.top++ = e;
}
void SqStack::Pop(SqStack& S, BTreePointer& e) {
if (S.top == S.base) std::cout << "ERROR";
e = *--S.top;
}
void BTree::getDepth(BTreePointer T,int depth) {
if (!T)return;
T->depth = depth;
getDepth(T->leftChild, depth + 1);
getDepth(T->rightChild, depth + 1);
}
bool BTree::CreateTree(BTreePointer& T) {
if (!EXITCURSOR)return false;
char nodeMsg;
scanf_s("%c", &nodeMsg);
if (nodeMsg == '\n') { std::cout << "输入个数据不足以构成一个二叉树,请重新输入" << std::endl;
EXITCURSOR = false;
}
else if (nodeMsg == '#')T = NULL;
else
{
T = (BTreePointer)malloc(sizeof(BTree));
T->data = nodeMsg;
T->isVisited = false;
CreateTree(T->leftChild);
CreateTree(T->rightChild);
}
return EXITCURSOR;
}
void BTree::Traverse_Pre(BTreePointer T) {
if (T) {
std::cout << T->data<<" ";
Traverse_Pre(T->leftChild);
Traverse_Pre(T->rightChild);
}
}
void BTree::Traverse_Post(BTreePointer T) {
if (T) {
Traverse_Post(T->leftChild);
Traverse_Post(T->rightChild);
std::cout << T->data << " ";
}
}
void BTree::Traverse_Inorder(BTreePointer T) {
if (T) {
Traverse_Inorder(T->leftChild);
std::cout << T->data << " ";
Traverse_Inorder(T->rightChild);
}
}
void BTree::Traverse_RDL(BTreePointer T) {
if (T) {
Traverse_RDL(T->rightChild);
for (int i = 1; i < T->depth; i++) {
std::cout << " ";
}
std::cout << T->data << std::endl;
std::cout << std::endl;
Traverse_RDL(T->leftChild);
}
}
void BTree::Notraverse_Pre(BTreePointer T,SqStack stack) {
while (T||!stack.empty(stack))
{
if (T) {
stack.Push(stack,T);
std::cout << T->data << " ";
T = T->leftChild;
}
else {
stack.Pop(stack, T);
T = T->rightChild;
}
}
}
void BTree::Notraverse_Post(BTreePointer T, SqStack stack) {
while (T || !stack.empty(stack))
{
if (T) {
stack.Push(stack, T);
T = T->leftChild;
}
else {
T = *--stack.top;
*stack.top++;
if (T->rightChild!=NULL && !T->isVisited) {
T->isVisited = true;
T = T->rightChild; }
else
{
std::cout << T->data << " ";
stack.Pop(stack, T);
T = NULL;
}
}
}
}
void BTree::Notraverse_Inorder(BTreePointer T, SqStack stack) {
while (T || !stack.empty(stack))
{
if (T) {
stack.Push(stack, T);
T = T->leftChild;
}
else {
stack.Pop(stack, T);
std::cout << T->data << " ";
T = T->rightChild;
}
}
}
int main() {
std::cout << "请输入树的节点信息,例如‘abdg###e##c#f##’,其中#代表空" << std::endl;
BTreePointer tree;
while (!tree->CreateTree(tree)) EXITCURSOR = true;
std::cout << "递归先序遍历 :";
tree->Traverse_Pre(tree);
std::cout << std:: endl;
std::cout << "递归中序遍历 :";
tree->Traverse_Inorder(tree);
std::cout << std::endl;
std::cout << "递归后序遍历 :";
tree->Traverse_Post(tree);
std::cout << std::endl;
std::cout << std::endl;
SqStack stack;
stack.Init(stack);
std::cout << "非递归先序遍历 :";
tree->Notraverse_Pre(tree,stack);
std::cout << std::endl;
std::cout << "非递归中序遍历 :";
tree->Notraverse_Inorder(tree, stack);
std::cout << std::endl;
std::cout << "非递归后序遍历 :";
tree->Notraverse_Post(tree,stack);
std::cout << std::endl;
std::cout << std::endl;
tree->getDepth(tree,1);
tree->Traverse_RDL(tree);
std::cout << std::endl;
system("pause");
}