Im trying to create an insertion function on an binary search tree. I looked at other questions here but they re not like my problem.
I can compile it but it crashes on the commented line below:
void insertNode(TreeNode** r, int n) {
TreeNode* novono;
novono = createNode(n);
if (*r == NULL){ //corrected
*r = novono;
(*r) -> right = NULL;
(*r) -> left = NULL;
(*r) -> data = n;
}
if (n < ((*r) -> data))
insertNode(&((*r) -> left), n); //SEGMENTATION FAULT HERE
if (n > ((*r) -> data))
insertNode(&((*r) -> right), n); //SEGMENTATION FAULT HERE
}
This is the main function that tries to insert ints from a vector:
int main() {
#define MAX 15
TreeNode* root = NULL;
bool OK = true;
int i = 0,
n,
V[MAX] = { 50, 10, 70, 2, 1, 13, 15, 65, 69, 77, 11, 3, 80, 76, 64};
initTree(&root);
printf("Passou da inicialização");
for ( i = 0 ; i < MAX ; i++ ) {
printf("[%d] ", V[i]);
insertNode(&root, V[i]);
}
And this is the inicialization function that is executed before the insertion. (i dont think it is the problem, but im putting it here just cause i know you will ask for it).
void clearTree( TreeNode** r) {
if ( *r != NULL) {
printf("Clear %d\n", (*r)->data);
clearTree(&(*r)->left);
clearTree(&(*r)->right);
free(*r);
*r = NULL;
}
}
void initTree( TreeNode** r) {
if (*r != NULL)
clearTree( &(*r) );
if( *r == NULL)
printf("Limpeza com sucesso !!\n");
else
printf("Limpeza sem sucesso !!\n");
}
Create Node function as requested:
TreeNode* createNode(int n) {
TreeNode* newNode = (TreeNode*) malloc(sizeof(TreeNode));
if ( newNode != NULL) {
newNode->data = n;
newNode->left =
newNode->right = NULL;
}
return newNode;
}
乍一看...“ if(* r = NULL)”应该是“ if(* r == NULL)”
On second look, I don't see the crash, but I see a memory leak. Think what happen within the same function with novono if (*r == NULL) was false. I would recommend you to forget this code and try to write it again from scratch and try to avoid of pointers to pointers - **something.
instead of:
void insertNode(TreeNode** r, int n)
I woud try:
/* this error handling would be sufficient for the beginner */
void * xmalloc(size_t size) {
void * ret = malloc(size);
if (!ret) {
printf("error\n");
exit(1);
}
return ret;
}
/* returns root of the tree */
TreeNode * insertNode(TreeNode* r, int n) {
if (!r) return createNode(n);
...
return r;
}
TreeNode * createNode(int n) {
TreeNode * ret = xmalloc(sizeof(TreeNode));
ret->left = left->right = 0;
ret->data = n;
return ret;
}
Instead of initTree, just:
....
TreeNode * root = 0;
root = insertNode(root, 10);
root = insertNode(root, 12);
root = insertNode(root, 20);
root = insertNode(root, 14);
if (n < ((*r) -> data));
insertNode(&((*r) -> left), n);
is equivalent to:
if (n < ((*r) -> data)) {
;
}
insertNode(&((*r) -> left), n);
So very likely NOT what you want.
(GCC with -Wempty-body
(included in -Wextra
) would have warned you about it.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.