zoukankan      html  css  js  c++  java
  • 【数据结构】二叉树的创建、遍历以及广度深度优先搜索的实现

    先序、中序、后序遍历主要依靠递归来实现

    广度优先搜索主要依靠队列(先进先出),深度优先依靠栈(先进后出)

    #include<iostream>
    using namespace std;
    template<class T>
    struct Node {
        T data;
        Node<T> *next;
    };
    template<class T>
    class LinkStack {
    private:
        Node<T> *top;
    public:
        LinkStack();
        ~LinkStack();
        void Push(T x);
        T Pop();
        T Top();
        bool Empty();
    };
    template<class T>
    LinkStack<T>::LinkStack()
    {
        top = NULL;
    }
    template<class T>
    LinkStack<T>::~LinkStack()
    {
        Node<T> *p = top;
        while (p)
        {
            Node<T> *q = p;
            p = p->next;
            delete q;
        }
        top = NULL;
    }
    template<class T>
    void LinkStack<T>::Push(T x)
    {
        Node<T> *s = new Node<T>();
        s->data = x;
        s->next = top;
        top = s;
    }
    template<class T>
    T LinkStack<T>::Pop()
    {
        T x;
        if (top == NULL)
        {
            cout << "下溢" << endl;
            exit(1);
        }
        x = top->data;
        Node<T> *p = top;
        top = top->next;
        delete p;
        return x;
    }
    template<class T>
    T LinkStack<T>::Top()
    {
        if (top == NULL)
        {
            cout << "下溢" << endl;
            exit(1);
        }
        return top->data;
    }
    template<class T>
    bool LinkStack<T>::Empty()
    {
        return top == NULL;
    }
    栈的模板定义,点击查看
    #include<iostream>
    using namespace std;
    template<class T>
    struct QNode {
        T data;
        QNode *next;
    };
    template<class T>
    class LinkQueue {
    private:
        QNode<T> *front, *rear;
    public:
        LinkQueue();
        ~LinkQueue();
        void EnQueue(T x);
        T DeQueue();
        T GetQueue();
        bool Empty();
        void print();
    };
    template<class T>
    LinkQueue<T>::~LinkQueue()
    {
        rear = front = NULL;
    }
    template<class T>
    void LinkQueue<T>::print()
    {
        if (Empty())
        {
            cout << "队列为空!" << endl;
    
        }
        else
        {
            QNode<T> *p = front->next;
            while (p != NULL)
            {
                cout << p->data << " ";
                p = p->next;
            }
        }
        cout << endl;
    }
    template<class T>
    LinkQueue<T>::LinkQueue()
    {
        QNode<T> *s = new QNode<T>();
        s->next = NULL;
        front = rear = s;
    }
    template<class T>
    T LinkQueue<T>::DeQueue()
    {
        if (rear == front)
        {
            cout << "下溢" << endl;    //即队列为空
            exit(1);
        }
        QNode<T> *p = front->next;
        T x = p->data;
        front->next = p->next;
        if (p->next == NULL)
        {
            rear = front;
        }
        delete p;
        return x;
    }
    template<class T>
    T LinkQueue<T>::GetQueue()
    {
        if (rear == front)
        {
            cout << "下溢" << endl;    //即队列为空
            exit(1);
        }
        return front->next->data;
    }
    template<class T>
    void LinkQueue<T>::EnQueue(T x)
    {
        QNode<T> *s = new QNode<T>();
        s->data = x;
        s->next = NULL;
        rear->next = s;
        rear = s;
    }
    template<class T>
    bool LinkQueue<T>::Empty()
    {
        return rear == front;
    }
    队列的模板定义,点击查看

    这两个其实主要是类的定义不同,但struct的定义是一样的,所以可以自己优化一下。

    当然也可以直接调用c++标准库里的<stack> <queue> 都是可以直接使用的,在文章最后会放出标准代码,与上面的代码函数定义名称稍有不同。

    #include<iostream>
    #include<cstring>
    #include"Stack.h"
    #include"Queue.h"
    using namespace std;
    template<class T>
    struct BTNode {
        T data;
        BTNode<T> *lchild, *rchild;
    };
    template<class T>
    class BTree {
    private:
        BTNode<T> *root;
        void pre_Order(BTNode<T> * t);
        void in_Order(BTNode<T> * t);
        void post_Order(BTNode<T> * t);
        void depthFirstSearch(BTNode<T> * t);
        void breadthFirstSearch(BTNode<T> *t);
        BTNode<T>* create(char s[],int &pos);
    public:
        int length;
        void createBiTree();
        void preOrder();
        void inOrder();
        void postOrder();
        void DepthFirstSearch();
        void BreadthFirstSearch();
    };
    template<class T>
    void BTree<T>::breadthFirstSearch(BTNode<T> *t)
    {
        LinkQueue<BTNode<T>*> nodeQueue;
        nodeQueue.EnQueue(t);
        while (!nodeQueue.Empty())
        {
            BTNode<T> *node = nodeQueue.GetQueue();
            cout << node->data << ' ';
            nodeQueue.DeQueue();
            if (node->lchild)
            {
                nodeQueue.EnQueue(node->lchild);
            }
            if (node->rchild)
            {
                nodeQueue.EnQueue(node->rchild);
            }
        }
    }
    template<class T>
    void BTree<T>::BreadthFirstSearch()
    {
        cout << "广度优先搜索:";
        breadthFirstSearch(root);
        cout << endl;
    }
    template<class T>
    void BTree<T>::depthFirstSearch(BTNode<T> *t)
    {
        LinkStack <BTNode<T>*> nodeStack;
        nodeStack.Push(t);
        while (!nodeStack.Empty())
        {
            BTNode<T> *node = nodeStack.Top();
            cout << node->data << ' ';
            nodeStack.Pop();
            if (node->rchild)
            {
                nodeStack.Push(node->rchild);
            }
            if (node->lchild)
            {
                nodeStack.Push(node->lchild);
            }
        }
    }
    template<class T>
    void BTree<T>::DepthFirstSearch()
    {
        cout << "深度优先搜索:";
        depthFirstSearch(root);
        cout << endl;
    }
    template<class T>
    BTNode<T>* BTree<T>::create(char s[], int &pos)
    {
        ++pos;
        BTNode<T> *p;
        if (pos >= length)
        {
            return NULL;
        }
        else
        {
            if (s[pos] == '#')
            {
                p = NULL;
            }
            else
            {
                p = new BTNode<T>();
                p->data = s[pos];
                p->lchild = create(s,pos);
                p->rchild = create(s,pos);
            }
            return p;
        }
    }
    template<class T>
    void BTree<T>::in_Order(BTNode<T> *t)
    {
        if (t != NULL)
        {
            in_Order(t->lchild);
            cout << t->data << " ";
            in_Order(t->rchild);
        }
    }
    template<class T>
    void BTree<T>::post_Order(BTNode<T> *t)
    {
        if (t != NULL)
        {
            post_Order(t->lchild);
            post_Order(t->rchild);
            cout << t->data << " ";
        }
    }
    template<class T>
    void BTree<T>::pre_Order(BTNode<T> *t)
    {
        if (t != NULL)
        {
            cout << t->data << " ";
            pre_Order(t->lchild);
            pre_Order(t->rchild);
        }
    }
    template<class T>
    void BTree<T>::createBiTree()
    {
        cout << "请按照先序方式输入要创建的二叉树,#表示为空" << endl;
        int pos = -1;
        char str[50];
        cin >> str;
        length = strlen(str);
        root = create(str,pos);
        cout << "先序创建二叉树成功!" << endl;
    }
    template<class T>
    void BTree<T>::preOrder()
    {
        cout << "先序遍历:";
        pre_Order(root);
        cout << endl;
    }
    template<class T>
    void BTree<T>::inOrder()
    {
        cout << "中序遍历:";
        in_Order(root);
        cout << endl;
    }
    template<class T>
    void BTree<T>::postOrder()
    {
        cout << "后序序遍历:";
        post_Order(root);
        cout << endl;
    }
    int main()
    {
        BTree<char> test;
        test.createBiTree();
        test.preOrder();
        test.inOrder();
        test.postOrder();
        test.DepthFirstSearch();
        test.BreadthFirstSearch();
        system("pause");
        return 0;
    }
    //ABD##EF##G##C#H##
    #include<iostream>
    #include<cstring>
    #include<stack>
    #include<queue>
    using namespace std;
    template<class T>
    struct BTNode {
        T data;
        BTNode<T> *lchild, *rchild;
    };
    template<class T>
    class BTree {
    private:
        BTNode<T> *root;
        void pre_Order(BTNode<T> * t);
        void in_Order(BTNode<T> * t);
        void post_Order(BTNode<T> * t);
        void depthFirstSearch(BTNode<T> * t);
        void breadthFirstSearch(BTNode<T> *t);
        BTNode<T>* create(char s[],int &pos);
    public:
        int length;
        void createBiTree();
        void preOrder();
        void inOrder();
        void postOrder();
        void DepthFirstSearch();
        void BreadthFirstSearch();
    };
    template<class T>
    void BTree<T>::breadthFirstSearch(BTNode<T> *t)
    {
        queue<BTNode<T>*> nodeQueue;
        nodeQueue.push(t);
        while (!nodeQueue.empty())
        {
            BTNode<T> *node = nodeQueue.front();
            cout << node->data << ' ';
            nodeQueue.pop();
            if (node->lchild)
            {
                nodeQueue.push(node->lchild);
            }
            if (node->rchild)
            {
                nodeQueue.push(node->rchild);
            }
        }
    }
    template<class T>
    void BTree<T>::BreadthFirstSearch()
    {
        cout << "广度优先搜索:";
        breadthFirstSearch(root);
        cout << endl;
    }
    template<class T>
    void BTree<T>::depthFirstSearch(BTNode<T> *t)
    {
        stack<BTNode<T>*> nodeStack;
        nodeStack.push(t);
        while (!nodeStack.empty())
        {
            BTNode<T> *node = nodeStack.top();
            cout << node->data << ' ';
            nodeStack.pop();
            if (node->rchild)
            {
                nodeStack.push(node->rchild);
            }
            if (node->lchild)
            {
                nodeStack.push(node->lchild);
            }
        }
    }
    template<class T>
    void BTree<T>::DepthFirstSearch()
    {
        cout << "深度优先搜索:";
        depthFirstSearch(root);
        cout << endl;
    }
    template<class T>
    BTNode<T>* BTree<T>::create(char s[], int &pos)
    {
        ++pos;
        BTNode<T> *p;
        if (pos >= length)
        {
            return NULL;
        }
        else
        {
            if (s[pos] == '#')
            {
                p = NULL;
            }
            else
            {
                p = new BTNode<T>();
                p->data = s[pos];
                p->lchild = create(s,pos);
                p->rchild = create(s,pos);
            }
            return p;
        }
    }
    template<class T>
    void BTree<T>::in_Order(BTNode<T> *t)
    {
        if (t != NULL)
        {
            in_Order(t->lchild);
            cout << t->data << " ";
            in_Order(t->rchild);
        }
    }
    template<class T>
    void BTree<T>::post_Order(BTNode<T> *t)
    {
        if (t != NULL)
        {
            post_Order(t->lchild);
            post_Order(t->rchild);
            cout << t->data << " ";
        }
    }
    template<class T>
    void BTree<T>::pre_Order(BTNode<T> *t)
    {
        if (t != NULL)
        {
            cout << t->data << " ";
            pre_Order(t->lchild);
            pre_Order(t->rchild);
        }
    }
    template<class T>
    void BTree<T>::createBiTree()
    {
        cout << "请按照先序方式输入要创建的二叉树,#表示为空" << endl;
        int pos = -1;
        char str[50];
        cin >> str;
        length = strlen(str);
        root = create(str,pos);
        cout << "先序创建二叉树成功!" << endl;
    }
    template<class T>
    void BTree<T>::preOrder()
    {
        cout << "先序遍历:";
        pre_Order(root);
        cout << endl;
    }
    template<class T>
    void BTree<T>::inOrder()
    {
        cout << "中序遍历:";
        in_Order(root);
        cout << endl;
    }
    template<class T>
    void BTree<T>::postOrder()
    {
        cout << "后序序遍历:";
        post_Order(root);
        cout << endl;
    }
    调用库的形式
    int main()
    {
        BTree<char> test;
        test.createBiTree();
        test.preOrder();
        test.inOrder();
        test.postOrder();
        test.DepthFirstSearch();
        test.BreadthFirstSearch();
        system("pause");
        return 0;
    }
    测试代码

    测试输入:ABD##EF##G##C#H##

    结果

    请按照先序方式输入要创建的二叉树,#表示为空
    ABD##EF##G##C#H##
    先序创建二叉树成功!
    先序遍历:A B D E F G C H
    中序遍历:D B F E G A C H
    后序序遍历:D F G E B H C A
    深度优先搜索:A B D E F G C H
    广度优先搜索:A B C D E H F G

  • 相关阅读:
    机器学习实战-学习笔记-第一章
    Spark高级数据分析中文版-读者交流
    AAS代码第2章
    AWS-CDH5.5安装-软件下载
    AWS-CDH5.5安装 CM配置节点
    AWS-CDH5.5安装-安装
    Spark-1.5.1 on CDH-5.4.7
    tpch-kudu
    kudu playground
    CDH5.4.5运行多字符分割记录
  • 原文地址:https://www.cnblogs.com/robotpaul/p/9986265.html
Copyright © 2011-2022 走看看