欢迎来到入门教程网!

C语言

当前位置:主页 > 软件编程 > C语言 >

C语言非递归后序遍历二叉树

来源:本站原创|时间:2020-01-10|栏目:C语言|点击:

本文实例为大家分享了C语言非递归后序遍历二叉树的具体代码,供大家参考,具体内容如下

法一:实现思路:一个栈 先按 根->右子树->左子树的顺序访问二叉树。访问时不输出。另一个栈存入前一个栈只进栈的结点。
最后输出后一个栈的结点数据。

#include<stdio.h>
#include<stdlib.h>
typedef struct TreeNode{
  char element;
  struct TreeNode *left,*right;
}Tree,*BTree;
typedef struct StackNode{
  BTree data;
  struct StackNode *next;
}Stack,*PStack;
typedef struct{
  PStack top; 
}LinkStack,*PLinkStack;
//初始化空栈
PLinkStack Init_Stack(void){
  PLinkStack S;
  S=(PLinkStack)malloc(sizeof(LinkStack));
  if(S){
    S->top=NULL;
  }
  return S;
}
//压栈
void Push_Stack(PLinkStack S,BTree T){
  PStack p;
  p=(PStack)malloc(sizeof(Stack));
  p->data=T;
  p->next=S->top;
  S->top=p;
}
//判空
int empty_Stack(PLinkStack S){
  if(S->top){
    return 0;
  }else{
    return 1;
  }
}
//出栈
PStack Pop_Stack(PLinkStack S){
  PStack q; 
  if(empty_Stack(S)){
    return S->top;
  }else{
    q=S->top;
    S->top=S->top->next;
  }
  return q;  
}
//销毁栈
void DestroyStack(PLinkStack S){
  PStack del; 
  while(S->top!=NULL){
    del=S->top;
    S->top=S->top->next;
    free(del);
  }
  free(S);
} 
BTree BuildTree(void){
  char ch;
  BTree node;
  ch=getchar();
  if(ch=='#'){
    node=NULL;
  }else{
    node=(BTree)malloc(sizeof(Tree));
    node->element=ch;
    node->left=BuildTree();
    node->right=BuildTree();
  }
  return node;
}
void NotRecursionPostOrder(BTree T){
  PLinkStack S,CS;
  S=Init_Stack();
  CS=Init_Stack();
  while(T || !empty_Stack(S)){
    if(T){
      Push_Stack(S,T);
      Push_Stack(CS,T);
      T=T->right;
    }else{
      T=Pop_Stack(S)->data;
      T=T->left;
    }
  }
  while(CS->top!=NULL){
    printf("%c",CS->top->data->element);
    CS->top=CS->top->next;
  }
  DestroyStack(CS);
}
int main(void){
  BTree T;
  T=BuildTree();
  NotRecursionPostOrder(T);
  return 0;
} 

法二:实现思路。按先序遍历访问每一个结点。存入栈中,当为空时,就出立即栈(第一次出栈)。出栈后应该立即进栈,去访问进栈结点的右结点,这样可以保证先输出左、右结点,再输出根结点。二次进栈利用flag标记。

#include<stdio.h>
#include<stdlib.h>
typedef struct TreeNode {
  char element;
  int flag;
  struct TreeNode *left, *right;
}Tree, *BTree;
typedef struct StackNode {
  BTree data;
  struct StackNode *next;
}Stack, *PStack;
typedef struct {
  PStack top;
}LinkStack, *PLinkStack;
//初始化空栈
PLinkStack Init_Stack(void) {
  PLinkStack S = (PLinkStack)malloc(sizeof(LinkStack));
  if (S) {
    S->top = NULL;
  }
  return S;
}
//压栈
void Push_Stack(PLinkStack S, BTree T) {
  PStack p;
  p = (PStack)malloc(sizeof(Stack));
  p->data = T;
  p->next = S->top;
  S->top = p;
}
//判空
int empty_Stack(PLinkStack S) {
  if (S->top) {
    return 0;
  }
  else {
    return 1;
  }
}
//出栈
PStack Pop_Stack(PLinkStack S) {
  PStack q = S->top;
  S->top = S->top->next;
  return q;
}
BTree BuildTree(void) {
  BTree t;
  char ch;
  ch = getchar();
  if (ch == '#') {
    t = NULL;
  }
  else {
    t = (BTree)malloc(sizeof(Tree));
    t->element = ch;
    t->left = BuildTree();
    t->right = BuildTree();
  }
  return t;
}
void DestroyStack(PLinkStack S){
  PStack p;
  while(S->top){
    p=S->top;
    free(p);
    S->top=S->top->next;
  }
} 
void NotRecursionPostOrder(BTree T) {
  PLinkStack S;
  S = Init_Stack();
  while (T || !empty_Stack(S)) {
    if (T) {
      T->flag = 0;
      Push_Stack(S, T);
      T = T->left;
    }
    else {
      T = Pop_Stack(S)->data;
      if (T->flag == 0) {
        T->flag = 1;
        Push_Stack(S, T);
        T = T->right;
      }
      else {
        if (T->flag == 1) {
          printf("%c", T->element);
          T = NULL;
        }
      }
    }
  }
  DestroyStack(S);//销毁栈 
}
int main(void) {
  BTree T;
  T = BuildTree();
  NotRecursionPostOrder(T);
  return 0;
}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

上一篇:C++ 约瑟夫环的实例代码

栏    目:C语言

下一篇:必须知道的C语言八大排序算法(收藏)

本文标题:C语言非递归后序遍历二叉树

本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/1097.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有