二叉树的建立,先序、中序、后续递归和非递归遍历
----------
shengming.h
----------
#pragma once
#include "stdafx.h"
#define N 20
typedef char Elemtype;
typedef int Status;
extern int coun;
typedef struct Bnode
{
Elemtype data;
struct Bnode *lchild;
struct Bnode *rchild;
}Bnode,*BTree;
typedef struct BNODE
{
BTree next;
bool isFirst;
}BNODE;
BTree BTree_Create();
void PreOrder(BTree BT);
void PreOrder1(BTree BT);
void MidOrder(BTree BT);
void MidOrder1(BTree BT);
void PostOrder(BTree BT);
void PostOrder1(BTree BT);
void PostOrder2(BTree BT);
BTree Build_Tree(Elemtype* prestart,Elemtype* preend,Elemtype* midstart,Elemtype* midend); //前序 中序推后序
BTree Build_Code(Elemtype* poststart,Elemtype* postend, Elemtype* midstart, Elemtype* midend); //后序 中序推前序
dingyi.cpp
#include "stdafx.h"
#include "shengming.h"
#include <iostream>
#include <stack>
using namespace std;
int coun;
BTree BTree_Create()
{
Bnode* BT;
BT = (BTree)malloc(sizeof(Bnode));
if (!BT)
{
cout << "申请内存失败!" << endl;
exit(OVERFLOW);
}
if (coun == 0)
{
cout << "输入根节点:" << endl;
}
coun++;
char ch;
cin >> ch;
BT->data = ch;
if (ch == '#')
{
BT = NULL;
}
else
{
cout << "输入节点"<<BT->data<<"的左孩子#表示空:" << endl;
BT->lchild = BTree_Create();
cout << "输入节点" << BT->data << "的右孩子#表示空!" << endl;
BT->rchild = BTree_Create();
}
return BT;
}
void PreOrder(BTree BT)
{
if (!BT)
return;
cout << BT->data << " ";
PreOrder(BT->lchild);
PreOrder(BT->rchild);
}
void PreOrder1(BTree BT) //非递归实现
{
stack<BTree> s;
BTree p = BT;
while (p!=NULL||!s.empty())
{
while (p) //把所有的左子树全部压入栈中
{
cout << p->data << " ";
s.push(p);
p = p->lchild;
}
if (!s.empty())
{
p = s.top();
s.pop();
p = p->rchild;
}
}
}
void MidOrder1(BTree BT)
{
stack<BTree> s;
BTree p = BT;
while (p != NULL || !s.empty())
{
while (p) //把所有的左子树全部压入栈中
{
s.push(p);
p = p->lchild;
}
if (!s.empty())
{
p = s.top();
cout << p->data << " ";
s.pop();
p = p->rchild;
}
}
}
void MidOrder(BTree BT)
{
if (!BT)
return;
MidOrder(BT->lchild);
cout << BT->data << " ";
MidOrder(BT->rchild);
}
void PostOrder(BTree BT)
{
if (!BT)
return;
PostOrder(BT->lchild);
PostOrder(BT->rchild);
cout << BT->data << " ";
}
void PostOrder1(BTree BT) //非递归后续遍历1
{
stack<BNODE *> s;
BTree p = BT;
BNODE *temp;
while (p != NULL || !s.empty())
{
while (p)
{
BNODE *node;
node = (BNODE *)malloc(sizeof(BNODE));
node->next = p;
node->isFirst = true;
s.push(node);
p = p->lchild;
}
if (!s.empty())
{
temp = s.top();
s.pop();
if (temp->isFirst == true)
{
temp->isFirst = false;
s.push(temp);
p = temp->next->rchild;
}
else
{
cout << temp->next->data << " ";
p = NULL;
}
}
}
}
void PostOrder2(BTree BT) //非递归后续遍历2
{
stack<BTree> s;
BTree cur; //当前节点
BTree pre = NULL; //前一个节点
s.push(BT);
while (!s.empty())
{
cur = s.top();
if (cur->lchild == NULL && cur->rchild == NULL || pre!=NULL&&(pre==cur->lchild||pre==cur->rchild))
{
cout << cur->data << " ";
pre = cur;
s.pop();
}
else
{
if (cur->rchild != NULL)
s.push(cur->rchild);
if (cur->lchild != NULL)
s.push(cur->lchild);
}
}
}
BTree Build_Tree(Elemtype* prestart, Elemtype* preend, Elemtype* midstart, Elemtype* midend) //前序 中序推后序
{
BTree root;
root = (BTree)malloc(sizeof(Bnode));
if (!root)
{
cout << "申请内存失败!" << endl;
exit(OVERFLOW);
}
root->data = *prestart; //把前序的第一个节点保存为根节点
root->lchild = NULL;
root->rchild = NULL;
if (midstart == midend) //没有if语句也可以得出结果
{
return root;
}
Elemtype* mid;
mid = midstart;
while (*prestart != *mid)
{
mid++;
}
//左子树长度
int length;
length = mid - midstart;
//重建左子树
if (length > 0)
root->lchild = Build_Tree(prestart+1,prestart+length-1,midstart,midstart+length-1);
//重建右子树
if (midstart + length < midend)
root->rchild = Build_Tree(prestart+length+1,preend,midstart+length+1,midend);
return root;
}
BTree Build_Code( Elemtype* poststart,Elemtype* postend, Elemtype* midstart, Elemtype* midend)
{
BTree root;
root = (BTree)malloc(sizeof(Bnode));
if (!root)
{
cout << "申请内存失败!" << endl;
exit(OVERFLOW);
}
cout << "*postend=" << *postend << endl;
root->data = *postend; //把后序的最后一个节点保存为根节点
root->lchild = NULL;
root->rchild = NULL;
if (midstart == midend && *midstart == *midend) //没有if语句也可以得出结果
{
return root;
}
Elemtype* mid;
mid = midstart;
while (*postend != *mid)
{
mid++;
}
//左子树长度
int length;
length = mid - midstart;
//重建左子树
if (length > 0)
{
root->lchild = Build_Code(poststart, poststart + length - 1, midstart, midstart + length - 1);
}
//重建右子树
if (midstart + length < midend)
{
root->rchild = Build_Code(poststart + length, postend - 1, midstart + length + 1, midend);
}
return root;
}
BinaryTree.cpp
// BinaryTree.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "shengming.h"
#include <iostream>
using namespace std;
int main()
{
BTree BT;
BT = BTree_Create();
cout << "先序遍历树:" << endl;
PreOrder(BT);
cout <<endl<< "非递归先序遍历树1:" << endl;
PreOrder1(BT);
cout << endl << "中序遍历树:" << endl;
MidOrder(BT);
cout << endl << "非递归中序遍历树1:" << endl;
MidOrder1(BT);
cout << endl << "后序遍历树:" << endl;
PostOrder(BT);
cout << endl << "非递归后序遍历树1:" << endl;
PostOrder1(BT);
cout << endl << "非递归后序遍历树2:" << endl;
PostOrder1(BT);
cout << "\n";
Elemtype *pre=NULL,*mid=NULL,*post=NULL;
Elemtype pre_buffer[N],mid_buffer[N],post_buffer[N];
cout << "输入树的先序#表示结束:" << endl;
scanf("%s", pre_buffer);
pre = pre_buffer;
cout << "输入树的中序#表示结束:" << endl;
scanf("%s", mid_buffer);
mid = mid_buffer;
BT = Build_Tree(pre, pre + strlen(pre) - 1,mid,mid+strlen(mid)-1);
cout <<"后序遍历树:" << endl;
PostOrder(BT);
cout <<endl<< "输入树的后序#表示结束:" << endl;
scanf("%s", post_buffer);
post = post_buffer;
BT = Build_Code(post,post+strlen(post)-1,mid,mid+strlen(mid)-1);
cout << "先序遍历树:" << endl;
PreOrder(BT);
PostOrder(BT);
MidOrder(BT);
cout << "\n";
return 0;
}