数据结构——二叉树的创建和遍历

来源:互联网 发布:双色球九宫图算法杀号 编辑:程序博客网 时间:2024/06/05 17:14

在学习数据结构中,除了我们学习的线性结构,还有一种特殊而且重要的树形结构。最常见的二叉树,二叉树的种类又有很多。
二叉树概念
二叉树:一棵二叉树是结点的一个有限集合,该集合或者为空,或者是由一个根节点加上两棵分别称为左子树和右子树的二叉树组成。
满二叉树:在一棵二叉树中,如果所有分支结点都存在左子树和右子树,并且所有叶子节点都在同一层上。(满二叉树拥有的节点个数为2^k-1 k为二叉树的高度)

这里写图片描述

完全二叉树:除了最后一层,其余的非叶子节点都满,且最后一层的叶子集中在左边。
这里写图片描述

首先我们要学会这种特殊数据结构的创建,创建一个空树,完成拷贝构造,赋值操作符的重载,一颗二叉树的销毁。其次对一颗已有二叉树的遍历,前序遍历,中序遍历,后序遍历,还有层序遍历。然后实现遍历的递归形式的遍历以及非递归的循环方式的遍历。

还有需要我们注意的是,在二叉树的非递归遍历时,我们要用到STL中堆栈stack,还有队列queue。
stack具有FILO先进后出的特点。
queue具有FIFO先进先出的特点。

#include<iostream>#include<string>#include<queue>#include<stack>using namespace std;template<class T>struct BinarytreeNode{    BinarytreeNode(const T& data)    : _leftchild(NULL)    , _rightchild(NULL)    , _data(data)    {}    BinarytreeNode<T>* _leftchild;    BinarytreeNode<T>* _rightchild;    T _data;};template<class T>class Binarytree{    typedef BinarytreeNode<T> node;public:    Binarytree()        :root(NULL)    {}    Binarytree(const T* array, size_t size)    {        size_t index = 0;        CreatBinarytree(_root, array, size, index);    }    Binarytree(const Binarytree<T>& Bt)    {        _root = CopyBinarytree(Bt._root);    }        Binarytree<T>& operator=(Binarytree<T>& Bt)        {            if (this != &Bt)            {                Binarytree<T> newBt(Bt);                swap(_root, newBt._root);            }            return *this;        }    //  Binarytree<T>& operator=(const Binarytree<T>& Bt)    //  {    //      if (this != &Bt)    //      {    //          Destory(_root);    //          _root = CopyBinarytree(Bt._root);    //      }    //      return *this;    //  }    //遍历--(递归的形式遍历)Recursion    //前序遍历    void pre_traversal_Recursion()    {        _pre_traversal_Recursion(_root);        cout << endl;    }    //中序遍历    void in_traversal_Recursion()    {        _in_traversal_Recursion(_root);        cout << endl;    }    //后序遍历    void post_traversal_Recursion()    {        _post_traversal_Recursion(_root);        cout << endl;    }    //遍历--(循环的形式遍历)Non-Recursion    //前序遍历    void pre_traversal_NonRecursion()    {        _pre_traversal_NonRecursion(_root);    }    //中序遍历    void in_traversal_NonRecursion()    {        _in_traversal_NonRecursion(_root);    }    //后序遍历    void post_traversal_NonRecursion()    {        _post_traversal_NonRecursion(_root);    }    void Level_traversal()    {        _Level_traversal(_root);    }    ~Binarytree()    {        Destory(_root);    }private:    void CreatBinarytree(node*& root, const T* array, size_t size, size_t& index)    {        if (index < size && '#' != array[index])        {            root = new node(array[index]);            CreatBinarytree(root->_leftchild, array, size, ++index);            CreatBinarytree(root->_rightchild, array, size, ++index);        }    }    node* CopyBinarytree(node* root)    {        node* tmproot = NULL;        if (root)        {            tmproot = new node(root->_data);            if (root->_leftchild)                tmproot->_leftchild = CopyBinarytree(root->_leftchild);            if (root->_rightchild)                tmproot->_rightchild = CopyBinarytree(root->_rightchild);        }        return tmproot;    }    void _pre_traversal_Recursion(node* root)    {        if (root)        {            cout << root->_data << "-";            _pre_traversal_Recursion(root->_leftchild);            _pre_traversal_Recursion(root->_rightchild);        }    }    void _in_traversal_Recursion(node* root)    {        if (root)        {            _in_traversal_Recursion(root->_leftchild);            cout << root->_data << "-";            _in_traversal_Recursion(root->_rightchild);        }    }    void _post_traversal_Recursion(node* root)    {        if (root)        {            _post_traversal_Recursion(root->_leftchild);            _post_traversal_Recursion(root->_rightchild);            cout << root->_data << "-";        }    }    void _pre_traversal_NonRecursion(node* root)    {        if (NULL == root)            return;        stack<node*> s;        s.push(root);        while (!s.empty())        {            node* cur = s.top();            cout << cur->_data << " ";            s.pop();            if (cur->_rightchild)                s.push(cur->_rightchild);            if (cur->_leftchild)                s.push(cur->_leftchild);        }        cout << endl;    }    void _in_traversal_NonRecursion(node* root)    {        if (NULL == root)            return;        stack<node*> s;        node* cur = root;        while (cur || !s.empty())        {            while (cur)            {                s.push(cur);                cur = cur->_leftchild;            }            cur = s.top();            s.pop();            cout << cur->_data << " ";            cur = cur->_rightchild;        }        cout << endl;    }    void _post_traversal_NonRecursion(node* root)    {        if (NULL == root)            return;        stack<node*> s;        node* cur = root;        node* prev = NULL;        while (cur || !s.empty())        {            while (cur)            {                s.push(cur);                cur = cur->_leftchild;            }            cur = s.top();            if (cur->_rightchild == NULL || cur->_rightchild == prev)            {                cout << cur->_data << " ";                prev = cur;                s.pop();                cur = NULL;            }            else            {                cur = cur->_rightchild;            }        }        cout << endl;    }    void _Level_traversal(node* root)    {        if (!root)            return;        queue<node*> qu;        qu.push(root);        while (!qu.empty())        {            node* cur = qu.front();            cout << cur->_data << " ";            qu.pop();            if (cur->_leftchild)                qu.push(cur->_leftchild);            if (cur->_rightchild)                qu.push(cur->_rightchild);        }        cout << endl;    }    void Destory(node*& root)    {        if (root)        {            Destory(root->_leftchild);            Destory(root->_rightchild);            delete root;            root = NULL;        }    }private:    node* _root;};
原创粉丝点击