2012-04-27 46 views
0

我正在處理一個與二叉樹一起工作的程序。添加新節點到樹中時出錯。我可以添加一個節點,但添加另一個節點後,我得到一個STATUS_ACCESS_VIOLATION錯誤。我認爲這個錯誤可能與處理搜索功能的函數參數有關。如果可以,請你幫助我。C++二進制搜索樹狀態訪問違規錯誤添加節點

這裏是他的.h我寫文件:

#ifndef P4_H 
#define P4_H 
#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <cctype> 
#include <string> 
using namespace std; 

struct TreeNode //ListNode structure with components 
{ 
    int acctNum; 
    TreeNode *left; 
    TreeNode *right; 
}; 

typedef TreeNode* nodePtr; //defines a pointer to a treenode struct 

class Tree 
{ 
    private: 
      nodePtr root; 
      int numElements; 

    public: 

    Tree() 
    { root = NULL; numElements = 0; } 

    bool treeEmpty() 
    { return (numElements == 0); } 

    void addNode() 
    { 
     int key; 
     cout << "Enter account number to add: "; 
     cin >> key; 
     cout << endl << endl; 

     nodePtr location = NULL, parent = NULL, p = NULL; 

     bool found = searchTree(key, &location, &parent); 

     cout << found << " " << location << " " << parent << endl << endl; 

     if (found) 
     { 
     cout << "Error! Account number: " << key << " already exists within" 
     << " the tree." << endl << endl; 
     } 
     else 
     { 
     if (parent == NULL) 
     { 
      root = new TreeNode; 
      root->acctNum = key; 
     } 
     else 
     { 
      if (parent->acctNum > key) 
      { 
       parent->left = new TreeNode; 
       p = parent->left; 
       p->acctNum = key; 
      } 
      else 
      { 
       parent->right = new TreeNode; 
       p = parent->right; 
       p->acctNum = key; 
      } 
     } 
     } 
    } 

    bool searchTree(int key, nodePtr *location, nodePtr *parent) 
    { 
     bool found = false; 
     nodePtr p = root; 
     *location = root; 
     parent = NULL; 

     while (p != NULL && !found) 
     { 
     *location = p; 
     if (key == p->acctNum) 
      found = true; 
     else if (key < p->acctNum) 
     { 
      *parent = p; 
      p = p->left; 
     } 
     else if (key > p->acctNum) 
     { 
      *parent = p; 
      p = p->right; 
     } 
     } 
     return found; 
    } 

    void deleteNode() 
    { 
     int key; 
     nodePtr location = NULL, parent = NULL; 

     cout << "Enter account number to delete: "; 
     cin >> key; 
     cout << endl << endl; 

     bool found = searchTree(key, &location, &parent); 

     if (!found) 
     { 
     cout << "Error! Account number: " << key << " was not found." 
     << endl << endl; 
     } 
     else if (location->left != NULL && location->right != NULL) 
     { //delete node with left and right subtrees 
     nodePtr leftmost = location->right, leftmostParent = NULL; 

     while (leftmost->left != NULL) 
     { 
      leftmostParent = leftmost; 
      leftmost = leftmost->left; 
     } 

     leftmost->left = location->left; 
     leftmost->right = location->right; 
     leftmostParent->left = NULL; 

     if (parent->acctNum > location->acctNum) 
      parent->left = leftmost; 
     else 
      parent->right = leftmost; 

     delete location; 
     location = NULL; 
     } 
     else if (location->left != NULL && location->right == NULL) 
     { //delete node with only a left subtree 
     if (parent->acctNum > location->acctNum) 
      parent->left = location->left; 
     else 
      parent->right = location->left; 

     delete location; 
     location = NULL; 
     } 
     else if (location->left == NULL && location->right != NULL) 
     { //delete node with only a right subtree 
     nodePtr leftmost = location->right, leftmostParent = NULL; 

     while (leftmost->left != NULL) 
     { 
      leftmostParent = leftmost; 
      leftmost = leftmost->left; 
     } 

     leftmost->right = location->right; 
     leftmostParent->left = NULL; 

     if (parent->acctNum > location->acctNum) 
      parent->left = leftmost; 
     else 
      parent->right = leftmost; 

     delete location; 
     location = NULL; 
     } 
     else 
     { //delete a leaf node 
     if (location->acctNum > parent->acctNum) 
      parent->right = NULL; 
     else 
      parent->left = NULL; 

     delete location; 
     location = NULL; 
     } 
    } 

    void displayAscend(nodePtr p, int count) 
    { 
     if (p->left != NULL) 
     displayAscend(p->left, count); 
     cout << count << ". " << p->acctNum << endl; 
     count ++; 
     if (p->right != NULL) 
     displayAscend(p->right, count); 
    } 

    void displayDescend(nodePtr p, int count) 
    { 
     if (p->right != NULL) 
     displayAscend(p->right, count); 
     cout << count << ". " << p->acctNum << endl; 
     count ++; 
     if (p->left != NULL) 
     displayAscend(p->left, count); 
    } 

    void readFile() 
    { 
     char filename[50]; 

     cout << "Enter name of file to open: "; 
     cin.getline(filename,51); 
     cout << endl << endl; 

     ifstream inFile; 
     inFile.open(filename); 

     while (!inFile) 
     { 
     cout << "Error opening file! Please try again." << endl << endl; 
     cout << "Enter name of file: "; 
     cin.getline(filename,51); 
     cout << endl << endl; 
     } 

     int num; 

     while (!inFile.eof()) 
     { 
     inFile >> num; 

     nodePtr location = NULL, parent = NULL, p = NULL; 

     bool found = searchTree(num, &location, &parent); 

     if (found) 
     { 
      cout << "Error! Account number: " << num << " already exists" 
      << " within the tree." << endl << endl; 
     } 
     else 
     { 
      if (parent == NULL) 
      { 
       root = new TreeNode; 
       root->acctNum = num; 
      } 
      else 
      { 
       if (parent->acctNum > num) 
       { 
        parent->left = new TreeNode; 
        p = parent->left; 
        p->acctNum = num; 
       } 
       else 
       { 
        parent->right = new TreeNode; 
        p = parent->right; 
        p->acctNum = num; 
       } 
      } 
     } 
     } 



    } 
}; 
#endif 
+0

您在哪條線上發生訪問違規? – JaredPar 2012-04-27 18:06:14

+1

[C++ Binary Search Tree error with adding nodes]可能重複(http://stackoverflow.com/questions/10355294/c-binary-search-tree-error-with-adding-nodes)你應該真的編輯添加的信息轉化爲原始問題,而不是發佈另一個幾乎完全相同的問題。 – 2012-04-27 18:06:22

回答

2

在searchTree,你設置父爲NULL循環之前,則取消對它的引用的循環。

+0

謝謝你的工作! – 2012-04-27 18:14:58

+0

但現在我得到一個錯誤,顯示樹內的節點 – 2012-04-27 18:46:16

+0

提供一些細節,也許有人可以幫助你。 – 2012-04-27 18:51:49