二叉搜索树插入(c++)

insertion in binary search tree (C++)

本文关键字:c++ 插入 搜索树      更新时间:2023-10-16

我是数据结构新手。我在做一个c++程序插入一个元素在二叉搜索树。程序编译没有任何错误,但当我运行程序时,在输入第一个n后,程序停止工作。请帮助我使这个程序正常工作。

我的代码如下:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node{
    int data;
    struct node* left;
    struct node* right;
};
struct node *root=NULL ,*par=NULL ,*pos=NULL,*save=NULL , *ptr=NULL ;
struct node* newNode(int data)
{
    struct node* newnode= (struct node*)malloc(sizeof(struct node));
    newnode->data=data;
    newnode->right=NULL; 
    newnode->left=NULL;
    return newnode;
}
void findpos(int data)
{
    if(root==NULL)
    {
        par=NULL,pos=NULL;
        return;
    }
    if(root->data==data)
    {
        par=NULL , pos=root;
        return;
    }
    if(data<root->data)
    {
        save=root; ptr=root->left; 
    }
    else{
        save=root; ptr=root->right;
    }
    while(ptr!=NULL)
    {
        if(ptr->data==data)
        {
            par=save; pos=ptr;
            return;
        }
        if(data<ptr->data)
        {
            save=ptr;
            ptr=ptr->left;
        }
        else{
            save=ptr;
            ptr=ptr->right;
        }
    }
    pos=NULL; par=save;
    return;
}

void insert(int data)
{
    findpos(data);
    if(pos!=NULL)
    {
        return;
    }
    pos=newNode(data);
    if(data<par->data)
    par->left=pos;
    else
    par->right=pos;
    return;
}

  int main()
    {
    struct node *root=  newNode(4);
    root->left=newNode(3);
    root->left->left=newNode(2);
    root->right=newNode(6);
    int n;
    cin>>n;
    insert(n);
    cout<<pos->data;  //just trying to see if it works
        return 0;
    }

main()中声明的根指针覆盖全局根指针。然后在第一次findpos()时,根(全局)仍然为NULL。因此,只需替换该代码:

int main()
{
    struct node *root=  newNode(4);
    root->left=newNode(3);
    root->left->left=newNode(2);
    root->right=newNode(6);
    int n;
    cin>>n;
    insert(n);
    cout<<pos->data;  //just trying to see if it works
    return 0;
}

By this:

int main()
{
    root=  newNode(4);
    root->left=newNode(3);
    root->left->left=newNode(2);
    root->right=newNode(6);
    int n;
    cin>>n;
    insert(n);
    cout<<pos->data;  //just trying to see if it works
    return 0;
}

修改main方法,使其在全局命名空间中修改root;在main中声明一个名为root local的新节点之前。

int main() {
    //      struct node *root = newNode(4); // you made this root a member of main.
    // not a global member anymore. 
                    root = newNode(4); // use this
            // Populate some nodes
                    root->left=newNode(3);
                    root->left->left=newNode(2);
                    root->right=newNode(6);
                    /* tree so far:
                     *              4       
                     *             / 
                     *            3   6
                     *           /
                     *          2
                     */

                    int n;
                    cin >> n;
                    cout << "user input " << n << "n";
                    insert(n);
                    cout << pos->data;  //just trying to see if it works
                    return 0;
}