1. 程式人生 > >寫二叉排序樹時遇到的問題

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

首先附上程式碼

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


資料儲存結構:
樹用二叉連結串列樹的方式儲存、原資料用陣列儲存


*/


#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*是代表指標的含義,而加上‘&’(引用)後函式內外才能共用一個地址,達到內變外變的效果。

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