二叉树各种遍历全代码实现
看到二叉树的遍历,虽然上课讲这段的时候,就练习了很多,但当时仅仅是做了伪代码实现。没有做实际的可执行代码实现,这次好好写了一下,遍历的原理不再阐述,主要贴一下代码实现
PS:其实就是上课的时候,对于非递归部分,老师仅仅讲了如何用栈和队列的思想,而没有讲一讲实现栈的部分,而教科书上的代码我觉得太冗长了,于是乎直接用C++的STL容器的stack与queue来玩,要不然真的要写烂了……
使用样例
完全代码实现
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
#define MAX_SIZE 100
typedef char ElemType;
typedef struct TreeNode *BinTree;
struct TreeNode{
ElemType data;
BinTree left;
BinTree right;
};
void PreOrder(BinTree BT)
{
if(BT){
printf("%c ",BT->data);
PreOrder(BT->left);
PreOrder(BT->right);
}
}
void InOrder(BinTree BT)
{
if(BT){
InOrder(BT->left);
printf("%c ",BT->data);
InOrder(BT->right);
}
}
void PostOrder(BinTree BT)
{
if(BT){
PostOrder(BT->left);
PostOrder(BT->right);
printf("%c ",BT->data);
}
}
void PreOrder2(BinTree BT)
{
stack<BinTree> stack;
BinTree p = BT;
while(p || !stack.empty()){
if(p != NULL){
stack.push(p);
printf("%c ",p->data);
p = p->left;
}else{
p = stack.top();
stack.pop();
p = p->right;
}
}
}
void InOrder2(BinTree BT)
{
stack<BinTree> stack;
BinTree p = BT;
while(p!=NULL || !stack.empty()){
if(p!=NULL){
stack.push(p);
p = p->left;
}else{
p = stack.top();
printf("%c ",p->data);
stack.pop();
p = p->right;
}
}
}
//后序非递归
typedef struct BinTNodePost{
BinTree bitree;
char tag;
}BinTNodePost,*BinTreePost;
void PostOrder2(BinTree BT)
{
stack<BinTreePost> stack;
BinTree p = BT;
BinTreePost postT;
while(p != NULL || !stack.empty()){
//遍历左树
while(p != NULL){
postT = (BinTreePost)malloc(sizeof(BinTNodePost));
postT->bitree = p;
postT ->tag = 'L';
stack.push(postT);
p = p->left;
}
while(!stack.empty() && (stack.top())->tag == 'R'){
postT = stack.top();
stack.pop();
printf("%c ",postT->bitree->data);
}
if(!stack.empty()){
postT = stack.top();
postT->tag = 'R';
p = postT->bitree;
p = p->right;
}
}
}
void PostOrder3(BinTree BT)
{
stack<BinTree> stack;
BinTree p = BT;
BinTree r = NULL; //辅助判断右子树是否被判断
while(p != NULL || !stack.empty()){
if(p != NULL){ //走到最左侧,轮流入栈
stack.push(p);
p = p->left;
}else{
p = stack.top();
if(p->right != NULL && p->right != r){
p = p->right;
stack.push(p);
p = p->left;
}else{
p = stack.top();
stack.pop();
printf("%c ",p->data);
r = p;
p = NULL;
}
}
}
}
void LevelOrder(BinTree BT)
{
BinTree p = BT;
queue<BinTree> queue;
queue.push(p);
while(!queue.empty()){
p = queue.front();
printf("%c ",p->data);
queue.pop();
if(p->left != NULL){
queue.push(p->left);
}
if(p->right != NULL){
queue.push(p->right);
}
}
}
int main()
{
BinTree node1,node2,node3,node4,node5,node6;
node1 = (BinTree)malloc(sizeof(struct TreeNode));
node2 = (BinTree)malloc(sizeof(struct TreeNode));
node3 = (BinTree)malloc(sizeof(struct TreeNode));
node4 = (BinTree)malloc(sizeof(struct TreeNode));
node5 = (BinTree)malloc(sizeof(struct TreeNode));
node6 = (BinTree)malloc(sizeof(struct TreeNode));
node1->data = 'A';
node2->data = 'B';
node3->data = 'C';
node4->data = 'D';
node5->data = 'E';
node6->data = 'F';
node1->left = node2;
node1->right = node3;
node2->left = node4;
node2->right = node5;
node3->left = NULL;
node3->right = node6;
node4->left = NULL;
node4->right = NULL;
node5->left = NULL;
node5->right = NULL;
node6->left = NULL;
node6->right = NULL;
printf("PreOrder:");
PreOrder(node1);
printf("\nInOrder:");
InOrder(node1);
printf("\nPostOrder:");
PostOrder(node1);
printf("\nPreOrder2:");
PreOrder2(node1);
printf("\nInOrder2:");
InOrder2(node1);
printf("\nPostOrder2:");
PostOrder2(node1);
printf("\nPostOrder3:");
PostOrder3(node1);
printf("\nLevelOrder:");
LevelOrder(node1);
return 0;
}
运行结果
PreOrder:A B D E C F
InOrder:D B E A C F
PostOrder:D E B F C A
PreOrder2:A B D E C F
InOrder2:D B E A C F
PostOrder2:D E B F C A
PostOrder3:D E B F C A
LevelOrder:A B C D E F