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