微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

【数据结构】二叉查找树


#include <iostream>
#include <queue>
#include <stack>
using namespace std;
#define MAX 10
struct BinTreeNode
{
    int m_data;
    BinTreeNode *lchild,*rchild;
    BinTreeNode(int item,BinTreeNode *left=NULL,BinTreeNode *right=NULL)
        :m_data(item),lchild(left),rchild(right) {}
};
class BinaryTree
{
    private:
        BinTreeNode *root;
    public:
        BinaryTree()
        {
            root=NULL;
        }
        BinTreeNode *GetRoot()
        {
            return root;
        }
        void Insert(const int node)
        {
            BinTreeNode *currentpointer;
            BinTreeNode *parentpointer;
            BinTreeNode *newpointer;
            BinTreeNode *ptr = root;
            newpointer = new BinTreeNode(node);
            if(root==NULL)
            {
                root=newpointer;
            }
            else
            {
                currentpointer=ptr;
                while(currentpointer!=NULL)
                {
                    parentpointer=currentpointer;
                    if(currentpointer->m_data>node)
                    {
                        currentpointer=currentpointer->lchild;
                    }
                    else
                    {
                        currentpointer=currentpointer->rchild;
                    }
                }
                if(parentpointer->m_data>node)
                {
                    parentpointer->lchild=newpointer;
                }
                else
                {
                    parentpointer->rchild=newpointer;
                }
            }
        }
        BinTreeNode *Creat(int data[],int len)
        {
            for(int i=0; i<len; i++)
            {
                Insert(data[i]);
            }
            return root;
        }
        void PreOrder(BinTreeNode *p)
        {
            if(p!=NULL)
            {
                cout<<p->m_data<<',';
                PreOrder(p->lchild);
                PreOrder(p->rchild);
            }
        }
        void PreOrder2(BinTreeNode *p)//非递归版
        {
            stack<BinTreeNode*>S;
            S.push(p);
            while(!S.empty())
            {
                p=S.top();
                while(p)
                {
                    cout<<p->m_data<<',';
                    S.push(p->lchild);
                    p=p->lchild;
                }
                S.pop();
                if(!S.empty())
                {
                    p=S.top();
                    S.pop();
                    S.push(p->rchild);
                }
            }
            
        }
        void Inorder(BinTreeNode *p)
        {
            if(p!=NULL)
            {
                Inorder(p->lchild);
                cout<<p->m_data<<',';
                Inorder(p->rchild);
            }
        }
        void Inorder2(BinTreeNode *p)//非递归版
        {
            stack<BinTreeNode*>S;
            S.push(p);
            while(!S.empty())
            {
                p=S.top();
                while(p)
                {
                    S.push(p->lchild);
                    p=p->lchild;
                }
                S.pop();
                if(!S.empty())
                {
                    p=S.top();
                    S.pop();
                    cout<<p->m_data<<',';
                    S.push(p->rchild);
                }
            }
            
        }
        void postorder(BinTreeNode *p)
        {
            if(p!=NULL)
            {
                postorder(p->lchild);
                postorder(p->rchild);
                cout<<p->m_data<<',';
            }
        }
        void layerOrder(BinTreeNode *p)
        {
            if(p!=NULL)
            {
                queue<BinTreeNode *>st;
                st.push(p);
                while(!st.empty())
                {
                    BinTreeNode*Now=st.front();
                        st.pop();
                    cout<<Now->m_data<<',';
                    if(Now->lchild!=NULL)
                    {
                        st.push(Now->lchild);
                    }
                    if(Now->rchild!=NULL)
                    {
                        st.push(Now->rchild);       
                    }   
                }
            }
        }
        BinTreeNode *Find(const int &c)
        {
            BinTreeNode *pCurrent =root;
            if(pCurrent!=NULL)
            {
                while(pCurrent!=NULL)
                {
                    if(pCurrent->m_data==c)
                    {
                        return pCurrent;
                    }
                    else
                    {
                        if(c > pCurrent->m_data)
                            pCurrent=pCurrent->rchild;
                        else
                            pCurrent=pCurrent->lchild;
                    }
                }
            }
            return NULL;
        }
        bool DeleteBT(const int &key)
        {
            BinTreeNode *q,*s;
            BinTreeNode *current=root;
            BinTreeNode *prt=NULL;
            while((NULL!=current)&&(current->m_data!=key))
            {
                prt=current;
                if(current->m_data>key)
                    current=current->lchild;
                else
                    current=current->rchild;
            }
            if(current==NULL)
            {
                cout<<"没有此节点"<<endl;
                return false;
            }
            //删除叶子节点
            if(current->lchild==NULL&¤t->rchild==NULL)
            {
                if(current==root)
                {
                    root=NULL;
                }
                else
                {
                    if(current==prt->lchild)
                    {
                        prt->lchild=NULL;
                    }
                    else
                    {
                        prt->rchild=NULL;
                    }
                }
            }
            //右边有人
            if(current->lchild==NULL&¤t->rchild!=NULL)
            {
                if(current==root)
                {
                    current=current->rchild;
                }
                else
                {
                    if(current==prt->lchild)
                    {
                        prt->lchild=current->rchild;
                    }
                    else
                    {
                        prt->rchild=current->rchild;
                    }
                }
            }
            //左边有人
            if(current->rchild==NULL&¤t->lchild!=NULL)
            {
                if(current==root)
                {
                    current=current->lchild;
                }
                else
                {
                    if(current==prt->lchild)
                    {
                        prt->lchild=current->lchild;
                    }
                    else
                    {
                        prt->rchild=current->lchild;
                    }
                }
            }
            //两边都有人
            if(current->lchild!=NULL&¤t->rchild!=NULL)
            {
                q=current;
                s=current;
                current=current->lchild;
                while(current->rchild)
                {
                    s=current;
                    current=current->rchild;
                }
                q->m_data=current->m_data;
                if(q!=s)
                    s->rchild=current->lchild;
                else
                    q->lchild=current->lchild;
            }
            delete current;
            current=NULL;
            return true;
        }
        void destroy(BinTreeNode *current)
        {
            if(current!=NULL)
            {
                destroy(current->lchild);
                destroy(current->rchild);
                delete current;
                current = NULL;
            }
        }
        ~BinaryTree()
        {
            destroy(root);
        }
        int CountLeafs(BinTreeNode *current)//叶子节点
        {
            if(current==NULL)
            {
                return 0;
            }
            else
            {
                if(current->rchild==NULL&¤t->rchild==NULL)
                {
                    return 1;
                }
                else
                {
                    int num1=CountLeafs(current->lchild);
                    int num2=CountLeafs(current->rchild);
                    return num1+num2;
                }
            }
        }

        int Depth(BinTreeNode *current)//深度
        {
            int Depthleft;
            int Depthright;
            int dp=0;
            if(current==NULL)
            {
                return 0;
            }
            else
            {
                Depthleft=Depth(current->lchild);
                Depthright=Depth(current->rchild);
                dp=1+(Depthright>=Depthleft ? Depthright:Depthleft);
            }
            return dp;
        }
        BinTreeNode *copy(BinTreeNode *tree)
        {
            BinTreeNode *pleft;
            BinTreeNode *pright;
            if(!tree)
            {
                return NULL;
            }
            if(tree->lchild!=NULL)
            {
                pleft=copy(tree->lchild);
            }
            else
            {
                pleft=NULL;
            }
            if(tree->rchild!=NULL)
            {
                pright=copy(tree->rchild);
            }
            else
            {
                pright=NULL;
            }
            BinTreeNode *cptree=new BinTreeNode(tree->m_data,pleft,pright);
            root=cptree;
            return cptree;
        }
};
int main()
{
    BinaryTree myTree;
    int a[]= {6,3,4,7,8,2,5,9,1};
    /*
         6
        / \
       3   7
      / \   \
     2   4   8
    /     \   \
   0       5   9
    \
     1
    */
    myTree.Creat(a,MAX);  //创建二叉搜索树
    cout<<"先序遍历";myTree.PreOrder(myTree.GetRoot());cout<<endl;
    cout<<"中序遍历";myTree.Inorder(myTree.GetRoot());cout<<endl;
    cout<<"后序遍历";myTree.postorder(myTree.GetRoot());cout<<endl;
    cout<<"层序遍历";myTree.layerOrder(myTree.GetRoot());cout<<endl;

    cout<<"非递归先序遍历";myTree.PreOrder2(myTree.GetRoot());cout<<endl;
    cout<<"非递归中序遍历";myTree.Inorder2(myTree.GetRoot());cout<<endl;

    cout<<"树深度";cout<<myTree.Depth(myTree.GetRoot())<<endl;
    cout<<"叶子节点数";cout<<myTree.CountLeafs(myTree.GetRoot())<<endl;
    BinaryTree copyTree;//新建复制树
    copyTree.copy(myTree.GetRoot());
    cout<<"复制树先序遍历";copyTree.PreOrder(copyTree.GetRoot());
    cout<<"复制树深度"<<copyTree.Depth(copyTree.GetRoot())<<endl;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐