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
@ -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,14 +59,12 @@ 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++)
|
||||||
@ -74,7 +72,6 @@ void printAVL(avlNode *node, int level)
|
|||||||
|
|
||||||
printf("%d", node->key);
|
printf("%d", node->key);
|
||||||
|
|
||||||
//printf("_");
|
|
||||||
printAVL(node->left, level+1);
|
printAVL(node->left, level+1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -121,7 +118,6 @@ avlNode *RightLeftRotate(avlNode *z)
|
|||||||
return (leftRotate(z));
|
return (leftRotate(z));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
avlNode *insert(avlNode *node, int key)
|
avlNode *insert(avlNode *node, int key)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -134,20 +130,15 @@ avlNode *insert(avlNode *node, int 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 as per the AVL formula*/
|
||||||
node->height = (max(nodeHeight(node->left),
|
node->height = (max(nodeHeight(node->left), nodeHeight(node->right)) + 1);
|
||||||
nodeHeight(node->right)) + 1);
|
|
||||||
|
|
||||||
|
|
||||||
/*Checking for the balance condition*/
|
/*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);
|
||||||
|
|
||||||
@ -160,17 +151,17 @@ avlNode *insert(avlNode *node, int 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)
|
||||||
@ -210,14 +201,14 @@ avlNode *delete(avlNode *node, int queryNum)
|
|||||||
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);
|
||||||
|
|
||||||
@ -230,6 +221,7 @@ avlNode *delete(avlNode *node, int queryNum)
|
|||||||
{
|
{
|
||||||
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);
|
||||||
@ -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;
|
||||||
@ -327,8 +309,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;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
@ -398,6 +378,7 @@ int main()
|
|||||||
printAVL(root, 1);
|
printAVL(root, 1);
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
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