我有一些学校的家庭作业,我应该实施BST来解决两个问题。 所以我开始了,我只需要擦除,插入和搜索功能。 但每次我运行我的代码我都会出现分段错误。谁能帮我?? 这是我的代码:
#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
#define MP make_pair
#define pb push_back
#define X first
#define Y second
using namespace std;
typedef unsigned long long int ulli;
ulli n;
using namespace std;
struct BST{ // Binary Search Tree
struct node{ // Node
node *left; // Left Child
node *par; // Parent node
node *right; // Right child
int val; // the value
node(){
left = par = right = NULL;
val = 0;
}
};
node *root; // Root of the tree
BST(){
root = NULL;
}
bool insert( int value ){
node *tmp = new node;
tmp->val = value;
if ( root == NULL ){
root = tmp;
return 0;
}
node *cur = root;
while ( tmp->par != cur ){
if ( tmp->val < cur->val ){
if ( cur->left == NULL ){
tmp->par = cur;
cur->left = tmp;
break;
}
cur = cur->left;
}else{
if ( cur->right == NULL ){
tmp->par = cur;
cur->right = tmp;
break;
}
cur = cur->right;
}
}
return 0;
}
node *search( int val ){
node *tmp = root;
while( tmp->val != val ){
if ( val < tmp->val )
tmp = tmp->left;
else
tmp = tmp->right;
if ( tmp == NULL ){
return NULL;
}
}
return tmp;
}
bool erase( int val ){
node *tmp = search( val ), *k;
k = tmp;
if ( tmp == root ){
if ( tmp->left == NULL && tmp->right == NULL ){
root = NULL;
delete tmp;
}
if ( tmp->left != NULL && tmp->right == NULL ){
root = root->left;
delete tmp;
return 0;
}
if ( tmp->left == NULL && tmp->right != NULL ){
root = root->right;
delete tmp;
return 0;
}
tmp = tmp->right;
while( tmp->left != NULL )
tmp = tmp->left;
if ( tmp->right != NULL )
tmp->par->left = tmp->right;
else
tmp->par->left = NULL;
tmp->par = NULL;
tmp->right = k->right;
tmp->left = k->left;
root = tmp;
return 0;
}
if ( tmp->left == NULL && tmp->right == NULL ){
if ( tmp->par->left == tmp )
tmp->par->left = NULL;
else
tmp->par->right = NULL;
delete tmp;
}
if ( tmp->left != NULL && tmp->right == NULL ){
tmp->left->par = tmp->par;
if ( tmp->par->left == tmp )
tmp->par->left = tmp->left;
else
tmp->par->right = tmp->left;
delete tmp;
return 0;
}
if ( tmp->left == NULL && tmp->right != NULL ){
tmp->right->par = tmp->par;
if ( tmp->par->left == tmp )
tmp->par->left = tmp->right;
else
tmp->par->right = tmp->right;
delete tmp;
return 0;
}
tmp = tmp->right;
while( tmp->left != NULL )
tmp = tmp->left;
if ( tmp->right != NULL )
tmp->par->left = tmp->right;
else
tmp->par->left = NULL;
tmp->par = k->par;
if ( k->par->right == k )
k->par->right = tmp;
else
k->par->left = tmp;
tmp->right = k->right;
tmp->left = k->left;
}
};
BST b;
vector<int> v;
int main () {
cin >> n;
int k;
for ( int i = 0; i < n; i++ ){
cin >> k;
v.pb( k );
b.insert( k );
}
for ( int i = 0; i < n; i++ )
b.erase( v[i] );
return 0;
}
答案 0 :(得分:2)
你想要取消引用悬空指针,我想是因为你错过了return 0;
声明
bool erase( int val ){
node *tmp = search( val ), *k;
k = tmp;
if ( tmp == root ){
if ( tmp->left == NULL && tmp->right == NULL ){
root = NULL;
delete tmp; // tmp is deleted, should no longer be used afterwards
// return 0; // Missing return 0
}
if ( tmp->left != NULL && tmp->right == NULL ){
...
}
if ( tmp->left == NULL && tmp->right != NULL ){
...
}
tmp = tmp->right; // tmp is dereferenced
while( tmp->left != NULL )
tmp = tmp->left;
另外:如果search(val)
返回NULL
,您没有错误检查,尽管这是可能的:
node *search( int val ){
node *tmp = root;
while( tmp->val != val ){
if ( val < tmp->val )
tmp = tmp->left;
else
tmp = tmp->right;
if ( tmp == NULL ){
return NULL; <---
}
}
return tmp;
}