二叉树非递归遍历完整版

这篇博客介绍了二叉树的三种遍历方式:先序遍历、中序遍历和后续遍历。通过C语言实现,先序遍历通过递归创建树并打印节点,中序遍历将访问节点放在入栈时,后续遍历利用静态栈进行节点操作。代码详细展示了遍历过程。
摘要由CSDN通过智能技术生成

先序遍历

#include<stdio.h>
#include<malloc.h>
#include<stdbool.h>
 
 //树
typedef int ElemType;
typedef struct Node
{
    struct Node *lchild;
    ElemType data;
    struct Node *rchild; 
    /* data */
}Node,Tree;


Tree * create_tree(Tree *t){
//先序创建
    ElemType ch;
    scanf("%d",&ch);
    if(ch == 0)
        //t = (Tree *)malloc(sizeof(Tree))
        t = NULL;
    else{
        t = (Tree *)malloc(sizeof(Tree));
        t->data = ch;
        t->lchild = create_tree(t->lchild);
        t->rchild = create_tree(t->rchild);
    }
    return t;
}

//栈
#define INIT_SIZE 10
#define INCREASE 10


typedef struct 
{
    Node *top;
    Node *base;
    int length;
    int size;
    /* data */
}Stack;

void init_stack(Stack *p){
    p->base = p->top = (Node *)malloc(INIT_SIZE*sizeof(Node));
    p->length = 0;
    p->size = INIT_SIZE;
}

void push(Stack *p, Node *e){
    if(p->length >= p->size){
        p->base = (Node *)realloc(p->base,(p->size+INCREASE)*sizeof(Node));
        p->top = p-> base + p->length;
        p->size += INCREASE;
    }
    *p->top++ = *e;
    p->length++;
}


Node * pop(Stack *p, Node *e){
    if(p->top == p->base){
        printf("THE STACK IS NULL");
        return NULL;
    }
    e = --p->top;
    p->length--;
    return e;
}

bool Empty(Stack *stack){
    if(stack->length == 0)
        return true;
    else return false;
}


void xianxu(Tree *t){

    if(t == NULL){
        return ;
    }
    Node *p = t;
    Stack stack;
    init_stack(&stack);
    while(p || !Empty(&stack)){
        while(p){
            push(&stack,p);
            p = p->lchild;
        }
        if(!Empty(&stack)){
            p = pop(&stack,p);
            printf("%2d",p->data);
            p = p->rchild;
        }
    }
}

int main(){
    Tree *t;
    t = create_tree(t);
    xianxu(t);

}

中序遍历

中序遍历与先序极其相似,只需要将访问放在入栈时即可

后续遍历

eg:这里将栈的该为静态存储,(因为我被指向指针的指针搞晕了)

#include<stdio.h>
#include<malloc.h>
#include<stdbool.h>
 
 //树
typedef int ElemType;
typedef struct Node
{
    struct Node *lchild;
    ElemType data;
    struct Node *rchild; 
    /* data */
}Node,Tree;


Tree * create_tree(Tree *t){
//先序创建
    ElemType ch;
    scanf("%d",&ch);
    if(ch == 0)
        //t = (Tree *)malloc(sizeof(Tree))
        t = NULL;
    else{
        t = (Tree *)malloc(sizeof(Tree));
        t->data = ch;
        t->lchild = create_tree(t->lchild);
        t->rchild = create_tree(t->rchild);
    }
    return t;
}

//栈
#define INIT_SIZE 10
#define INCREASE 10


typedef struct 
{
    Node *Elem[10];
    int top;
    int base;
    int length;
    int size;
    /* data */
}Stack;

void init_stack(Stack *p){
    p->base = p->top = 0;
    p->length = 0;
    p->size = INIT_SIZE;
}

void push(Stack *p, Node *e){
    if(p->length >= p->size){
        printf("The Stack is Filled");
    }
    p->Elem[p->top] = e;
    p->top++;
    p->length++;
}


void pop(Stack *p){
    if(p->top == p->base){
        printf("THE STACK IS NULL");
        return ;
    }
    Node *e = p->Elem[--p->top];
    p->length--;
    return ;
}

bool Empty(Stack *stack){
    if(stack->length == 0)
        return true;
    else return false;
}

Node * top(Stack *p){
    return p->Elem[p->top-1];
}

void houxu(Tree *t){

    if(t == NULL){
        return ;
    }
    Node *p = t;
    Node *pre;
    Stack stack;
    init_stack(&stack);
    while(p || !Empty(&stack)){
        while(p){
            push(&stack,p);
            p = p->lchild;
        }
        p = top(&stack);
        if( p->rchild == NULL || p->rchild == pre){
            printf("%2d",p->data);
            pop(&stack);
            pre = p;
            p = NULL;
        }else{
            p = p->rchild;
        }
    }
}

int main(){
    Tree *t;
    t = create_tree(t);
    xianxu(t);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值