online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
#include <stdio.h> #include <stdlib.h> struct tree { // node struct int value; int key; char color; struct tree *left; struct tree *right; }; typedef struct tree mynode; // move this below declaration for struct tree. // keeping declarations in the right order does // a great deal in making code easier to navigate. //allocate memory, set color, key, value and NULL sons mynode* red_node_create (int Key, int Value) { mynode *p = (mynode *) calloc (1, sizeof (mynode)); p->color = 1; p->value = Value; p->key = Key; p->left = NULL; p->right = NULL; return p; } // having a proper way to free memory is very important when dealing // with trees and lists. It would be better if this was not recursive. // I'll leave this exercise to you. I'm sure you can find examples on // the Net. void free_node(mynode* p) { if (!p) return; free_node(p->left); free_node(p->right); free(p); } // Rotation only needs 1 parameter: the root node around which // rotation occurs. void rightRotate (mynode ** root) { if (!root || !*root || !(*root)->left) { printf ("bad arguments in myRightRotate()\n"); return; } // take left node, make it parent, make old parent the right node // of new parent, and make right node of old left node the left node // of old_parent // using letters as in graphics on this page: // https://en.wikipedia.org/wiki/Tree_rotation // these non leaves nodes cannot be NULL mynode* Q = *root; mynode* P = Q->left; // the leaf nodes could be NULL. Only B is needed. // but A and C are checked in unit test look-alike below. mynode* A = P->left; mynode* B = P->right; mynode* C = Q->right; // rotate *root = P; P->right = Q; Q->left = B; #define CHECK_RIGHT_ROTATE // undef as needed. #ifdef CHECK_RIGHT_ROTATE // make sure the nodes are in place. if (P != *root) printf("RR error. root is not P\n"); if (Q != (*root)->right) printf("RR error. root->right is not Q\n"); if (A != P->left) printf("RR error. A is not at P->left\n"); if (B != Q->left) printf("RR error. B is not at Q->left\n"); if (C != Q->right) printf("RR error. C is not at Q->right\n"); #endif } int main () { // make minimal tree for a proper rotate. root has both left and right node. // - left node has both left and right leaf nodes, values and 1 and 2 // - node is a leaf node. value 3 mynode *root = red_node_create (0, 0); root->left = red_node_create (0, 0); root->left->left = red_node_create(1, 1); root->left->right = red_node_create(2, 2); root->right = red_node_create(3, 3); // before rotate, we should have // root->left->left: leaf, value 1 // root->left->right: leaf, value 2 // root->right: leaf, value 3 printf ("before: %d %d %d \n", root->left->left->value, root->left->right->value, root->right->value); rightRotate (&root); // after rotate, we should have // root->left: leaf, value 1 // root->right->left: leaf, value 2 // root->right->right: leaf, value 3 printf ("after: %d %d %d \n", root->left->value, root->right->left->value, root->right->right->value); free_node(root); return 0; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue