在这里我已经制作了一个删除二进制搜索树(BST)的程序,但是在执行时得到了分段错误(核心转储),我认为它在删除函数中但不知道我是否删除了删除函数的函数调用然后它的工作原理很好,就像找到最大元素,inorder遍历,但删除将无法正常工作.
#include<stdio.h>
#include<stdlib.h>
struct BST{
int data;
struct BST *left;
struct BST *right;
};
struct BST *newNode(int data){
struct BST *temp = (struct BST *)malloc(sizeof(struct BST));
temp->data=data;
temp->left=0;
temp->right=0;
return temp;
}
void inOrder(struct BST *root){
if(root==0)
return;
inOrder(root->left);
printf("%d",root->data);
inOrder(root->right);
}
struct BST *findMax(struct BST *root){
if(root==0)
return 0;
if(root->right!=0)
return findMax(root->right);
return root;
}
struct BST *dele(struct BST *root,int data){
if(root==0)
return 0;
if(data<root->data)
root->left=dele(root->left,data);
if(data>root->data)
root->right=dele(root->right,data);
else{
if(root->left && root->right)
{
struct BST *temp=findMax(root->left);
root->data=temp->data;
root->left=dele(root->left,root->data);
} …Run Code Online (Sandbox Code Playgroud)