二叉树的中序、先序、后序、层序遍历 & 二叉树的深度 & 节点查找

来源:互联网 发布:windows 域控 编辑:程序博客网 时间:2024/05/22 17:49
#pragmaonce
#include<assert.h>
#include<queue>
#include<stack>

template<classT>
structBinaryTreeNode
{
                BinaryTreeNode<T>* _left;
                BinaryTreeNode<T>* _right;
                T_data;

                BinaryTreeNode(constT&x)
                                :_left(NULL)
                                ,_right(NULL)
                                ,_data(x)
                {}
};

template<classT>
classBinaryTree
{
                typedefBinaryTreeNode<T>Node;
public:
                BinaryTree()
                                :_root(NULL)
                {}

                BinaryTree(constT*a,size_tsize,constT&invalid)
                {
                                size_tindex = 0;
                                _root = _CreateTree(a,size,invalid, index);
                }

                BinaryTree(constBinaryTree<T>&t)
                {
                                _root = _Copy(t._root);
                }

                // t1 = t2 ==> t1.operator=(&t1, t2)
                // 传统写法
                //BinaryTree<T>& operator=(const BinaryTree<T>& t)
                //{
                //             if (this != &t)
                //             {
                //                             this->_Destory(this->_root);
                //                             this->_root = _Copy(t._root);
                //             }

                //             return *this;
                //}

                // t1 = t2 ==> t1.operator=(&t1, t2)
                // 现代写法 == 强盗式逻辑
                // t1 = t1
                /*BinaryTree<T>& operator=(BinaryTree<T> t)
                {
                                swap(this->_root, t._root);
                                return *this;
                }*/

                // t1 = t2
                BinaryTree<T>& operator=(constBinaryTree<T>&t)
                {
                                if(this!= &t)
                                {
                                                BinaryTree<T> tmp(t);
                                                swap(this->_root, tmp._root);
                                }

                                return*this;
                }

                ~BinaryTree()
                {
                                _Destory(_root);
                                _root =NULL;
                }

                voidPrevOrder()
                {
                                _PrevOrder(_root);
                                cout<<endl;
                }

                voidInOrder()
                {
                                _InOrder(_root);
                                cout<<endl;
                }

                voidPostOrder()
                {
                                _PostOrder(_root);
                                cout<<endl;
                }

                /*size_t Size()
                {
                                return _Size(_root);
                }*/

                size_tSize()
                {
                                size_tsize = 0;
                                _Size(_root, size);

                                returnsize;
                }

                size_tDepth()
                {
                                return_Depth(_root);
                }

                // 换一种思路实现
                size_tLeafSize()
                {
                                return_LeafSize(_root);
                }

                size_tGetKLevel(size_tk)
                {
                                assert(k > 0);
                                return_GetKLevel(_root,k);
                }

                Node* Find(constT&x)
                {
                                return_Find(_root,x);
                }

                voidLevelOrder()
                {
                                if(_root ==NULL)
                                                return;

                                queue<Node*> q;
                                q.push(_root);

                                while(!q.empty())
                                {
                                                Node* front = q.front();
                                                q.pop();
                                                cout<<front->_data<<" ";

                                                if(front->_left)
                                                                q.push(front->_left);

                                                if(front->_right)
                                                                q.push(front->_right);
                                }

                                cout<<endl;
                }

                voidPrevOrderNonR()
                {
                                stack<Node*> s;
                                if(_root)
                                                s.push(_root);

                                while(!s.empty())
                                {
                                                Node* top = s.top();
                                                cout<<top->_data<<" ";
                                                s.pop();

                                                if(top->_right)
                                                                s.push(top->_right);

                                                if(top->_left)
                                                                s.push(top->_left);
                                }

                                cout<<endl;
                }

                voidInOrderNonR()
                {
                                stack<Node*> s;
                                Node* cur = _root;

                                while(cur || !s.empty())
                                {
                                                while(cur)
                                                {
                                                                s.push(cur);
                                                                cur = cur->_left;
                                                }

                                                Node* top = s.top();
                                                cout<<top->_data<<" ";
                                                s.pop();

                                                cur = top->_right;
                                }

                                cout<<endl;
                }

                voidPostOrderNonR()
                {
                                stack<Node*> s;
                                Node* prev =NULL;
                                Node* cur = _root;

                                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<<" ";
                                                                s.pop();
                                                                prev = top;
                                                }
                                                else
                                                {
                                                                cur = top->_right;
                                                }
                                }

                                cout<<endl;
                }

protected:
                Node* _Copy(Node*root)
                {
                                if(root==NULL)
                                                returnNULL;

                                Node* newRoot =newNode(root->_data);
                                newRoot->_left = _Copy(root->_left);
                                newRoot->_right = _Copy(root->_right);

                                returnnewRoot;
                }

                void_Destory(Node*root)
                {
                                if(root==NULL)
                                                return;

                                _Destory(root->_left);
                                _Destory(root->_right);

                                deleteroot;
                }

              
             Node* _CreateTree(constT*a,size_tsize,
                                constT&invalid,size_t&index)
                {
                                Node* root =NULL;
                                if(index<size&& a[index] !=invalid)
                                {
                                                root =newNode(a[index]);
                                                root->_left = _CreateTree(a,size,invalid, ++index);
                                                root->_right = _CreateTree(a,size,invalid, ++index);
                                }
                                returnroot;
                }

                void_PrevOrder(Node*root)
                {
                                if(root==NULL)
                                                return;

                                cout<<root->_data<<" " ;
                                _PrevOrder(root->_left);
                                _PrevOrder(root->_right);
                }

                void_InOrder(Node*root)
                {
                                if(root==NULL)
                                                return;

                                _InOrder(root->_left);
                                cout<<root->_data<<" " ;
                                _InOrder(root->_right);
                }

                void_PostOrder(Node*root)
                {
                                if(root==NULL)
                                                return;

                                _PostOrder(root->_left);
                                _PostOrder(root->_right);
                                cout<<root->_data<<" " ;
                }

                /*size_t _Size(Node* root)
                {
                                if (root == NULL)
                                                return 0;

                                return _Size(root->_left)+ _Size(root->_right)+1;
                }*/

                /*size_t _Size(Node* root)
                {
                                static size_t sSize = 0;
                                if (root == NULL)
                                                return sSize;

                                ++sSize;

                                _Size(root->_left);
                                _Size(root->_right);

                                return sSize;
                }*/

                void_Size(Node*root,size_t&size)
                {
                                staticsize_tsSize = 0;
                                if(root==NULL)
                                                return;

                                ++size;

                                _Size(root->_left,size);
                                _Size(root->_right,size);
                }

                size_t_Depth (Node*root)
                {
                                if(root==NULL)
                                {
                                                return0;
                                }

                                intleft = _Depth(root->_left);
                                intright = _Depth(root->_right);

                                returnleft > right ? left+1 : right+1;
                }

                size_t_LeafSize(Node*root)
                {
                                if(root==NULL)
                                {
                                                return0;
                                }

                                if(root->_left ==NULL&&root->_right ==NULL)
                                {
                                                return1;
                                }

                                return_LeafSize(root->_left) + _LeafSize(root->_right);
                }

                size_t_GetKLevel(Node*root,size_tk)
                {
                                if(root==NULL)
                                                return0;

                                if(k== 1)
                                                return1;

                                return_GetKLevel(root->_left,k-1) + _GetKLevel(root->_right,k-1);
                }

                Node* _Find(Node*root,constT&x)
                {
                                if(root==NULL)
                                                returnNULL;

                                if(root->_data ==x)
                                                returnroot;

                                Node* ret = _Find(root->_left,x);
                                if(ret)
                                                returnret;

                                return_Find(root->_right,x);
                }

protected:
                Node* _root;
};

voidTestBinaryTree()
{
                inta1[10] = {1, 2, 3,'#','#', 4,'#','#', 5, 6};

                BinaryTree<int> t(a1,sizeof(a1)/sizeof(a1[0]),'#');
                t.PrevOrder();
                t.InOrder();
                t.PostOrder();

                cout<<"Tree Size?"<<t.Size()<<endl;
                cout<<"3 Level?"<<t.GetKLevel(3)<<endl;
                BinaryTreeNode<int>* ret = t.Find(6);
                cout<<"Find 6 ?:"<<ret->_data<<endl;

                t.LevelOrder();
                t.PrevOrderNonR();
                t.InOrderNonR();
                t.PostOrderNonR();
}
0 0