Reputation: 21
I made avl tree and it successfully compiles. however, while it is inserting, it inserts only 1 numbers and makes a segmentation fault. Where do I need to change to make the code successful?
I got help from this file https://www.geeksforgeeks.org/avl-tree-set-1-insertion/ and made a static internal function _insert to make the code look simpler.
/* internal function
This function uses recursion to insert the new data into a leaf node
return pointer to new root
*/
static NODE *_insert(NODE *root, NODE *newPtr)
{
if (root) return newPtr;
if (newPtr->data < root->data)
root->left = _insert(root->left, newPtr);
else if (newPtr->data >= root->data)
root->right = _insert(root->right, newPtr);
root->height = 1 + max(root->left->height, root->right->height);
int bal = getHeight(root);
if (bal > 1 && newPtr->data < root->left->data)
return rotateRight(root);
if (bal<-1 && newPtr->data > root->right->data)
return rotateLeft(root);
if (bal > 1 && newPtr->data > root->left->data)
{
root->left = rotateLeft(root->left);
return rotateRight(root);
}
if (bal < -1 && newPtr->data < root->right->data)
{
root->right = rotateRight(root->right);
return rotateLeft(root);
}
return root;
}
int AVL_Insert(AVL_TREE *pTree, int data)
{
NODE *pNode = _makeNode(data);
if (!pNode) return 0;
pTree->root = _insert(pTree->root, pNode);
if (!pTree->root) return 0;
else return 1;
}
``````
````````````
/* internal function
Exchanges pointers to rotate the tree to the right
updates heights of the nodes
return new root
*/
static NODE *rotateRight(NODE *root)
{
NODE *t1 = root->left;
NODE *t2 = t1->right;
t1->right = root;
root->left = t2;
root->height = max(root->left->height, root->right->height) + 1;
t1->height = max(t1->left->height, t1->right->height) + 1;
return t1;
}
/* internal function
Exchanges pointers to rotate the tree to the left
updates heights of the nodes
return new root
*/
static NODE *rotateLeft(NODE *root)
{
NODE *t1 = root->right;
NODE *t2 = t1->left;
t1->left = root;
root->right = t2;
root->height = max(root->left->height, root->right->height) + 1;
t1->height = max(t1->left->height, t1->right->height) + 1;
}
//main code
srand(time(NULL));
for (int i = 0; i < MAX_ELEM; i++)
{
data = rand() % (MAX_ELEM * 3) + 1; // random number
// data = i+1; // sequential number
fprintf(stdout, "%d ", data);
// insert function call
AVL_Insert(tree, data);
}
fprintf(stdout, "\n");
When I run the visual studio, I get only 1 number inserted And when I run this in linux, I get segmentation fault(core dumped)
How can I solve this? thank you
Upvotes: 2
Views: 313
Reputation: 18420
You made a mistake here:
static NODE *_insert(NODE *root, NODE *newPtr)
{
if (root) return newPtr;
When there is no root (i.e. the empty tree), then the new tree consists solely of the new node. Hence, this should be:
if (!root) return newPtr;
This means, your segmentation fault happens in the next line:
if (newPtr->data < root->data)
because you dereference the null pointer root
here.
Upvotes: 1