• 二叉树的基本操作


    #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;
        }
    }
  • 相关阅读:
    较快者等待较慢者发出通知
    Uint and uintptr in golang 非类型安全指针
    C10K C10M 通过DPDK+用户态协议栈来进行内核旁路来提升网络性能
    大型产品团队敏捷发布火车运行指南
    注册中心
    RPA(Robotic Process Automation,机器人流程自动化)
    Actor model
    基于Protobuf共享字段的分包和透传零拷贝技术
    基于etcd实现大规模服务治理应用实战
    删除 服务 注册表
  • 原文地址:https://www.cnblogs.com/vhyc/p/5499716.html
Copyright © 2020-2023  润新知