2012-08-26 51 views
0

我正在嘗試編寫簡單的二叉搜索樹類(BST)。損壞的指針 - BST - 調試

當我添加一個節點(eg.value = 10)時,根得到更新,並使用VS C++調試器在BST :: insert(...)的末尾進行驗證。

然後我試圖顯示(Case語句3)節點(值-10),並沒有打印。原因是,當調用getRoot()時,根是NULL(?? !!!)

有人可以幫我調試這個問題。

#include <iostream> 
#include <stdio.h> 
#include "bst_node.h" 

class BST{ 
private: 
    bst_node *root; 

public: 
    bst_node* getRoot(); 
    void insert(bst_node *, int val); 
    void deleteValue(int val); 
    void display(bst_node *); 
    void destroy(); 

    BST() 
    { 
     root = NULL; 
     std::cout<<"BST constructor"<<"\n"; 
    } 

    ~BST() 
    { 
     std::cout<<"BST destructor"<<"\n"; 
    } 
}; 

int main() 
{ 
    int item; 
    int choice; 
    BST tree1; 
    while(1) 
    { 
     printf("\n Choices are:\n"); 
     printf("\n 1.Insertbeg\n\n 2.deleteNode\n\n 3.display\n\n 4.exit\n\n"); 
     printf(" Enter U'r choice: "); 
     scanf("%d",&choice); 
     switch(choice) 
     { 
      case 1: 
       { 
        printf("Enter element to be inserted: "); 
        scanf("%d",&item); 
        tree1.insert(tree1.getRoot(), item); 
        break; 
       } 
      case 2: 
       { 
        printf("Enter element to be deleted: "); 
        scanf("%d",&item); 
//     tree1.deleteValue(item); 
        break; 
       } 
      case 3: 
       { 
        tree1.display(tree1.getRoot()); 
        break; 
       } 
      case 4: 
        exit(1); 
        break; 
      default: printf("INVALID CHOICE TRY AGAIN\n"); 
     } 
    } 
} 

void BST::insert(bst_node* root, int val) 
{ 
    if(root == NULL) 
    { 
     // add first element 
     bst_node *new_node = bst_node::create_empty_node(); 
     new_node->val = val; 
     root = new_node; 
    } 

    else if(val < root->val) 
    { 
     insert(root->l_child, val); 
    } 

    else if (val >= root->val) 
    { 
     insert(root->r_child, val); 
    } 
} 

void BST::display(bst_node *root) 
{ 
    if(root == NULL) 
    { 
     return; 
    } 
    else 
    { 
     std::cout << root->val <<"\n"; 
     display(root->l_child); 
     display(root->r_child); 
    } 
} 

void BST::deleteValue(int val) 
{ 
    std::cout<< " Do nothing"; 
} 

bst_node* BST::getRoot() 
{ 
    return root; 
} 

bst_node.h


class bst_node 
{ 
public: 
    int val; 
    bst_node *l_child; 
    bst_node *r_child; 
    static bst_node* create_empty_node(); 
}; 

bst_node* bst_node::create_empty_node() 
{ 
    bst_node *new_node; 
    new_node = new bst_node; 
    new_node -> l_child = new_node -> r_child = NULL; 
    return(new_node); 
} 
+0

'create_empty_node'方法在哪裏? –

+4

您通過將參數命名爲insert()''root'來隱藏私有成員'root'。所有對「root」的修改都將應用於本地變量。 – amit

回答

2

您是隱藏私有成員root通過命名參數insert()root爲好。所有對root的修改都將應用於局部變量。例如行:

root = new_node; 

將不會有任何影響。您正在爲一個永不再使用的易失性設置一個新值。

這樣做通常是一個不好的做法(有一些例外,如setter)。嘗試重命名並檢查它是否至少解決了一些問題。

+1

謝謝。重做,它的工作! –

0

變化void BST::insert(bst_node* root, int val)void BST::insert(bst_node*& root, int val)void BST::insert(bst_node** root, int val)

的easyest沒有其他修改的功能是使用bst_node * &

指針本身就是傳遞給函數的整數,這個指針然後被複制,所以改變指針的值不會在函數的範圍之外被改變。對指針的引用將使您的變化在函數範圍外變得可用。