I have written a code which performs some basic operations on binary search tree. My code runs perfectly on linux ecosystems like Ubuntu or WSL2 but gives incorrect output in windows ecosystem more specifically Windows 10. Here's my code
#include <stdio.h>
#include <malloc.h>
typedef struct TreeNode
{
int data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
typedef struct node
{
TreeNode *data;
struct node *next;
} node;
typedef struct queue
{
node *head;
node *tail;
int size;
} queue;
TreeNode *insert_in_bst(int element, TreeNode *root);
TreeNode *delete_in_bst(int element, TreeNode *root);
void inorder(TreeNode *root);
void levelwise(TreeNode *root);
void enqueue(queue *q, TreeNode *root);
node *dequeue(queue *q);
void mirror_image(TreeNode *root);
int height(TreeNode *root);
int main(int argc, char *argv[])
{
int n = 0;
printf("Enter elements to be added to bst - ");
scanf("%d", &n);
TreeNode *root;
while (n != -1)
{
root = insert_in_bst(n, root);
scanf("%d", &n);
}
levelwise(root);
printf("\nCurrent inorder - ");
inorder(root);
int del;
printf("\nEnter element to delete - ");
scanf("%d", &del);
delete_in_bst(del, root);
levelwise(root);
printf("\nNew inorder - ");
inorder(root);
printf("\nMirror image\n");
mirror_image(root);
levelwise(root);
int h = height(root);
printf("\nHeight = %d", h);
return 0;
}
TreeNode *insert_in_bst(int element, TreeNode *root)
{
if (!root)
{
TreeNode *newNode = (TreeNode *)malloc(sizeof(TreeNode));
newNode->data = element;
return newNode;
}
if (element < root->data)
{
root->left = insert_in_bst(element, root->left);
}
else
{
root->right = insert_in_bst(element, root->right);
}
return root;
}
TreeNode *delete_in_bst(int element, TreeNode *root)
{
if (!root)
return NULL;
if (element < root->data)
{
root->left = delete_in_bst(element, root->left);
}
else if (element > root->data)
{
root->right = delete_in_bst(element, root->right);
}
else
{
if (!root->left)
return root->right;
else if (!root->right)
return root->left;
else
{
TreeNode *curr = root->right;
while (!curr->left)
curr = curr->left;
root->data = curr->data;
root->right = delete_in_bst(curr->data, root->right);
}
}
return root;
}
void inorder(TreeNode *root)
{
if (!root)
return;
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
void levelwise(TreeNode *root)
{
node *currNode = (node *)malloc(sizeof(node));
currNode->data = root;
queue *q = (queue *)malloc(sizeof(queue));
enqueue(q, root);
while (q->size != 0)
{
int l = q->size;
for (int i = 0; i < l; i++)
{
node *temp = dequeue(q);
TreeNode *t = temp->data;
printf("%d ", t->data);
if (t->left)
{
enqueue(q, t->left);
}
if (t->right)
{
enqueue(q, t->right);
}
}
printf("\n");
}
}
void enqueue(queue *q, TreeNode *root)
{
node *temp = (node *)malloc(sizeof(node));
temp->data = root;
if (!q || !q->head)
{
q->head = temp;
q->tail = temp;
q->size = 1;
}
else
{
q->tail->next = temp;
q->tail = q->tail->next;
q->size++;
}
}
node *dequeue(queue *q)
{
node *top = q->head;
q->head = q->head->next;
q->size--;
return top;
}
void mirror_image(TreeNode *root)
{
if (!root)
return;
TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
mirror_image(root->left);
mirror_image(root->right);
}
int height(TreeNode *root)
{
if (!root)
return 0;
int lh = height(root->left);
int rh = height(root->right);
return 1 + (lh > rh ? lh : rh);
}
Heres the output on Windows ecosystems
Here's the output on linux/wsl2
On further debugging on Windows systems, i found out that by default; value to root is assigned as 1. On linux systems this thing doesnt happen. Can someoe help me find out the reason? Thank you!