二叉树的基本操作方式

二叉树的一些基本操作都是通过递归来实现的
在用递归解决二叉树的问题时要善于二叉树划分为根节点左子树和右子树。
这里写图片描述

二叉树递归创建的具体函数调用过程

这里写图片描述

二叉树的递归实现的基本操作
#include<iostream>
#include<stdlib.h>
#include<queue>
using namespace std;
template<class T>
struct BinaryTreeNode
{
        BinaryTreeNode( const T &x)
        :Lchild( NULL)
        , Rchild( NULL)
        , value( x)
        {

        }
         BinaryTreeNode<T >* Lchild;
         BinaryTreeNode<T >* Rchild;
         T value;
};
template<class T>
class BinaryTree
{
public:
         typedef BinaryTreeNode <T> Node;
        BinaryTree()
               :_Root( NULL)
        {

        } //一颗空树
        BinaryTree( T*a , int size, int invalid )
        {
                int index = 0;
               _root=_CreatBinaryTree( a,size ,invalid,index);
        }
        BinaryTree( const BinaryTree &t)
        {
               _root=_Copy( t._root);
        }
         BinaryTree& operator=(const BinaryTree& t)
        {
                return _assign(t ._root);
        }
        ~BinaryTree()
        {
               _Delete(_root);
        }
         void PrevOrder()
        {
               _PrevOrder(_root);
               cout << endl;
        }
         void InOrder()
        {
               _InOrder(_root);
               cout << endl;
        }
         void PostOrder()
        {
               _PostOrder(_root);
               cout << endl;
        }
         void LevelOrder()
        {
               _LevelOrder(_root);
        }
         size_t Size()
        {
                return _Size(_root);
        }
         size_t _Size(Node *root)
        {
                if (root == NULL)
               {
                        return 0;
               }
                else
               {
                        return _Size(root ->Lchild) + _Size(root->Rchild) + 1;
               }
        }
         size_t LeafSize()
        {
                return _LeafSize(_root);
        }
         size_t  _LeafSize(Node *root)
        {
                if (root == NULL)
               {
                        return 0;
               }
                if (root ->Lchild == NULL&& root->Rchild == NULL)
               {
                        return 1;
               }
                else
               {
                        return _LeafSize(root ->Lchild) + _LeafSize(root->Rchild);
               }

        }
         size_t GetKLevel(size_t k)
        {
                return _GetKLevel(_root, k );
        }
         size_t Depth()
        {
                return _Depth(_root);
        }
         Node* Find(const T& x)
        {
                return _Find(_root, x );
        }
protected:
         Node* _Copy(Node *root) //拷贝构造
        {
                Node* tmp = NULL ;
                if (root != NULL)
               {
                       tmp = new Node (root->value);
                       tmp->Lchild = _Copy( root->Lchild);
                       tmp->Rchild = _Copy( root->Rchild);
               }
                return tmp;
        }

         BinaryTree&_assign(Node *root) //赋值运算符
        {
                if (_root != NULL )
               {
                       _Delete(_root);
               }
                Node* tmp = NULL ;
                if (root != NULL)
               {
                       tmp = new Node (root->value);
                       tmp->Lchild = _Copy( root->Lchild);
                       tmp->Rchild = _Copy( root->Rchild);
               }
               _root = tmp;
                return *this ;
        }
         Node* _CreatBinaryTree(const T*a, int size, int invalid, int & index)
        {
                Node* root = NULL ;
                if (index < size&& a[index ] != invalid)
               {
                       root = new Node (a[index]);
                       root->Lchild = _CreatBinaryTree( a, size , invalid, ++index );
                       root->Rchild = _CreatBinaryTree( a, size , invalid, ++index );
               }
                return root;
        }
         void _Delete(Node *root)
        {
                if (root != NULL)
               {
                       _Delete( root->Lchild);
                       _Delete( root->Rchild);
                       delete root ;
                       root = NULL ;
               }

        }
         void _PrevOrder(Node *root)
        {
                if (root != NULL)
               {
                       cout << root->value << " " ;
                       _PrevOrder( root->Lchild);
                       _PrevOrder( root->Rchild);
               }
        }
         void _InOrder(Node *root)
        {
                if (root != NULL)
               {
                       _InOrder( root->Lchild);
                       cout << root->value << " " ;
                       _InOrder( root->Rchild);
               }
        }
         void _PostOrder(Node *root)
        {
                if (root != NULL)
               {
                       _PostOrder( root->Lchild);
                       _PostOrder( root->Rchild);
                       cout << root->value << " " ;
               }
        }
         void _LevelOrder(Node *root)
        {
                queue<Node *>q;
                if (root )
               {
                       q.push( root);
               }
                while (!q.empty())
               {
                        Node*tmp = q.front();
                       q.pop();
                       cout << tmp->value << " ";
                        if (tmp->Lchild)
                       {
                              q.push(tmp->Lchild);
                       }
                        if (tmp->Rchild)
                       {
                              q.push(tmp->Rchild);
                       }
               }
        } //利用了队列先进先出的特点进行了广度优先遍历
         size_t _GetKLevel(Node *root, size_t k )
        {
                if (root == NULL)
               {
                        return 0;
               }
                if (k == 1)
               {
                        return 1;
               }
                return _GetKLevel(root ->Lchild, k - 1) + _GetKLevel(root->Rchild, k - 1);
        }
         size_t _Depth(Node *root) //思想比较左右字数的大小,左子树比右子树大则返回左子树的大小+1
        {
                if (root == NULL)
               {
                        return 0;
               }
                size_t left = _Depth(root ->Lchild);
                size_t right = _Depth(root ->Rchild);
                return left > right ? left + 1 : right + 1;
        }
         Node* _Find(Node *root, const T &x)
        {
                if (root == NULL)
               {
                        return NULL ;
               }
                if (root ->value == x)
               {
                        return root ;
               }
                Node*ret = _Find(root ->Lchild, x);
                if (ret != NULL )
               {
                        return ret;
               }
                return _Find(root ->Rchild, x);
        }
protected:
         Node* _root;
};
void FunTest()
{
         int array1[10] = { 1, 2, 3, '#' , '#', 4, '#', '#' , 5, 6 };
         int array2[10] = { 7, 8, 9, '#' , '#', 10, '#', '#' , 11, 12 };
         BinaryTree<int > t1(array1, sizeof(array1) / sizeof(array1[0]), '#' );
         BinaryTree<int > t2(array2, sizeof(array2) / sizeof(array2[0]), '#' );
        t1.PrevOrder();
        t2.PrevOrder();
        t2 = t1;
        t2.PrevOrder();
        t2.LevelOrder();
        cout << t2.Size()<<endl;
        cout << t2.LeafSize() << endl;
        cout << t2.Find(4)<<endl;
        cout << "Depth:" << t2.Depth()<< endl;
        cout << "GetKLevel:" << t2.GetKLevel(2)<<endl;
}
int main()
{
        FunTest();
        system( "pause");
         return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值