非遞歸實現樹的前序遍歷和中序遍歷

//這就要用棧來實現
這裏寫圖片描述

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define MAX_SIZE 100 //棧的最大容量
/*
訪問二叉樹三種方式:
1.前序遍歷:根 左  右
2.中序遍歷:左 根  右
3.後續遍歷:左 右  根
*/
//定義樹的節點
typedef struct tree_node
{
    char value;
    struct  tree_node* lchild;
    struct  tree_node* rchild;
}tree_node;

//定義樹結構
//定義棧節點
typedef struct node
{
    tree_node*  ptr_node;
    struct node * next;
}node;

//定義棧
typedef struct stack
{
    node * top;
    int size;//當前棧節點個數
    int capacity;//限制棧存儲最大節點數
}stack;

//初始化棧
void  init_stack(stack * ptr_stack)
{
    ptr_stack->top=NULL;
    ptr_stack->size=0;
    ptr_stack->capacity=MAX_SIZE;
}

//進棧
void  push_stack(stack * ptr_stack,tree_node* ptr_node)
{
    if(!is_stack_full(ptr_stack))
    {
        node*  ptr_new=(node*)malloc(1*sizeof(node));
        assert(ptr_new != NULL);
        ptr_new->ptr_node=ptr_node;
        ptr_new->next=NULL;

        //入棧
        ptr_new->next=ptr_stack->top;
        ptr_stack->top=ptr_new;
        ptr_stack->size++;
        ptr_new=NULL;
    }
    else
    {
        printf("stack is full\n");
    }    
}

//出棧
void  pop_stack(stack * ptr_stack)
{
    if(!is_stack_empty(ptr_stack))
    {
        node*  ptr_tmp=ptr_stack->top;
        ptr_stack->top=ptr_stack->top->next;
        ptr_stack->size--;        

        free(ptr_tmp);
        ptr_tmp=NULL;
    }
    else
    {
        printf("stack is empty\n");
    }
}
//判斷棧是否滿
int  is_stack_full(stack * ptr_stack)
{
    if(ptr_stack->size>ptr_stack->capacity)
    {
        return 1;
    }
    return 0;
}
//判斷棧是否爲空
int  is_stack_empty(stack *  ptr_stack)
{
    if(ptr_stack->size==0)
    {
        return 1;
    }
    return 0;
}
//返回棧頂節點
tree_node*  get_stack_top(stack* ptr_stack)
{
    if(!is_stack_empty(ptr_stack))
    {
        return  ptr_stack->top->ptr_node;
    }
    return NULL;
}
//返回棧的節點個數
int get_stack_cnt(stack* ptr_stack)
{
    return  ptr_stack->size;
}

//構建一個樹
tree_node*  init_binary_tree()
{
    char value;
    scanf("%c",&value);
    getchar();
    tree_node* root;
    if(value=='q')//'q輸入結束字符'
    {
        root=NULL;
    }                                                                                                           
    else
    {
        root=(tree_node*)malloc(1*sizeof(tree_node));
        root->value=value;
        root->lchild=init_binary_tree();
        root->rchild=init_binary_tree();
    }
    return root;    
}

//非遞歸前序遍歷
void   print_pre_tree(tree_node*  root)
{
    stack st;
    init_stack(&st);
    while(root!=NULL || !is_stack_empty(&st))
    {
        while(root!=NULL)
        {
            printf("%c\t",root->value);
            push_stack(&st,root);
            root=root->lchild;
        }
        if(!is_stack_empty(&st))
        {
            root=get_stack_top(&st);
            pop_stack(&st);
            root=root->rchild;
        }
    }
}

//非遞歸中序
void   print_mid_tree(tree_node * root)
{
    stack st;
    init_stack(&st);
    while(root!=NULL || !is_stack_empty(&st))
    {
        while(root!=NULL)
        {           
            push_stack(&st,root);
            root=root->lchild;
        }
        if(!is_stack_empty(&st))
        {
            root=get_stack_top(&st);
            printf("%c\t",root->value);
            pop_stack(&st);
            root=root->rchild;
        }
    }
}


int main(int argc, char const *argv[])
{
    tree_node* root=init_binary_tree();
    printf("非遞歸前序遍歷: ");
    print_pre_tree(root);
    printf("\n");
    printf("非遞歸中序遍歷: ");
    print_mid_tree(root);
    printf("\n");
    return 0;
}

運行結果:
這裏寫圖片描述

發佈了56 篇原創文章 · 獲贊 18 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章