【无标题】

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;

typedef struct queue
{
    Node* dataarr[MAX];
    int front;
    int rear;
}Sqqueue;
void initqueue(Sqqueue* que)
{
    que->front = 0;
    que->rear = 0;
}
void inqueue(Sqqueue* que, Node* p)
{
    que->dataarr[que->rear++] = p;
    //que->rear++;
}
Node* outqueue(Sqqueue* que)
{
    if (que->front != que->rear)
    {
        Node* e;
        e = que->dataarr[que->front++];
        //que->front++;
        return e;
    }
    else
    {
        printf("the queue is empty\n");
    }

}
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 leveinorder(Node* root)
{
    //Stack* sta;
    //sta = (Stack*)malloc(sizeof(Stack));
    //InitStack(sta);

    Sqqueue* que;
    que = (Sqqueue*)malloc(sizeof(Sqqueue));
    initqueue(que);

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

    Node* temp;
    temp = malloc(sizeof(Node));


    inqueue(que, p);

    while (que->front != que->rear)
    {
        temp = outqueue(que);
        //PushStack(sta, temp);
        printf("%d ", temp->data);
        if (temp->left)
        {
            inqueue(que, temp->left);
        }
        if (temp->right)
        {
            inqueue(que, temp->right);
        }
    }


}
int main()
{
    int arr[5] = { 3,1,4,2,5 };
    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 non-recursion of leveorder result:");
    leveinorder(root);
    printf("\n");


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值