先序后序中序非递归算法

// BinaryTreeBianli.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include <stdio.h>  
#include <malloc.h>  
#include <stdlib.h>  
#include <queue>  
#include <stack>  
#include <iostream>  
using namespace std;
typedef struct BiTNode{
char data;
BiTNode *lchild, *rchild;
}BiTNode, *BiTree;


void CreateBiTree(BiTree &T)//建树,按先序顺序输入节点  
{
char ch;
scanf_s("%c", &ch);
if (ch == ' ')
{
T = NULL;
return;
}
else
{
T = (BiTree)malloc(sizeof(BiTNode));
if (!T)
exit(1);
T->data = ch;
CreateBiTree(T->lchild);
CreateBiTree(T->rchild);
}
}
void InOrderTraverse(BiTree T)//非递归中序遍历  
{


stack<BiTree> Stack;
if (!T)
{
printf("空树!\n");
return;
}


while (T || !Stack.empty())
{
while (T)
{
Stack.push(T);
T = T->lchild;
}
T = Stack.top();
Stack.pop();
printf("%c", T->data);
T = T->rchild;
}
}






void PreOrderTraverse(BiTree T)//非递归先序遍历  
{


stack<BiTree> Stack;
if (!T)
{
printf("空树!\n");
return;
}
while (T || !Stack.empty())
{
while (T)
{
Stack.push(T);
printf("%c", T->data);
T = T->lchild;
}
T = Stack.top();
Stack.pop();
T = T->rchild;
}
}




void PostOrderTraverse(BiTree T)//非递归后序遍历,用一个标记标记右子树是否访问过  
{
int flag[20];
stack<BiTree> Stack;
if (!T)
{
printf("空树!\n");
return;
}
while (T)
{
Stack.push(T);
flag[Stack.size()] = 0;
T = T->lchild;
}
while (!Stack.empty())
{
T = Stack.top();
while (T->rchild && flag[Stack.size()] == 0)
{
flag[Stack.size()] = 1;
T = T->rchild;
while (T)
{
Stack.push(T);
flag[Stack.size()] = 0;
T = T->lchild;
}
}
T = Stack.top();
printf("%c", T->data);
Stack.pop();
}
}
void main()
{


BiTree T;
CreateBiTree(T);
PreOrderTraverse(T);
printf("\n");
InOrderTraverse(T);
printf("\n");
PostOrderTraverse(T);
printf("\n");
getchar();

}




后序遍历

void PostOrder(bitreptr t)

{

if(t==NULL)

return ;

else

{

p=t;

top=0;

}

do

{

while(p!=NULL)

{

top++;

s[top]=p;

p=p->Lchild;

}

while((top!=0)&&(p==NULL))

{

p=s[top];

top--;

if(p>0)

{

top++;

s[top]=-p;

p=p->Rchild;

}

else

{

p=-p;

printf("%d",p->data);

p=NULL;

}

}

}while((top!=0)||p!=NULL);

}

第一次进栈表示它的左子数正在被遍历,而第二次进栈则表示正在遍历他的右子树,在这两种遍历均完成后才处理这个节点。

第二次进栈用负指针的方法。第一个while语句跟随左分支链将每一个遇到的结点的地址进栈,当这样一个链结束时,则弹出相应的栈顶元素与零比较,如果是正的,变负后压栈,并取右分支。若弹出的是负值,表示已经完成了对这个节点右子数的遍历,于是输出

1.先序遍历非递归算法#define maxsize 100typedef struct{ Bitree Elem[maxsize]; int top;}SqStack;void PreOrderUnrec(Bitree t){ SqStack s; StackInit(s); p=t; while (p!=null || !StackEmpty(s)) { while (p!=null) //遍历左子树 { visite(p->data); push(s,p); p=p->lchild; }//endwhile if (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树遍历 { p=pop(s); p=p->rchild; }//endif }//endwhile }//PreOrderUnrec2.中序遍历非递归算法#define maxsize 100typedef struct{ Bitree Elem[maxsize]; int top;}SqStack;void InOrderUnrec(Bitree t){ SqStack s; StackInit(s); p=t; while (p!=null || !StackEmpty(s)) { while (p!=null) //遍历左子树 { push(s,p); p=p->lchild; }//endwhile if (!StackEmpty(s)) { p=pop(s); visite(p->data); //访问根结点 p=p->rchild; //通过下一次循环实现右子树遍历 }//endif }//endwhile}//InOrderUnrec3.后序遍历非递归算法#define maxsize 100typedef enum{L,R} tagtype;typedef struct { Bitree ptr; tagtype tag;}stacknode;typedef struct{ stacknode Elem[maxsize]; int top;}SqStack;void PostOrderUnrec(Bitree t){ SqStack s; stacknode x; StackInit(s); p=t; do { while (p!=null) //遍历左子树 { x.ptr = p; x.tag = L; //标记为左子树 push(s,x); p=p->lchild; } while (!StackEmpty(s) && s.Elem[s.top].tag==R) { x = pop(s); p = x.ptr; visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点 } if (!StackEmpty(s)) { s.Elem[s.top].tag =R; //遍历右子树 p=s.Elem[s.top].ptr->rchild; } }while (!StackEmpty(s));}//PostOrderUnrec
当然,二叉树的遍历有三种主要方式:先序遍历(根-左-右)、中序遍历(左-根-右)和后序遍历(左-右-根)。非递归的层次遍历(也叫广度优先遍历,从上到下、从左到右)通常使用队列来辅助实现。 这里分别给出这些遍历的非递归算法代码: 1. 层序遍历(广度优先遍历): ```c #include <stdio.h> #include <stdlib.h> #include <queue> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; void levelOrder(struct TreeNode* root) { if (root == NULL) return; // 使用队列存储每一层的节点 queue<struct TreeNode*> q; q.push(root); while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { struct TreeNode* node = q.front(); q.pop(); printf("%d ", node->val); // 打印当前节点值 if (node->left != NULL) q.push(node->left); if (node->right != NULL) q.push(node->right); } printf("\n"); // 换行表示新的一层 } } ``` 2. 先序遍历(递归和非递归两种方式,这里是非递归版本,使用栈): ```c void preorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s; s.push(root); while (!s.empty()) { struct TreeNode* node = s.top(); s.pop(); printf("%d ", node->val); // 打印当前节点值 if (node->right != NULL) s.push(node->right); if (node->left != NULL) s.push(node->left); } } ``` 3. 中序遍历(非递归,同样使用栈): ```c void inorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s; struct TreeNode* curr = root; while (curr != NULL || !s.empty()) { while (curr != NULL) { s.push(curr); curr = curr->left; } curr = s.top(); s.pop(); printf("%d ", curr->val); // 打印当前节点值 curr = curr->right; } } ``` 4. 后序遍历(非递归,使用两个栈): ```c void postorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s1, s2; s1.push(root); while (!s1.empty()) { struct TreeNode* node = s1.top(); s1.pop(); s2.push(node); if (node->left != NULL) s1.push(node->left); if (node->right != NULL) s1.push(node->right); } while (!s2.empty()) { struct TreeNode* node = s2.top(); s2.pop(); printf("%d ", node->val); // 打印当前节点值 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值