在二叉搜索树中插入访问冲突错误 [英] Inserting in a binary search tree access violation error

查看:26
本文介绍了在二叉搜索树中插入访问冲突错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在二叉搜索树中插入节点,我在 searchNode() 函数的 if(ptr->data== item) 行遇到访问冲突错误.我怎样才能删除它.我是调试新手.

我首先尝试插入几个节点,然后使用显示功能显示它们.在插入过程中,程序会搜索要插入的节点的适当位置,然后将其插入.如果节点已经存在,程序简单地返回.

#include#include#include结构节点{整数数据;结构节点*左;结构节点*对;};void searchNode(struct node* root,int item,struct node* loc,struct node *par){结构节点 * ptr,*save;如果(根==空){loc=NULL;par=NULL;返回;}别的{如果(项目==根->数据){par=NULL;loc=root;返回;}否则如果(项目<根->数据){save=root;ptr=root->left;}否则如果(项目>根->数据){save=root;ptr=root->right;}而(ptr!=NULL){if(ptr->data == item ){loc=ptr;标准=保存;返回;}否则如果(ptr->数据>项目){保存=ptr;ptr=ptr->左;}别的{保存=ptr;ptr=ptr->对;}}loc=NULL;标准=保存;}}void insertNode(struct node* root,int item, struct node * loc){结构节点* par,*newNode;searchNode(root,item,loc,par);if(loc!=NULL)返回;newNode=(struct node *)malloc(sizeof(struct node));newNode->left=NULL;newNode->right=NULL;如果(标准= NULL){根=新节点;}else if(itemdata){par->left=newNode;}else if(item> par->data){par->right=newNode;}}无效显示(结构节点* t,整数级别){国际我;如果(t){display(t->right,level+1);printf("
");for(i=0;i数据);display(t->left,level+1);}}int main(){int n,data,i;结构节点*根,*loc;scanf("%d",&n);for(i=0;i

解决方案

你正在调用你的插入方法:

insertNode(root,data,loc);

并且 root 是按值传递的,因此在 insertNode 方法中对 root 所做的任何更改都不会在 main 中可见.由于您在 main 中的 root 是未初始化的,所以同样会被传递给 display,在那里您尝试取消引用导致未定义行为的未初始化指针.>

要解决此问题,您可以将 root 的地址传递给 insertNode 函数或从函数返回更改后的根.

I am trying to insert node in a binary search tree, I am getting an access voilation error at line if(ptr->data== item) in searchNode() function. How can I remove it. I am new to debugging.

I am first trying to insert few nodes and then display them using display function. During insertion the program searches for the appropriate position of the node to be inserted and then inserts it. Program simply returns if node already exists.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
       int data;
       struct node* left;
       struct node* right;
};
void searchNode(struct node* root,int item,struct node* loc,struct node *par)
{
              struct node * ptr,*save;
               if(root==NULL)
               {
                             loc=NULL;
                             par=NULL;
                             return;
               }
               else
               {
                   if(item==root->data)
                   {

                                  par=NULL;loc=root;  return;
                   }
                   else if(item < root->data)
                   {
                         save=root;ptr=root->left;
                   }
                   else if(item > root->data)
                   {
                        save=root;ptr=root->right;
                   }               

                   while(ptr!=NULL)
                   {
                           if(ptr->data == item )
                           {
                                              loc=ptr;
                                              par=save;
                                           return;
                           }
                          else if(ptr->data > item )
                          {
                               save=ptr;
                               ptr=ptr->left;        
                          }
                          else
                          {
                              save=ptr;ptr=ptr->right;
                          }
                   }
                   loc=NULL;
                   par=save;
               }
}
void insertNode(struct node* root,int item, struct node * loc)
{
     struct node* par,*newNode;
     searchNode(root,item,loc,par);
     if(loc!=NULL)
               return;
     newNode=(struct node *)malloc(sizeof(struct node));
     newNode->left=NULL;newNode->right=NULL;
     if(par==NULL)
     {
                  root=newNode;
     }
     else if(item< par->data)
     {
          par->left=newNode;
     }
     else if(item> par->data)
     {
          par->right=newNode;
     }
}
void display(struct node* t, int level)
{
     int i;
     if(t)
     {
          display(t->right,level+1);
          printf("
");
          for(i=0;i<level;i++)
          printf(" ");
          printf("%d",t->data);
          display(t->left,level+1);
     }
}
int main()
{
    int n,data,i;
    struct node* root,*loc;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
                    scanf("%d",&data);
                    insertNode(root,data,loc);
    }
    display(root,1);
    getch();
    return 0;
}

解决方案

You are calling your insert method as:

insertNode(root,data,loc);

and the root is passed by value as a result, any changes made to root in the insertNode method will not be visible in main. Since your root in main is uninitialized, the same gets passed to display where you try to dereference the uninitialized pointer leading to undefined behavior.

To fix this you either pass the address of root to insertNode function or return the changed root from the function.

这篇关于在二叉搜索树中插入访问冲突错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆