寫二叉排序樹時遇到的問題

首先附上代碼



/*
目的:
實現二叉排序樹構造、插入、刪除、查找功能


數據存儲結構:
樹用二叉鏈表樹的方式存儲、原數據用數組存儲


*/


#include<iostream>
#define MaxSize 1000


using namespace std;


struct BiNode
{
    int data;
    struct BiNode *lchild,*rchild;
};


class BiSortTree
{
public:
    BiSortTree(int a[],int n);                             //構造一個存儲了n個數據的二叉排序樹
    ~BiSortTree();                                       //同二叉鏈表的析構函數
//  void insertBST(BiNode *root,BiNode *s);
    void insertBST(BiNode *&root,BiNode *s);
    void delBSTR(BiNode *p,BiNode *f);                     //刪除節點f的左孩子p
    BiNode *searchBST(int k);                               //以root爲根節點查找值爲k的節點




    void InOrderTravse()
    {
        InOrderTravse(root);
    }


private:
    void release(BiNode *bt);
//  BiNode *searchBST(BiNode *root,int k);
    BiNode *searchBST(BiNode *&root,int k);             //查找值爲k的節點
    void InOrderTravse(BiNode *&tRoot);
    BiNode *root;                                       //二叉排序表的根指針
};


//插入函數
void BiSortTree::insertBST(BiNode * &root,BiNode *s)
{
    if(root==NULL)
    {
        root=s;
    }
    else if((s->data)<(root->data))
    {
        insertBST(root->lchild,s);
    }
    else
    {
        insertBST(root->rchild,s);
    }
}


//刪除f的左子樹P函數
void BiSortTree::delBSTR(BiNode *p,BiNode *f)
{
    if(p->lchild==NULL&&p->rchild==NULL)        //p爲葉子節點的情況
    {
        f->lchild=NULL;
        delete p;
    }


    if(p->lchild!=NULL&&p->rchild==NULL)        //p只有左子樹的情況
    {
        f->lchild=p->lchild;
        delete p;
    }


    if(p->lchild==NULL&&p->rchild!=NULL)         //p只有右子樹的情況
    {
        f->lchild=p->rchild;
        delete p;
    }


    if(p->lchild!=NULL&&p->rchild!=NULL)        //p既有左子樹又有右子樹的情況
    {
        BiNode *s=p->rchild;                    //存儲p的右子樹最左下的節點
        BiNode *par=p;                          //存儲s的雙親節點
        while(s->lchild!=NULL)
        {
            par=s;
            s=s->lchild;
        }


        p->data=s->data;                        //p的數據用s的數據代替


        if(par==p)                  //p的右子樹無左子樹,s即爲p的右子樹的情況
            p->rchild=s->rchild;
        //par->rchild=s->rchild;也是可以的
        else
            par->lchild=s->rchild;
        delete s;
    }
}






//查找函數
BiNode *BiSortTree::searchBST(BiNode *&root,int k)
{


    if(root==NULL)
        return NULL;
    else if(root->data==k)
    {
        return root;
    }
//  else if(root->data<k)
    else if(root->data>k)
    {
        return searchBST(root->lchild,k);
    }
    else
    {
        return searchBST(root->rchild,k);
    }
}


BiNode *BiSortTree::searchBST(int k)
{
    return searchBST(root,k);
}




void BiSortTree::release(BiNode *bt)
{
    if(bt!=NULL)
    {
        release(bt->lchild);
        release(bt->rchild);
        delete bt;
    }
}


void BiSortTree::InOrderTravse(BiNode *&tRoot)
{
    if (tRoot!= NULL)
    {
        InOrderTravse(tRoot->lchild);
        cout << tRoot->data << " ";
        InOrderTravse(tRoot->rchild);
    }
}


//構造函數
BiSortTree::BiSortTree(int a[],int n)
{
    int i;
    root=NULL;
    for(i=0; i<n; i++)
    {
        BiNode* s=new BiNode;
        s->data=a[i];
        s->rchild=s->lchild=NULL;
        insertBST(root,s);
    }
}




//析構函數
BiSortTree::~BiSortTree()
{
    release(root);
}


/**bug出現在最後root=NULL*/
int main()
{
    int n,i,r;
    BiNode * s=new BiNode;
    int a[MaxSize];
    cout<<"請輸入數據個數n、n個數據和要查找的數據"<<endl;
    cin>>n;
    for(i=0; i<n; i++)
    {
        cin>>a[i];
    }


    cin>>r;
    BiSortTree B(a,n);


    s=B.searchBST(r);


    if(s)
    {
        cout<<s->data<<"查找成功"<<endl;
    }
    else
    {
        cout<<s->data<<"查找失敗"<<endl;
    }


    return 0;


}


/*
測試數據
輸入:
6
54 42 34 66 78 15
42
輸出:
42查找成功
*/

兩點錯誤:

1.在searchBST函數具體實現過程中,在比較大小後是用rchild還是lchild沒有分清楚

2.該程序中searchBST和insertBST函數參數做了修改


改正:

1.right是右,left是左,莫要左右不分

2.大致是BiNode*是代表指針的含義,而加上‘&’(引用)後函數內外才能共用一個地址,達到內變外變的效果。

出現該錯誤由指針、引用部分概念模糊導致


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章