二叉树遍历(C语言)

   某次面试,面试官出了一道用非递归实现二叉树的中序遍历的题目,要求当场写出函数,由于之前没有想过这个问题,所以慌乱之中写的乱七八糟,自然面试也失败了,回家后想想还是要把这个搞明白了,于是自己想了很久,也看了之前的数据结构的书,再花了几天时间终于把前序中序后序遍历都写了一遍,同时还把生成树和stack栈的实现函数都写出来了,现在分享出来,也留做自己以后复习用。

    首先是栈的实现stack.c:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include"bianli.h"
#include"stack.h"
//实现STACK
int push(stack_s** stack_header, treenode_s* tree)
{
    int ret = 0;
    stack_s* nstack = {0};
    
    //printf("%s\n", __func__);
    nstack = (stack_s*)malloc(sizeof(stack_s));
    nstack->node = tree;
    nstack->next = NULL;
    if(!(*stack_header))
    {
        *stack_header = nstack;
    }
    else
    {
        nstack->next = *stack_header;
        *stack_header = nstack;
    }
    
    return ret;
}
treenode_s* pop(stack_s** stack_header)
{
    stack_s* tnode = NULL;
    treenode_s* ttree = NULL;
    //printf("%s\n", __func__);
    if(*stack_header)
    {
        tnode = *stack_header;
        *stack_header = (*stack_header)->next;
        ttree = tnode->node;
        if(tnode)
        {
            if(tnode->node)
            {
                //printf("tnode: %d \n", tnode->node->data);
            }
            free(tnode);
        }
        return ttree;
    }
    else
    {
        printf("End of stack\n");
        return NULL;
    }
}
treenode_s* top(stack_s** stack_header)
{
    if(*stack_header)
    {
        return (*stack_header)->node;
    }
    else
    {
        printf("End of stack\n");
        return NULL;
    }
}
栈的头文件stack.h
#ifndef H_STACK
#define H_STACK
#include"bianli.h"
typedef struct stack_t
{
    treenode_s* node;
    struct stack_t* next;
}stack_s;
int push(stack_s** stack_h, treenode_s* tree);
treenode_s* pop(stack_s** stack_h);
treenode_s* top(stack_s** stack_h);
#endif
创建初始树的函数creatree()和访问树的函数visit()以及主函数main(),文件bianli.c:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include"bianli.h"
//创建TREE
treenode_s* creatree()
{
    treenode_s* tree = NULL;
    treenode_s* parent = NULL;
    treenode_s* cur = NULL;
    treenode_s* tmp = NULL;
    int i = 0, length = 0;
    int zhongzi[] = {8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15};
    length = sizeof(zhongzi)/4;
    printf("length: %d \n", length);
    for(i=0; i<length; i++)
    {
        tmp = (treenode_s*)malloc(sizeof(treenode_s));
        tmp->data = zhongzi[i];
        tmp->lchild = NULL;
        tmp->rchild = NULL;
        cur = tree;
        if(!cur)
        {
            tree = tmp;
            //printf("cur: %d\n", tmp->data);
            continue;
        }
        while(cur)
        {
            if(tmp->data > cur->data)
            {
                parent = cur;
                cur = cur->rchild;
                if(!cur)
                {
                    parent->rchild = tmp;
                    //printf("cur: %d\n", tmp->data);
                }
            }
            else if(tmp->data < cur->data)
            {
                parent = cur;
                cur = cur->lchild;
                if(!cur)
                {
                    parent->lchild = tmp;
                    //printf("cur: %d\n", tmp->data);
                }
            }
            else
            {
                printf("cur: %d\n", cur->data);
            }
        }
    }
    return tree;
}
int visit(treenode_s* tree)
{
    if(!tree)
    {
        return -1;
    }
    printf("%d\n", tree->data);
    return 0;
}
int main(void)
{
    int ret = 0;
    treenode_s* tree;
    tree = creatree();
    backorderbianli(tree);
    return ret;
}
treenode结构体定义和头文件,bianli.h:

#ifndef H_BIANLI
#define H_BIANLI
typedef struct treenode{
    int data;
    int tag;
    struct treenode* lchild;
    struct treenode* rchild;
}treenode_s;
int preorderbianli(treenode_s* tree);
int inorderbianli(treenode_s* tree);
int backorderbianli(treenode_s* tree);
#endif
二叉树的前序遍历,preorderbianli.c:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"stack.h"
int preorderbianli(treenode_s* tree)
{
    treenode_s* cur = tree;
    stack_s* stack_h = NULL;
    while(cur || top(&stack_h))
    {
        if(cur)
        {
            push(&stack_h, cur);
            visit(cur);
            cur = cur->lchild;
        }
        else if(cur = pop(&stack_h))
        {
            cur = cur->rchild;
        }
    }
    return 0;
}
二叉树的中序遍历,inorderbianli.c:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"stack.h"
int inorderbianli(treenode_s* tree)
{
    treenode_s* cur = tree;
    stack_s* stack_h = NULL;
    while(cur || top(&stack_h))
    {
        if(cur)
        {
            push(&stack_h, cur);
            cur = cur->lchild;
        }
        else if(cur = pop(&stack_h))
        {
            visit(cur);
            cur = cur->rchild;
        }
    }
    return 0;
} 

二叉树的后序遍历,backorderbianli.c:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"stack.h"
int backorderbianli(treenode_s* tree)
{
    treenode_s* cur=tree;
    stack_s* tree_stack = NULL;
    while(cur)
    {
        if(cur->lchild)
        {
            if(!cur->lchild->tag)
            {
                push(&tree_stack, cur);
                cur = cur->lchild;
                continue;
            }
        }
        if(cur->rchild)
        {
            if(!cur->rchild->tag)
            {
                push(&tree_stack, cur);
                cur = cur->rchild;
                continue;
            }
        }
        visit(cur);
        cur->tag = 1;
        cur = pop(&tree_stack);
    }
    return 0;
} 

后面有时间再补充说明。

 

 

[转自本人网易博客,原作时间:2015-01-13 18:00:27]

posted @ 2021-05-31 11:07  青原白鹿  阅读(264)  评论(0)    收藏  举报