将节点插入二叉搜索树/链表?

时间:2012-03-11 11:52:41

标签: c++ insert linked-list binary-tree

好的,我对这个问题已经太累了,所以我想我会得到一些外界的帮助。该计划拥有人员“数据库”,其中包括员工和学生。每个学生都有一个“书籍”的二叉树,用户可以插入和搜索。我需要接受一个学生的名字,找到与该特定学生相对应的人员节点,并为该学生的bookTree添加一本书。

我收到的错误消息是 “Homework4.exe中0x013c53a0处的未处理异常:0xC0000005:访问冲突读取位置0xcccccd1c。” ,我假设这意味着我在某处弄乱指针。 callstack显示第512行(因此book_traverse())作为问题制定者。 这是我到目前为止(省略不必要的代码):提前致谢!

class PersonnelNode {       // This is a container class
private:
    Personnel       *pNode; // It contains a Personnel class
    PersonnelNode   *pNext; // pointer used to form a linked list
public:
    void setNode(Personnel *pNode) { this->pNode = pNode; }
    void setNext(PersonnelNode *pNext) { this->pNext = pNext; }
    Personnel* getNode() { return pNode; }
    PersonnelNode* getNext() { return pNext; }

    PersonnelNode() {       // constructor
        pNode = NULL;
        pNext = NULL;
    }
} *head = NULL; // declare a global pointer variable head

...

struct Book {
  char title[75];
  char url[75];
  char key;
  Book *left;
  Book *right;  

  Book(char *title, char *url) { // Constructor
    strcpy_s(this->title, title);
    strcpy_s(this->url, url);
    key = title[0];
    left = NULL;
    right = NULL;
  }
};

...

class Student : public Personnel { //inherit from Personnel
   ... (omitted the unnecessary code)
   Book *bookTree;


   //BookTree = NULL in constructor
}

...

int insert_book() {
   PersonnelNode *temp, *prev;
   Personnel *person;
   Student *student;
   Book *newBook;
   char title[75], url[75], sName[75];
   temp = head;

   cout << endl << "@Inserting book node.........." << endl;
   cout << "Enter the student name: ";
   cin.ignore();
   cin.getline(sName, 75);
       //*****My error is probably below here?
   while (temp != NULL) {
       person = temp->getNode();
       if (sName != person->getName()) {
           prev = temp;
           temp = temp->getNext();
       }
       else {
           student = (Student *) person;
       }
   }
   cout << "Enter the book title: ";
   cin.getline(title, 75);
   cout << "Enter the URL: ";
   cin.getline(url, 75);
   newBook = new Book(title, url);
   book_traverse(student->bookTree, newBook); //LINE 512
   return 0;
}

...

//***Recursive function to insert book
void book_traverse(Book* root, Book* newBook) { //Is this right?
   if (root == NULL)                           //I tried Book* &root, but then
    root = newBook;                        //the compiler doesn't like root==NULL
   else if (newBook->key < root->key)
    book_traverse(root->left, newBook);
   else
    book_traverse(root->right, newBook);
}

2 个答案:

答案 0 :(得分:2)

我认为你需要Book **

void book_traverse(Book** root, Book* newBook) 

然后在任何地方使用* root而不是root,例如

*root = newBook

否则,在book_traverse中,您将更改root的本地副本。

答案 1 :(得分:1)

声明并初始化必要的变量

  1. 列表项
  2. 读取要插入树中的数据项x。
  3. 创建一个新节点,其左右指针为null。
  4. 将数据x分配给新节点的信息字段。
  5. if(tree == NULL)    那么tree =新节点的地址 否则if(x&lt; tree - &gt; info)    if(tree-&gt; left == NULL)       然后tree-&gt; left =新节点    其他       tree = tree-&gt; left 重复步骤5。 否则if(x&gt; tree-&gt; info)    if(tree-&gt; right == NULL)       然后tree-&gt; right =新节点    其他       tree = tree-&gt; right  重复步骤5  否则if(x == tree-&gt; info)       打印“重复数据”并退出
  6. 下次插入时,请转到步骤5.
  7. REF:http://www.programmers-point.blogspot.in