fork download
  1. typedef struct node *tree;
  2.  
  3. int height(tree t){ // height of a (sub) tree
  4. if (t == NULL) return 0;
  5. return t->height; // relies on memoized height value
  6. }
  7.  
  8. static int max(int a, int b) { // utility function -- maximum
  9. if (a > b) return a;
  10. return b;
  11. }
  12.  
  13. static void memoize(tree t){ // update fields after tree mutation
  14. if (t == NULL) return;
  15. t->height = 1 + max(height(t->left), height(t->right));
  16. }
  17.  
  18. static tree newnode(int d){ // create a new treenode
  19. tree t = malloc(sizeof(struct node));
  20. t->data = d;
  21. t->left = NULL;
  22. t->right = NULL;
  23. memoize(t);
  24. return t;
  25. }
  26.  
  27. static void setleft(tree t, tree p){ // set left link and memoize
  28. t->left = p;
  29. memoize(t);
  30. }
  31.  
  32. static void setright(tree t, tree p){ // set right link and memoize
  33. t->right = p;
  34. memoize(t);
  35. }
  36.  
  37. static tree raise_right(tree t) { // "left" rotation
  38. tree tmp = t->right;
  39. setright(t, tmp->left);
  40. setleft(tmp, t);
  41. return tmp;
  42. }
  43.  
  44. static tree raise_left(tree t) { // "right" rotation
  45. tree tmp = t->left;
  46. setleft(t, tmp->right);
  47. setright(tmp, t);
  48. return tmp;
  49. }
  50.  
  51. static tree rebalance(tree t){ // rebalance after insert/del
  52. if (t == NULL) return t;
  53. if (height(t->left) > 1 + height(t->right)) { // left too high
  54. if (height(t->left->right) > height(t->left->left)) {
  55. setleft(t, raise_right(t->left)); // first of double rotation
  56. }
  57. return raise_left(t); // single/second rotation
  58. }
  59. if (height(t->right) > 1 + height(t->left)) { // right too high
  60. if (height(t->right->left) > height(t->right->right)) {
  61. setright(t, raise_left(t->right)); // first of double rotation
  62. }
  63. return raise_right(t); // single/second rotation
  64. }
  65. return t;
  66. }
  67.  
  68. tree insertavl(tree t, int d){ // AVleft preserving insert
  69. if (t == NULL) return newnode(d);
  70. if (d < t->data) {
  71. setleft(t, insertavl(t->left, d));
  72. } else {
  73. setright(t, insertavl(t->right, d));
  74. }
  75. return rebalance(t);
  76. }
  77.  
  78. static tree remove_rightmost(tree t, int *d){ // sets d to deleted key
  79. if (t->right == NULL) {
  80. *d = t->data;
  81. tree tmp = t->left;
  82. free(t);
  83. return tmp;
  84. }
  85. setright(t, remove_rightmost(t->right, d));
  86. return rebalance(t);
  87. }
  88.  
  89. tree deleteavl(tree t, int d){ // delete node with key value d
  90. if (t == NULL) return t;
  91. if (d < t->data) { // recurse left
  92. setleft(t, deleteavl(t->left, d));
  93. } else if (d > t->data) { // recurse right
  94. setright(t, deleteavl(t->right, d));
  95. } else if (t->right == NULL) { // no right child; promote left
  96. tree tmp = t;
  97. t = t->left;
  98. free(tmp);
  99. } else if (t->left == NULL) { // no left child; promote right
  100. tree tmp = t;
  101. t = t->right;
  102. free(tmp);
  103. } else {
  104. setleft(t, remove_rightmost(t->left, &t->data)); // replace root key
  105. }
  106. return rebalance(t);
  107. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int height(node*)’:
prog.cpp:4: error: ‘NULL’ was not declared in this scope
prog.cpp:5: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp: In function ‘void memoize(node*)’:
prog.cpp:14: error: ‘NULL’ was not declared in this scope
prog.cpp:15: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:15: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:15: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp: In function ‘node* newnode(int)’:
prog.cpp:19: error: invalid application of ‘sizeof’ to incomplete type ‘node’ 
prog.cpp:19: error: ‘malloc’ was not declared in this scope
prog.cpp:20: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:21: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:21: error: ‘NULL’ was not declared in this scope
prog.cpp:22: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp: In function ‘void setleft(node*, node*)’:
prog.cpp:28: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp: In function ‘void setright(node*, node*)’:
prog.cpp:33: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp: In function ‘node* raise_right(node*)’:
prog.cpp:38: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:39: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp: In function ‘node* raise_left(node*)’:
prog.cpp:45: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:46: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp: In function ‘node* rebalance(node*)’:
prog.cpp:52: error: ‘NULL’ was not declared in this scope
prog.cpp:53: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:53: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:54: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:54: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:55: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:59: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:59: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:60: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:60: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:61: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp: In function ‘node* insertavl(node*, int)’:
prog.cpp:69: error: ‘NULL’ was not declared in this scope
prog.cpp:70: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:71: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:73: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp: In function ‘node* remove_rightmost(node*, int*)’:
prog.cpp:79: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:79: error: ‘NULL’ was not declared in this scope
prog.cpp:80: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:81: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:82: error: ‘free’ was not declared in this scope
prog.cpp:85: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp: In function ‘node* deleteavl(node*, int)’:
prog.cpp:90: error: ‘NULL’ was not declared in this scope
prog.cpp:91: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:92: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:93: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:94: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:95: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:95: error: ‘NULL’ was not declared in this scope
prog.cpp:97: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:98: error: ‘free’ was not declared in this scope
prog.cpp:99: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:101: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:102: error: ‘free’ was not declared in this scope
prog.cpp:104: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
prog.cpp:104: error: invalid use of incomplete type ‘struct node’
prog.cpp:1: error: forward declaration of ‘struct node’
stdout
Standard output is empty