【无标题】

该博客介绍了如何使用C语言非递归地实现二叉树的先序、中序和后序遍历。通过定义栈结构并结合节点数据,博主详细展示了每个遍历过程的步骤,并提供了完整的代码实现。此外,还提供了插入节点的方法以及对不同遍历顺序的打印功能。
摘要由CSDN通过智能技术生成

(非递归)C语言 二叉树 先序 ,中序,后续遍历

#include <stdio.h>
#include <stdlib.h>
#include<stdbool.h>
#define MAX 100
typedef struct node//build struct node
{
    int data;
    int tap;
    struct node* left;
    struct node* right;
} Node;


typedef struct
{
    Node* data[MAX];
    int top;
}Stack;


void InitStack(Stack* sta)
{
    sta->top = -1;
}

void Isempty(Stack* sta)
{
    sta->top = -1;
}

void PushStack(Stack* sta, Node* p)
{
    sta->data[sta->top++] = p;
}

Node* PopStack(Stack* sta)
{
    return sta->data[--sta->top];
}
void PrintfStack(Stack* sta)
{
    if (sta->top == -1)
    {
        printf("the stack is empty\n");
    }
    else
    {
        printf("the stack is:");
        int p;
        p = sta->top;
        while (p != -1)
        {
            printf("%d ", sta->data[p]);
            p--;
        }
    }

    printf("\n");
}

void insert_node(Node* p, int value)//insert value into the root
{
    Node* node = malloc(sizeof(Node));//storage allocation
    node->data = value;
    node->left = NULL;
    node->right = NULL;


    Node* temp = p;//temporary node
    while (temp != NULL)
    {
        if (value < temp->data)
        {
            if (temp->left == NULL)
            {
                temp->left = node;
                return;
            }
            else
            {
                temp = temp->left;
            }
        }
        else
        {
            if (temp->right == NULL)
            {
                temp->right = node;
                return;
            }
            else
            {
                temp = temp->right;
            }
        }
    }



}


void preorder(Node* node)
{
    if (node != NULL)
    {
        printf("%d ", node->data);
        preorder(node->left);
        preorder(node->right);
    }
}

void inorder(Node* node)
{
    if (node != NULL)
    {
        inorder(node->left);
        printf("%d ", node->data);
        inorder(node->right);
    }
}

void postorder(Node* node)
{
    if (node != NULL)
    {
        postorder(node->left);
        postorder(node->right);
        printf("%d ", node->data);
    }
}
void preorderstack(Node* root)
{
    Stack* sta;
    sta = (Stack*)malloc(sizeof(Stack));
    InitStack(sta);


    Node* p;
    p = malloc(sizeof(Node));
    p = root;

    while (p || sta->top != -1)
    {
        while (p)
        {
            printf("%d ", p->data);
            PushStack(sta, p);
            p = p->left;
        }
        if (sta->top != -1)
        {
            p = PopStack(sta);
            p = p->right;
        }
    }
}

void inorder_stack(Node* root)
{
    Stack* sta;
    sta = (Stack*)malloc(sizeof(Stack));
    InitStack(sta);


    Node* p;
    p = malloc(sizeof(Node));
    p = root;

    while (p || sta->top != -1)
    {
        while (p)
        {
            //printf("%d ", p->data);
            PushStack(sta, p);
            p = p->left;
        }
        if (sta->top != -1)
        {
            p = PopStack(sta);
            printf("%d ", p->data);
            p = p->right;
        }
    }
}
void postorder_stack(Node* root)
{

    Stack* sta;
    sta = (Stack*)malloc(sizeof(Stack));
    InitStack(sta);


    Node* p;
    p = malloc(sizeof(Node));
    p = root;

    int falag = 0;
    while (p || sta->top != -1)
    {
        while (p)
        {
            p->tap = 1;
            PushStack(sta, p);
            p = p->left;
        }
        if (sta->top != -1)
        {
            p = PopStack(sta);
            //printf("%d ", p->data);
            //p = p->right;
            if (p->tap == 1)
            {
                p->tap++;
                PushStack(sta, p);
                p = p->right;
            }
            else if (p->tap == 2)
            {
                printf("%d ", p->data);
                p = NULL;
            }
        }
    }
}
int main()
{
    int arr[5] = { 3,2,5,4,6 };
    Node* root = malloc(sizeof(Node));
    root->data = arr[0];
    root->left = NULL;
    root->right = NULL;

    int i;
    for (i = 1; i < 5; i++)
    {
        insert_node(root, arr[i]);
    }

    printf("the preorder result:");
    preorder(root);
    printf("\n");
    printf("the inorder result:");
    inorder(root);
    printf("\n");
    printf("the postorder result:");
    postorder(root);
    printf("\n");

    printf("the non-recursion of preorder result:");
    preorderstack(root);
    printf("\n");

    printf("the non-recursion of preorder result:");
    inorder_stack(root);
    printf("\n");

    printf("the non-recursion of preorder result:");
    postorder_stack(root);
    printf("\n");

    printf("the non-recursion of leveorder result:");
    leveinorder(root);
    printf("\n");

    CountLeafNode(root);
    CountTwoNode(root);
    CountAllNode(root);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值