树和二叉树(二)

来源:互联网 发布:淘宝助理无销售属性 编辑:程序博客网 时间:2024/06/06 03:18

这里写图片描述

以下是二叉树的基本代码

#include<iostream>#include<assert.h>#include <stack>using namespace std;template<class T>struct  BinaryTreeNode                   {    T data;              BinaryTreeNode<T>* _left;    BinaryTreeNode<T>* _right;    BinaryTreeNode(const T& x)        :_left(NULL)         , _right(NULL)        , data(x)    {}};template<class T>class BinaryTree{    typedef BinaryTreeNode<T> Node;public:    BinaryTree() //无参构造函数        :_root(NULL)    {}    BinaryTree(T* a, size_t n, const T& invalid) //构造函数    {        size_t index = 0;        _root = CreateTree(a, n, invalid, index);    }    Node* _Copy(Node* root)    {        if (root == NULL)            return NULL;        Node* newRoot = new Node(root->data);        newRoot->_left = _Copy(root->_left);        newRoot->_right = _Copy(root->_right);        return newRoot;    }    BinaryTree(const BinaryTree<T>& t) //拷贝构造    {        _root = _Copy(t._root);    }    //BinaryTree<T>& operator=(const BinaryTree<T>& t) //赋值运算符的重载-传统写法    //{    //  if (this != &t)    //  {    //      Destroy(_root);    //      _root = _Copy(t._root);    //  }    //  return *this;    //}    BinaryTree<T>& operator=(BinaryTree<T> t) //赋值运算符的重载-现代写法    {        swap(_root, t._root);        return *this;    }    ~BinaryTree() //析构函数    {        Destroy(_root);    }    void Destroy(Node* root)    {        if (root == NULL)            return;        Destroy(root->_left);        Destroy(root->_right);        delete root;    }    size_t GetKlevel(size_t k) //第k层节点个数    {        assert(k > 0);        return _GetKlevel(_root, k);    }    Node* Find(const T& x) //查找二叉树节点    {        return _Find(_root,x);    }    size_t Size() //二叉树节点总个数    {        return _Size(_root);    }    size_t LeafSize() //叶子节点个数    {        return _LeafSize(_root);    }    size_t Depth() //二叉树深度    {        return _Depth(_root);    }    void PreOrderHelp() //前序遍历    {        _PreOrderHelp(_root);        cout << endl;    }    void PrevOrderNonR() //前序遍历-非递归    {        stack<Node*> s;        Node* cur = _root;        while (cur || !s.empty())        {            while (cur)            {                cout << cur->data << " ";                s.push(cur);                cur = cur->_left;            }            // top从栈取出来表示这个节点的左子树访问过了            // 剩余右子树还没访问,循环子问题访问右树            Node* top = s.top();            s.pop();            // 子问题的方式访问右树            cur = top->_right;        }        cout << endl;    }    void InOrderNonR() //中序遍历-非递归    {        stack<Node*> s;        Node* cur = _root;        while (cur || !s.empty())        {            while (cur)            {                s.push(cur);                cur = cur->_left;            }            Node* top = s.top();            s.pop();            cout << top->data << " ";            // 子问题的方式访问右树            cur = top->_right;        }        cout << endl;    }    void InOrderHelp() //中序遍历    {        _InOrderHelp(_root);        cout << endl;    }    void PostOrderNonR() //后序遍历-非递归    {        stack<Node*> s;        Node* cur = _root;        Node* prev = NULL;        while (cur || !s.empty())        {            while (cur)            {                s.push(cur);                cur = cur->_left;            }            Node* top = s.top();            if (top->_right == NULL || top->_right == prev)            {                cout << top->data << " ";                prev = top;                s.pop();            }            else            {                // 子问题                cur = top->_right;            }        }            cout << endl;    }    void PostOrderHelp() //后序遍历    {        _PostOrderHelp(_root);        cout << endl;    }protected:    Node* CreateTree(T* a, size_t n, const T& invalid, size_t& index) //创建子树    {        Node* root = NULL;        if (index < n&&a[index] != invalid)        {            root = new Node(a[index]);            root->_left = CreateTree(a, n, invalid, ++index);            root->_right = CreateTree(a, n, invalid, ++index);        }        return root;    }    size_t _GetKlevel(Node* root, size_t k) //第k层节点个数    {        assert(k > 0);        if (root == NULL)            return 0;        if (k == 1)            return 1;        if (k > 1)            return _GetKlevel(root->_left, k - 1) + _GetKlevel(root->_right, k - 1);    }    Node* _Find(Node* root, const T& x) //二叉树中查找某个值    {        if (root == NULL)            return NULL;        if (root->data == x)            return root;        Node* ret = _Find(root->_left, x);        if (ret)            return ret;        return _Find(root->_right, x);    }    size_t _Size(Node* root) //二叉树节点总个数    {        if (root == NULL)            return NULL;        else            return _Size(root->_left) + _Size(root->_right) + 1;    }    size_t _LeafSize(Node* root) //叶子节点个数    {        if (root == NULL)        {            return 0;        }        if (root->_left == NULL && root->_right == NULL)        {            return 1;        }        return _LeafSize(root->_left) + _LeafSize(root->_right);    }    size_t _Depth(Node* root)//求二叉树的高度    {        int depthval = 0;        if (root == NULL)            return 0;        else if (root->_left == NULL && root->_right == NULL)            return 1;        else        {            size_t dl = _Depth(root->_left);            size_t dr = _Depth(root->_right);            return depthval = 1 + (dl > dr ? dl : dr);        }    }    void _PreOrderHelp(const Node *root)const //前序遍历    {        if (root != NULL)        {            cout << root->data << " ";            _PreOrderHelp(root->_left);            _PreOrderHelp(root->_right);        }    }    void _InOrderHelp(const Node *root)const //中序遍历    {        if (root != NULL)        {            _InOrderHelp(root->_left);            cout << root->data << " ";            _InOrderHelp(root->_right);        }    }    void _PostOrderHelp(const Node* root)const //后序遍历    {        if (root != NULL)        {            _PostOrderHelp(root->_left);            _PostOrderHelp(root->_right);            cout << root->data << " ";        }    }protected:    Node* _root;};void test() //测试函数{    int array[15] = { 1, 2, '#', 3, '#', '#', 4, 5, '#', 6, '#', 7, '#', '#', 8 };    BinaryTree<int> T1(array, sizeof(array) / sizeof(array[0]), '#');    //T1.PreOrderHelp();    //T1.InOrderHelp();    //T1.PostOrderHelp();    //T1.PreOrderHelp();    //T1.InOrderNonR();    //T1.PostOrderNonR();    //cout << T1.GetKlevel(3) << endl;    //cout << T1.Find(3) << endl;    //cout << T1.Depth() << endl;    //cout << T1.LeafSize() << endl;    //cout << T1.Size() << endl;    BinaryTree<int> T2(T1);    T2.PreOrderHelp();    T2.InOrderHelp();    T2.PostOrderHelp();    T2.PreOrderHelp();    T2.InOrderNonR();    T2.PostOrderNonR();    cout << T2.GetKlevel(3) << endl;    cout << T2.Find(3) << endl;    cout << T2.Depth() << endl;    cout << T2.LeafSize() << endl;    cout << T2.Size() << endl;}int main(){    test();    system("pause");    return 0;}

关于后序遍历非递归算法的一些问题

这里写图片描述

这里写图片描述

0 0
原创粉丝点击