zoukankan      html  css  js  c++  java
  • 二叉树的基本操作

    #include <iostream>
    
    using namespace std;
    
    typedef struct Tree
    {
        char data1;
        struct Tree *lchild, *rchild;
    }Tree;
    
    template<typename T>
    struct Node
    {
        T data;
        Node<T>* next;
    };
    
    template<typename T>
    class stack
    {
        Node<T>* top;
    public:
        stack() :top(NULL){}
        void push(T n);
        T pop();
        bool stackEmpty();
        T getTop();
        ~stack(){}
        void print();
    };
    
    template<typename T>
    void stack<T>::push(T n)
    {
        Node<T>* r = new Node<T>;
        r->data = n;
        r->next = top;
        top = r;
    }
    
    template<typename T>
    T stack<T>::pop()
    {
        Node<T> *ptr = top;
        top = top->next;
        T t = ptr->data;
        delete ptr;
        return t;
    }
    
    
    template<typename T>
    void stack<T>::print()
    {
        for (Node<T>* p = top; p; p = p->next)
            cout << p->data << "  ";
    }
    
    template<typename T>
    T stack<T>::getTop()
    {
        return top->data;
    }
    
    template<typename T>
    bool stack<T>::stackEmpty()
    {
        if (top)return false;
        else return true;
    }
    //////////////////////////////////////////////
    
    
    void firstPutTree(Tree* &T)
    {//用我自己的模板栈实现的  先序 遍历 
        stack<Tree*> s;
        Tree* p = T;
        Tree* q;
        for (; p || !s.stackEmpty();)
        {
            if (p)
            {
                s.push(p);
                p = p->lchild;
                cout << s.getTop()->data1;
            }
            else
            {
                q = s.pop();
                p = q->rchild;
            }
        }
    }
    
    void lastPutTree(Tree* &T)
    {//用我自己的模板栈实现的  后序 遍历 
        stack<Tree*> s;
        Tree* p = T;
        Tree* q;
        for (; p || !s.stackEmpty();)
        {
            if (p)
            {
                s.push(p);
                p = p->lchild;
    
            }
            else
            {
                q = s.pop();
                if ((p = q->rchild) == NULL)
                    cout << q->data1;
            }
        }
    }
    
    void midPutTree(Tree* &T)
    {//用我自己的模板栈实现的  中序 遍历 
        stack<Tree*> s;
        Tree* p = T;
        Tree* q;
        for (; p || !s.stackEmpty();)
        {
            if (p)
            {
                s.push(p);
                p = p->lchild;
    
            }
            else
            {
                q = s.pop();
                cout << q->data1;
                p = q->rchild;
            }
        }
    }
    /*
    void firstPutTree(Tree* T)
    {
    if(T)
    {
    firstPutTree(T->lchild);
    cout<<T->data<<" ";
    firstPutTree(T->rchild);
    }
    }*/
    
    void firstCreateTree(Tree* &T)
    {//先序创建 
        char ch;
        cin >> ch;
        if (ch == '#')T = NULL;
        else
        {
            T = new Tree;
            T->data1 = ch;
            firstCreateTree(T->lchild);
            firstCreateTree(T->rchild);
        }
    }
    
    void midCreateTree(Tree* &T)
    {//中序创建 
        char ch;
        cin >> ch;
        if (ch == '#')T = NULL;
        else
        {
            T = new Tree;
            midCreateTree(T->lchild);
            T->data1 = ch;
            midCreateTree(T->rchild);
        }
    }
    
    void lastCreateTree(Tree* &T)
    {//后序创建 
        char ch;
        cin >> ch;
        if (ch == '#')T = NULL;
        else
        {
            T = new Tree;
            lastCreateTree(T->lchild);
            lastCreateTree(T->rchild);
            T->data1 = ch;
        }
    }
    
    void copy(Tree* &T, Tree* &T2)
    {//复制二叉树 
        if (T == NULL)
        {
            T2 = NULL;
            return;
        }
        else
        {
            T2 = new Tree;
            T2->data1 = T->data1;
            copy(T->lchild, T2->lchild);
            copy(T->rchild, T2->rchild);
        }
    }
    
    int deepth(Tree* &T)
    {    
        int m, n;
        if (T == NULL)return 0;
        else
        {
            m = deepth(T->lchild);
            n = deepth(T->rchild);
            if (m > n)
                return m + 1;
            else return n + 1;
        }
    }
    
    int NodeCount(Tree* &T)
    {
        if (T == NULL)
            return 0;
        else
            return NodeCount(T->lchild) + NodeCount(T->rchild) +1 ;
    }
    int main()
    {
        Tree* T, *T2;
        cout << "1.先序创建" << endl;
        cout << "2.中序创建" << endl;
        cout << "3.后序创建" << endl;
        cout << "4.先序遍历" << endl;
        cout << "5.中序遍历" << endl;
        cout << "6.后序遍历" << endl;
        cout << "7.复制" << endl;
        cout << "8.深度" << endl;
        cout << "9.结点" << endl;
        int i;
        cin >> i;
        for (; i != 0;)
        {
            switch (i)
            {
            case 1:firstCreateTree(T); break;
            case 2:midCreateTree(T); break;
            case 3:lastCreateTree(T); break;
            case 4:firstPutTree(T); break;
            case 5:midPutTree(T2); break;
            case 6:lastPutTree(T); break;
            case 7:copy(T, T2); break;
            case 8:cout<<deepth(T); break;
            case 9:cout << NodeCount(T); break;
            }
            cout << endl << "which : ";
            cin >> i;
        }
    }
  • 相关阅读:
    POJ2686 Traveling by Stagecoach(状压DP+SPFA)
    POJ3250 Bad Hair Day(单调栈)
    POJ3493 Largest Submatrix of All 1’s(单调栈)
    UVA 10160 Servicing Stations(状态压缩+迭代加深)
    POJ 2187 Beauty Contest
    HDU 6017 Girls Love 233(多态继承DP)
    POJ 2932 Coneology(扫描线)
    POJ 1127 Jack Straws (计算几何)
    挑战程序设计竞赛 3.5 借助水流解决问题的网络流
    AOJ 2230 How to Create a Good Game(费用流)
  • 原文地址:https://www.cnblogs.com/vhyc/p/5499716.html
Copyright © 2011-2022 走看看