Skip to content

Updated Tree/AVLTREE.c with cross-platform compatibility #754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions Trees/AVLTREE.C
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
/*Successful implementation of AVL tree with proper rotations
Code done by Parshwa Shah
*/
/*Update From Hari(username: Haricodezz)
clrscr() is not part of the standard C library and is not portable.
For cross-platform compatibility, consider using:
system("cls"); // for Windows
system("clear"); // for Unix-like systems
Alternatively, ANSI escape codes can be used for more control over console output.


*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
Expand All @@ -25,13 +34,13 @@ int max1(int a,int b)
{
return (a>b)?a:b;
}
struct node* new(int key)
struct node* New(int key)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->key = key;
node->left = NULL;
node->right = NULL;
node->h= 1; // new node is initially added at leaf
node->h= 1; // New node is initially added at leaf
return(node);
}
struct node *leftrot(struct node *x)
Expand Down Expand Up @@ -70,7 +79,7 @@ int bal=0;
if(ele==NULL)
{
//printf("Key=%d Balance factor=0\n",ele->key);
return new(ke);
return New(ke);
}
else if(ke>ele->key)
{
Expand Down Expand Up @@ -118,15 +127,15 @@ while(curr->left!=NULL)
{curr=curr->left;}
return curr;
}
struct node* delete(struct node *root,int key)
struct node* Delete(struct node *root,int key)
{
int bal=0;
if(root==NULL)
{return root;}
if(key<root->key)
{root->left=delete(root->left,key);}
{root->left=Delete(root->left,key);}
else if(key>root->key)
{root->right=delete(root->right,key);}
{root->right=Delete(root->right,key);}
else
{
if((root->left==NULL) || (root->right==NULL))
Expand All @@ -147,7 +156,7 @@ else
{
struct node *temp= inordersucc(root->right);
root->key=temp->key;
root->right=delete(root->right,temp->key);
root->right=Delete(root->right,temp->key);
}
}

Expand Down Expand Up @@ -189,10 +198,17 @@ inorder(root->left);
printf("%d ",root->key);
inorder(root->right);
}
void main()
int main()
{
int key,m=0;
clrscr();
// clrscr();
// Replaced clrscr() with system-specific clear command for portability.
#ifdef _WIN32
system("cls"); // Clear screen on Windows
#else
system("clear"); // Clear screen on Unix-like systems
#endif

do
{
printf("Enter the key to insert or 0 to quit\n");
Expand Down Expand Up @@ -224,11 +240,12 @@ scanf("%d",&key);
if(key==0)
break;
printf("\n************************************\n\n");
root=delete(root,key);
root=Delete(root,key);
printf("\n************************************\n\n");
}
while(key!=0);
printf("Inorder traversal of tree : ");
inorder(root);
getch();
return 0;
}