zoukankan      html  css  js  c++  java
  • 二叉树后序遍历法(递归与非递归)

    递归:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MaxSize 30;
    typedef int ElementType;
    
    struct TreeNode {
        ElementType Data;
        struct TreeNode *Left;
        struct TreeNode *Right;
        struct TreeNode *Next;
    };
    typedef struct TreeNode *BinTree;
    typedef struct TreeNode *Stack;
    
    Stack pop(Stack S);
    Stack CreateStack();
    void push(Stack s, BinTree T);
    BinTree CreateTreeNode();
    BinTree CreateBinTree(int op[], int in[], int par_s);
    void PostOrderTraversal(BinTree BT, ElementType root);
    
    int main()
    {
        BinTree BT;
        int N, i, e;
        int index_j = 0, index_i = 0;
        char action[5];
        scanf("%d", &N);
        int *in = (int*)malloc(sizeof(int)*N);
        int *op = (int*)malloc(sizeof(int) * 2 * N);
        getchar();
        for (i = 0; i < N * 2; i++)
        {
            scanf("%s", action);
            getchar();
            if (strcmp(action, "Push") == 0)
            {
                scanf("%d", &e);
                in[index_i++] = e;
                op[i] = 1;
            }
            else
            {
                op[i] = 0;
            }
        }
        BT=CreateBinTree(op, in, N);
        PostOrderTraversal(BT, BT->Data);
    }
    
    Stack CreateStack()
    {
        Stack s;
        s = (Stack)malloc(sizeof(struct TreeNode));
        s->Next = NULL;
        return s;
    }
    
    void push(Stack s, BinTree T)
    {
        if (s == NULL)
            s = CreateStack();
        T->Next = s->Next;
        s->Next = T;
    }
    
    Stack pop(Stack S)
    {
        Stack temp = NULL,n;
        if (!S || S->Next == NULL)
            return NULL;
        temp = S->Next;
        S->Next = temp->Next;
        n = temp;
        temp = NULL;
        return n;
    }
    
    BinTree CreateTreeNode()
    {
        BinTree Node = (BinTree)malloc(sizeof(struct TreeNode));
        Node->Left = NULL;
        Node->Right = NULL;
        return Node;
    }
    
    BinTree CreateBinTree(int op[], int in[], int par_s)
    {
        BinTree root = NULL;
        BinTree temp = NULL, Node = NULL;
        Stack S;
        int i = 0, n = 0;
        if (par_s == 1)
        {
            root = CreateTreeNode();
            root->Data = in[i];
            return root;
        }
        else if (par_s == 0)
            return NULL;
        else
        {
            S = CreateStack();
            root = CreateTreeNode();
            root->Data = in[i];
            Node = root;
            temp = root;
            push(S, temp);
            n++;
            for (i = 1; i < 2 * par_s; i++)
            {
                if (op[i])
                {
                    
                    Node = CreateTreeNode();
                    Node->Data = in[n];
                    push(S, Node);
                    if (temp->Left == NULL && op[i - 1])
                        temp->Left = Node;
                    else
                        temp->Right = Node;
                    temp = Node;
                    n++;
                }
                else
                {
                    temp=pop(S);
                }
            }
        }
        free(S);
        return root;
    }
    
    void PostOrderTraversal(BinTree BT,ElementType root)
    {
        if (BT->Left)
            PostOrderTraversal(BT->Left, root);
        if (BT->Right)
            PostOrderTraversal(BT->Right, root);
        if (BT->Data == root)
            printf("%d", BT->Data);
        else
            printf("%d ", BT->Data);
    }

    非递归:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MaxSize 30;
    typedef int ElementType;
    
    struct TreeNode {
        ElementType Data;
        struct TreeNode *Left;
        struct TreeNode *Right;
        struct TreeNode *Next;
    };
    typedef struct TreeNode *BinTree;
    typedef struct TreeNode *Stack;
    
    Stack pop(Stack S);
    Stack CreateStack();
    void push(Stack s, BinTree T);
    BinTree CreateTreeNode();
    BinTree CreateBinTree(int op[], int in[], int par_s);
    void PostOrderTraversal(BinTree T, ElementType root);
    
    int main()
    {
        BinTree BT;
        int N, i, e;
        int index_j = 0, index_i = 0;
        char action[5];
        scanf_s("%d", &N);
        int *in = (int*)malloc(sizeof(int)*N);
        int *op = (int*)malloc(sizeof(int) * 2 * N);
        getchar();
        for (i = 0; i < N * 2; i++)
        {
            scanf_s("%s", action, 5);
            getchar();
            if (strcmp(action, "Push") == 0)
            {
                scanf_s("%d", &e);
                in[index_i++] = e;
                op[i] = 1;
            }
            else
                op[i] = 0;
        }
        BT=CreateBinTree(op, in, N);
        PostOrderTraversal(BT, BT->Data);
    }
    
    Stack CreateStack()
    {
        Stack s;
        s = (Stack)malloc(sizeof(struct TreeNode));
        s->Next = NULL;
        return s;
    }
    
    void push(Stack s, BinTree T)
    {
        if (s == NULL)
            s = CreateStack();
        T->Next = s->Next;
        s->Next = T;
    }
    
    Stack pop(Stack S)
    {
        Stack temp = NULL,n;
        if (!S || S->Next == NULL)
            return NULL;
        temp = S->Next;
        S->Next = temp->Next;
        n = temp;
        temp = NULL;
        return n;
    }
    
    BinTree CreateTreeNode()
    {
        BinTree Node = (BinTree)malloc(sizeof(struct TreeNode));
        Node->Left = NULL;
        Node->Right = NULL;
        return Node;
    }
    
    BinTree CreateBinTree(int op[], int in[], int par_s)
    {
        BinTree root = NULL;
        BinTree temp = NULL, Node = NULL;
        Stack S;
        int i = 0, n = 0;
        if (par_s == 1)
        {
            root = CreateTreeNode();
            root->Data = in[i];
            return root;
        }
        else if (par_s == 0)
            return NULL;
        else
        {
            S = CreateStack();
            root = CreateTreeNode();
            root->Data = in[i];
            Node = root;
            temp = root;
            push(S, temp);
            n++;
            for (i = 1; i < 2 * par_s; i++)
            {
                if (op[i])
                {
                    
                    Node = CreateTreeNode();
                    Node->Data = in[n];
                    push(S, Node);
                    if (temp->Left == NULL && op[i - 1])
                        temp->Left = Node;
                    else
                        temp->Right = Node;
                    temp = Node;
                    n++;
                }
                else
                    temp=pop(S);
            }
        }
        free(S);
        return root;
    }
    
    void PostOrderTraversal(BinTree T,ElementType root)
    {
        BinTree temp = NULL;
        Stack S = CreateStack();
        while (T || S->Next) {
            while (T) {
                push(S, T);
                T = T->Left;
            }
            if (S->Next)
            {
                T = pop(S);
                push(S, T);
                T = T->Right;
                if (!T||T==temp)
                {
                    temp = pop(S);
                    printf("%d", temp->Data);
                    if (temp->Data != root)
                        printf(" ");
                    T = NULL;
                }
            }
        }
    }

    这里用的是堆栈构树的。

  • 相关阅读:
    缓冲区溢出学习笔记 一
    什么是TNotifyEvent(转)
    Delphi中dll的使用(转)
    bash 编程
    感觉自己真的很幸运
    开始写程序,发现自己很弱智
    你是我心中不变的新绿(转载)
    做一次年度总结和清理
    今天简直成精力狂了^_^
    Double Do weeks Again : )
  • 原文地址:https://www.cnblogs.com/2293002826PYozo/p/11284958.html
Copyright © 2011-2022 走看看