mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-22 13:31:21 +03:00
Formating code
This commit is contained in:
parent
1e255fca0c
commit
3ee9cbdfa2
@ -22,12 +22,12 @@ avlNode *newNode(int key)
|
||||
if(node == NULL)
|
||||
printf("!! Out of Space !!\n");
|
||||
else
|
||||
{
|
||||
node->key = key;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
node->height = 0;
|
||||
}
|
||||
{
|
||||
node->key = key;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
node->height = 0;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
@ -36,7 +36,7 @@ int nodeHeight(avlNode *node)
|
||||
{
|
||||
if(node == NULL)
|
||||
return -1;
|
||||
else
|
||||
else
|
||||
return(node->height);
|
||||
}
|
||||
|
||||
@ -48,7 +48,7 @@ int heightDiff(avlNode *node)
|
||||
return(nodeHeight(node->left) - nodeHeight(node->right));
|
||||
}
|
||||
|
||||
/* Returns the node with min key in the left subtree*/
|
||||
/* Returns the node with min key in the left subtree*/
|
||||
avlNode *minNode(avlNode *node)
|
||||
{
|
||||
avlNode *temp = node;
|
||||
@ -59,22 +59,19 @@ avlNode *minNode(avlNode *node)
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
void printAVL(avlNode *node, int level)
|
||||
{
|
||||
int i;
|
||||
if(node!=NULL)
|
||||
{
|
||||
printAVL(node->right, level+1);
|
||||
//printf("_");
|
||||
printf("\n\n");
|
||||
printf("\n\n");
|
||||
|
||||
for(i=0; i<level; i++)
|
||||
printf("\t");
|
||||
|
||||
printf("%d", node->key);
|
||||
|
||||
//printf("_");
|
||||
printAVL(node->left, level+1);
|
||||
}
|
||||
}
|
||||
@ -121,56 +118,50 @@ avlNode *RightLeftRotate(avlNode *z)
|
||||
return (leftRotate(z));
|
||||
}
|
||||
|
||||
|
||||
avlNode *insert(avlNode *node, int key)
|
||||
{
|
||||
|
||||
if(node == NULL)
|
||||
return (newNode(key));
|
||||
|
||||
/*Binary Search Tree insertion*/
|
||||
/*Binary Search Tree insertion*/
|
||||
|
||||
if(key < node->key)
|
||||
node->left = insert(node->left, key); /*Recursive insertion in L subtree*/
|
||||
else if(key > node->key)
|
||||
node->right = insert(node->right, key); /*Recursive insertion in R subtree*/
|
||||
//else
|
||||
//return node;
|
||||
|
||||
/* Node Height as per the AVL formula*/
|
||||
node->height = (max(nodeHeight(node->left), nodeHeight(node->right)) + 1);
|
||||
|
||||
|
||||
/* Node Height as per the AVL formula*/
|
||||
node->height = (max(nodeHeight(node->left),
|
||||
nodeHeight(node->right)) + 1);
|
||||
|
||||
|
||||
/*Checking for the balance condition*/
|
||||
/*Checking for the balance condition*/
|
||||
int balance = heightDiff(node);
|
||||
|
||||
/*Left Left */
|
||||
|
||||
/*Left Left */
|
||||
if(balance>1 && key < (node->left->key))
|
||||
return rightRotate(node);
|
||||
|
||||
/*Right Right */
|
||||
/*Right Right */
|
||||
if(balance<-1 && key > (node->right->key))
|
||||
return leftRotate(node);
|
||||
|
||||
/*Left Right */
|
||||
/*Left Right */
|
||||
if (balance>1 && key > (node->left->key))
|
||||
{
|
||||
node = LeftRightRotate(node);
|
||||
}
|
||||
/*Right Left */
|
||||
|
||||
/*Right Left */
|
||||
if (balance<-1 && key < (node->right->key))
|
||||
{
|
||||
node = RightLeftRotate(node);
|
||||
}
|
||||
|
||||
return node;
|
||||
return node;
|
||||
|
||||
}
|
||||
|
||||
|
||||
avlNode *delete(avlNode *node, int queryNum)
|
||||
{
|
||||
if(node == NULL)
|
||||
@ -182,14 +173,14 @@ avlNode *delete(avlNode *node, int queryNum)
|
||||
node->right = delete(node->right, queryNum); /*Recursive deletion in R subtree*/
|
||||
else
|
||||
{
|
||||
/*Single or No Child*/
|
||||
/*Single or No Child*/
|
||||
if((node->left == NULL) || (node->right==NULL))
|
||||
{
|
||||
avlNode *temp = node->left ?
|
||||
node->left :
|
||||
node->right;
|
||||
avlNode *temp = node->left ?
|
||||
node->left :
|
||||
node->right;
|
||||
|
||||
/* No Child*/
|
||||
/* No Child*/
|
||||
if(temp == NULL)
|
||||
{
|
||||
temp = node;
|
||||
@ -202,39 +193,40 @@ avlNode *delete(avlNode *node, int queryNum)
|
||||
}
|
||||
else
|
||||
{
|
||||
/*Two Child*/
|
||||
/*Two Child*/
|
||||
|
||||
/*Get the smallest key in the R subtree*/
|
||||
/*Get the smallest key in the R subtree*/
|
||||
avlNode *temp = minNode(node->right);
|
||||
node->key = temp->key; /*Copy that to the root*/
|
||||
node->right = delete(node->right, temp->key); /*Delete the smallest in the R subtree.*/
|
||||
}
|
||||
}
|
||||
/*single node in tree*/
|
||||
|
||||
/*single node in tree*/
|
||||
if(node == NULL)
|
||||
return node;
|
||||
|
||||
|
||||
/*Update height*/
|
||||
node->height = (max(nodeHeight(node->left),
|
||||
nodeHeight(node->right)) + 1);
|
||||
/*Update height*/
|
||||
node->height = (max(nodeHeight(node->left), nodeHeight(node->right)) + 1);
|
||||
|
||||
int balance = heightDiff(node);
|
||||
int balance = heightDiff(node);
|
||||
|
||||
/*Left Left */
|
||||
/*Left Left */
|
||||
if((balance>1) && (heightDiff(node->left) >= 0))
|
||||
return rightRotate(node);
|
||||
|
||||
/*Left Right */
|
||||
/*Left Right */
|
||||
if ((balance>1) && (heightDiff(node->left) < 0))
|
||||
{
|
||||
node = LeftRightRotate(node);
|
||||
}
|
||||
/*Right Right */
|
||||
|
||||
/*Right Right */
|
||||
if((balance<-1) && (heightDiff(node->right) >= 0))
|
||||
return leftRotate(node);
|
||||
|
||||
/*Right Left */
|
||||
/*Right Left */
|
||||
if ((balance<-1) && (heightDiff(node->right) < 0))
|
||||
{
|
||||
node = RightLeftRotate(node);
|
||||
@ -257,14 +249,11 @@ avlNode *findNode(avlNode *node, int queryNum)
|
||||
return node;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void printPreOrder(avlNode *node)
|
||||
{
|
||||
if(node == NULL)
|
||||
return;
|
||||
// printf("\nprintPreOrder function\n");
|
||||
//printf(" %d-H:%d \t", (node->key), (node->height));
|
||||
|
||||
printf(" %d ", (node->key));
|
||||
printPreOrder(node->left);
|
||||
printPreOrder(node->right);
|
||||
@ -274,9 +263,7 @@ void printInOrder(avlNode *node)
|
||||
{
|
||||
if(node == NULL)
|
||||
return;
|
||||
// printf("\nprintPreOrder function\n");
|
||||
printInOrder(node->left);
|
||||
//printf(" %d-H:%d \t", (node->key), (node->height));
|
||||
printf(" %d ", (node->key));
|
||||
printInOrder(node->right);
|
||||
}
|
||||
@ -285,16 +272,11 @@ void printPostOrder(avlNode *node)
|
||||
{
|
||||
if(node == NULL)
|
||||
return;
|
||||
// printf("\nprintPreOrder function\n");
|
||||
printPostOrder(node->left);
|
||||
printPostOrder(node->right);
|
||||
//printf("%d ", (node->key));
|
||||
//printf(" %d-H:%d \t", (node->key), (node->height));
|
||||
printf(" %d ", (node->key));
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
int choice;
|
||||
@ -309,16 +291,16 @@ int main()
|
||||
{
|
||||
printf("\n\nEnter the Step to Run : \n");
|
||||
|
||||
printf("\t1: Insert a node into AVL tree\n");
|
||||
printf("\t2: Delete a node in AVL tree\n");
|
||||
printf("\t3: Search a node into AVL tree\n");
|
||||
printf("\t4: printPreOrder (Ro L R) Tree\n");
|
||||
printf("\t5: printInOrder (L Ro R) Tree\n");
|
||||
printf("\t6: printPostOrder (L R Ro) Tree\n");
|
||||
printf("\t7: printAVL Tree\n");
|
||||
printf("\t1: Insert a node into AVL tree\n");
|
||||
printf("\t2: Delete a node in AVL tree\n");
|
||||
printf("\t3: Search a node into AVL tree\n");
|
||||
printf("\t4: printPreOrder (Ro L R) Tree\n");
|
||||
printf("\t5: printInOrder (L Ro R) Tree\n");
|
||||
printf("\t6: printPostOrder (L R Ro) Tree\n");
|
||||
printf("\t7: printAVL Tree\n");
|
||||
|
||||
printf("\t0: EXIT\n");
|
||||
scanf("%d", &choice);
|
||||
printf("\t0: EXIT\n");
|
||||
scanf("%d", &choice);
|
||||
|
||||
switch(choice)
|
||||
{
|
||||
@ -327,31 +309,29 @@ int main()
|
||||
flag=0;
|
||||
printf("\n\t\tExiting, Thank You !!\n");
|
||||
break;
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
case 1:
|
||||
{
|
||||
|
||||
|
||||
printf("\n\tEnter the Number to insert: ");
|
||||
scanf("%d", &insertNum);
|
||||
scanf("%d", &insertNum);
|
||||
|
||||
tempNode = findNode(root, insertNum);
|
||||
tempNode = findNode(root, insertNum);
|
||||
|
||||
if(tempNode!=NULL)
|
||||
printf("\n\t %d Already exists in the tree\n", insertNum);
|
||||
else
|
||||
{
|
||||
printf("\n\tPrinting AVL Tree\n");
|
||||
printAVL(root, 1);
|
||||
printf("\n");
|
||||
if(tempNode!=NULL)
|
||||
printf("\n\t %d Already exists in the tree\n", insertNum);
|
||||
else
|
||||
{
|
||||
printf("\n\tPrinting AVL Tree\n");
|
||||
printAVL(root, 1);
|
||||
printf("\n");
|
||||
|
||||
root = insert(root, insertNum);
|
||||
printf("\n\tPrinting AVL Tree\n");
|
||||
printAVL(root, 1);
|
||||
printf("\n");
|
||||
}
|
||||
root = insert(root, insertNum);
|
||||
printf("\n\tPrinting AVL Tree\n");
|
||||
printAVL(root, 1);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
@ -359,23 +339,23 @@ int main()
|
||||
case 2:
|
||||
{
|
||||
printf("\n\tEnter the Number to Delete: ");
|
||||
scanf("%d", &queryNum);
|
||||
scanf("%d", &queryNum);
|
||||
|
||||
tempNode = findNode(root, queryNum);
|
||||
tempNode = findNode(root, queryNum);
|
||||
|
||||
if(tempNode==NULL)
|
||||
printf("\n\t %d Does not exist in the tree\n", queryNum);
|
||||
else
|
||||
{
|
||||
printf("\n\tPrinting AVL Tree\n");
|
||||
printAVL(root, 1);
|
||||
printf("\n");
|
||||
root = delete(root, queryNum);
|
||||
if(tempNode==NULL)
|
||||
printf("\n\t %d Does not exist in the tree\n", queryNum);
|
||||
else
|
||||
{
|
||||
printf("\n\tPrinting AVL Tree\n");
|
||||
printAVL(root, 1);
|
||||
printf("\n");
|
||||
root = delete(root, queryNum);
|
||||
|
||||
printf("\n\tPrinting AVL Tree\n");
|
||||
printAVL(root, 1);
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n\tPrinting AVL Tree\n");
|
||||
printAVL(root, 1);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
@ -383,53 +363,54 @@ int main()
|
||||
case 3:
|
||||
{
|
||||
printf("\n\tEnter the Number to Search: ");
|
||||
scanf("%d", &queryNum);
|
||||
|
||||
tempNode = findNode(root, queryNum);
|
||||
scanf("%d", &queryNum);
|
||||
|
||||
tempNode = findNode(root, queryNum);
|
||||
|
||||
|
||||
if(tempNode == NULL)
|
||||
printf("\n\t %d : Not Found\n", queryNum);
|
||||
else
|
||||
{
|
||||
printf("\n\t %d : Found at height %d \n", queryNum, tempNode->height);
|
||||
if(tempNode == NULL)
|
||||
printf("\n\t %d : Not Found\n", queryNum);
|
||||
else
|
||||
{
|
||||
printf("\n\t %d : Found at height %d \n", queryNum, tempNode->height);
|
||||
|
||||
printf("\n\tPrinting AVL Tree\n");
|
||||
printAVL(root, 1);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n\tPrinting AVL Tree\n");
|
||||
printAVL(root, 1);
|
||||
printf("\n");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 4:
|
||||
{
|
||||
printf("\nPrinting Tree preOrder\n");
|
||||
printPreOrder(root);
|
||||
|
||||
printPreOrder(root);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 5:
|
||||
{
|
||||
printf("\nPrinting Tree inOrder\n");
|
||||
printInOrder(root);
|
||||
|
||||
printInOrder(root);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 6:
|
||||
{
|
||||
printf("\nPrinting Tree PostOrder\n");
|
||||
printPostOrder(root);
|
||||
|
||||
printPostOrder(root);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 7:
|
||||
{
|
||||
printf("\nPrinting AVL Tree\n");
|
||||
printAVL(root, 1);
|
||||
|
||||
printAVL(root, 1);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@ -438,8 +419,6 @@ int main()
|
||||
flag=0;
|
||||
printf("\n\t\tExiting, Thank You !!\n");
|
||||
break;
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user