C语言二叉树前序、中序、后序、层序遍历非递归实现 内含测试用例+输出图片

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct treeNode {
    char data;
    struct treeNode* LChild;
    struct treeNode* RChild;
};

struct Node {
    struct treeNode* data;
    struct Node* next;
};

struct Stack {
    int stackSize;
    struct Node* stackTop;
};

struct Queue {
    struct Node* front;
    struct Node* rear;
};

struct Node* createList()
{
    struct Node* headNode = (struct Node *)malloc(sizeof(struct Node));
    headNode->next = NULL;
    return headNode;
}

struct Node* createNode(struct treeNode* currentNode)
{
    struct Node* newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = currentNode;
    newNode->next = NULL;
    return newNode;
}

struct Stack* InitStack()
{
    struct Stack* newStack = (struct Stack *)malloc(sizeof(struct Stack));
    newStack->stackSize = 0;
    newStack->stackTop = createList();
    return newStack;
}

struct treeNode* StackTop(struct Stack* newStack)
{
    if(newStack->stackSize == 0)
        return NULL;
    else
        return newStack->stackTop->next->data;
}

int StackEmpty(struct Stack* newStack)
{
    if(newStack->stackSize == 0)
        return -1;
    else
        return 0;
}

void insertNode_head(struct Node* headNode, struct treeNode* currentNode)
{
    struct Node* newNode = createNode(currentNode);
    newNode->next = headNode->next;
    headNode->next = newNode;
}

void deleteNode_head(struct Node* headNode)
{
    struct Node* deleteNode = headNode->next;
    headNode->next = deleteNode->next;
    free(deleteNode);
    deleteNode = NULL;
}

void StackPush(struct Stack* newStack, struct treeNode* currentNode)
{
    insertNode_head(newStack->stackTop, currentNode);
    newStack->stackSize++;
}

void StackPop(struct Stack* newStack)
{
    if(newStack->stackSize == 0)
    {
        return;
    }
    else
    {
        deleteNode_head(newStack->stackTop);
        newStack->stackSize--;
    }
}

struct Queue* Initqueue(struct Queue* queue)
{
    queue->front = (struct Node *)malloc(sizeof(struct treeNode));
    queue->front->next = NULL;
    queue->rear = queue->front;
    return queue;
}

void QueuePush(struct Queue* queue, struct treeNode* currentNode){
    struct Node* pNew = (struct Node *)malloc(sizeof(struct Node));
    pNew->data = currentNode;
    pNew->next = NULL;
    queue->rear->next = pNew;
    queue->rear = pNew;
}
 
struct treeNode* QueuePop(struct Queue* queue){
    struct Node* pTemp = (struct Node *)malloc(sizeof(struct Node));
    pTemp = queue->front->next;
    if(pTemp->next == NULL){
        queue->rear = queue->front;
    }else{
        queue->front->next = pTemp->next;
    }
    struct treeNode* x = pTemp->data;
    free(pTemp);
    return x;
}

struct treeNode *InitTree()
{
    struct treeNode *node;
    if(node = (struct treeNode *)malloc(sizeof(struct treeNode)))
    {
        printf("Enter the data of root:\n");
        scanf("%s", &node->data);
        node->LChild = NULL;
        node->RChild = NULL;
        if(node != NULL)
        {
            return node;
        }
        else
        {
            return NULL;
        }
    }
    return NULL;
}

struct treeNode *TreeFindNode(struct treeNode *node, char data)
{
    struct treeNode *ptr;
    if(node == NULL)
    {
        return NULL;
    }
    else
    {
        if(node->data == data)
        {
            return node;
        }
        else
        {
            if(ptr = TreeFindNode(node->LChild, data))
            {
                return ptr;
            }
            else if(ptr = TreeFindNode(node->RChild, data))
            {
                return ptr;
            }
            else
            {
                return NULL;
            }
        }
    }
}

void AddTreeNode(struct treeNode* node)
{
    struct treeNode *pnode, *parent;
    char data;
    char menusel;
    if(pnode = (struct treeNode *)malloc(sizeof(struct treeNode)))
    {
        printf("Input the data of Binary tree node:\n");
        fflush(stdin);
        scanf("%s", &pnode->data);
        pnode->LChild = NULL;
        pnode->RChild = NULL;
        printf("Input the data of the parent of this node:");
        fflush(stdin);
        scanf("%s", &data);
        parent = TreeFindNode(node, data);
        if(!parent)
        {
            printf("Can't find parent node!\n");
            free(pnode);
            return;
        }
        printf("1.add node to Left subtree\n2.add node to Right subtree\n");
        do
        {
            menusel = getchar();
            menusel -= '0';
            if(menusel == 1 || menusel == 2)
            {
                if(parent == NULL)
                {
                    printf("Parent node is not exit, please site parent node at first!\n");
                }
                else
                {
                    switch(menusel)
                    {
                        case 1:
                            if(parent->LChild)
                            {
                                printf("Left subtree node is not empty!\n");
                            }
                            else
                            {
                                parent->LChild = pnode;
                            }
                            break;
                        case 2:
                            if(parent->RChild)
                            {
                                printf("Right subtree node is not empty!\n");
                            }
                            else
                            {
                                parent->RChild = pnode;
                            }
                            break;
                        default:
                            printf("Invalid parameter!\n");
                    }
                }
            }
        }
        while(menusel != 1 && menusel != 2);
    }
}

void ClearTree(struct treeNode* node)
{
    if(node)
    {
        ClearTree(node->LChild);
        ClearTree(node->RChild);
        free(node);
        node = NULL;
    }
}

void PreOrder(struct treeNode* root)
{
    if(root == NULL)
        return;
    struct Stack* stack = (struct Stack *)malloc(sizeof(struct Stack));
    stack = InitStack();
    StackPush(stack, root);
    struct treeNode* tmp;
    while(!StackEmpty(stack))
    {
        tmp = StackTop(stack);
        printf("%c ", tmp->data);
        StackPop(stack);
        if(tmp->RChild != NULL)
            StackPush(stack, tmp->RChild);                                                                                              
        if(tmp->LChild != NULL)
            StackPush(stack, tmp->LChild);
    }
    return;
}

void InOrder(struct treeNode* root)
{
    if(root == NULL)
        return;
    struct Stack* stack = (struct Stack *)malloc(sizeof(struct Stack));
    stack = InitStack();
    struct treeNode* tmp;
    tmp = root;
    while(!StackEmpty(stack) || tmp != NULL)
    {
        while(tmp != NULL)
        {
            StackPush(stack, tmp);
            tmp = tmp->LChild;
        }
        tmp = StackTop(stack);
        printf("%c ", tmp->data);                                                                                                      
        tmp = tmp->RChild;
        StackPop(stack);
    }
}

void PostOrder(struct treeNode* root)
{
    if(root == NULL)
        return;
    struct treeNode* t = root;
    struct Stack* stack1 = (struct Stack *)malloc(sizeof(struct Stack));
    struct Stack* stack2 = (struct Stack *)malloc(sizeof(struct Stack));
    stack1 = InitStack();
    stack2 = InitStack();
    StackPush(stack1, t);
    while(!StackEmpty(stack1))
    {
        t = StackTop(stack1);
        StackPop(stack1);
        StackPush(stack2, t);
        if(t->LChild != NULL) StackPush(stack1, t->LChild);
        if(t->RChild != NULL) StackPush(stack1, t->RChild);
    }
    while(!StackEmpty(stack2))
    {
        t = StackTop(stack2);
        StackPop(stack2);
        printf("%c ", t->data);
    }
}

void LevelOrder(struct treeNode* root)
{
    if(root == NULL)
        return;
    struct Queue* queue = (struct Queue *)malloc(sizeof(struct Queue));
    queue = Initqueue(queue);
    QueuePush(queue, root);
    while(queue->rear != queue->front)
    {
        struct treeNode* t = QueuePop(queue);
        printf("%c ", t->data);
        if(t->LChild)
        {
            QueuePush(queue, t->LChild);
        }
        if(t->RChild)
        {
            QueuePush(queue, t->RChild);
        }
    }
}

int main()
{
    struct treeNode *root = NULL;
    char menusel;
    root = InitTree();
    do
    {
        printf("Please select the menu to add the nodes of the binary tree\n");
        printf("0.exit\t");
        printf("1.add Binary tree node\n");
        menusel = getch();
        switch(menusel)
        {
            case '1':
                AddTreeNode(root);
                break;
            case '0':
                break;
            default:
                ;
        }
    } while(menusel != '0');
    do
    {
        printf("Please select the menu to traverse the binary tree. Enter 0 to exit:\n");
        printf("1.Preorder traversal\t");
        printf("2.Inorder traversal\n");
        printf("3.Postorder traversal\t");
        printf("4.Sequence traversal\n");
        menusel = getch();
        switch(menusel)
        {
            case '0':
                break;
            case '1':
                printf("\nThe result of preorder traversal: ");
                PreOrder(root);
                printf("\n");
                break;
            case '2':
                printf("\nThe result of inorder traversal: ");
                InOrder(root);
                printf("\n");
                break;
            case '3':
                printf("\nThe result of postorder traversal: ");
                PostOrder(root);
                printf("\n");
                break;
            case '4':
                printf("\nThe result of Sequence traversal: ");
                LevelOrder(root);
                printf("\n");
                break;
            default:
                ;
        }
    } while(menusel != '0');
    ClearTree(root);
    root = NULL;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值