You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /*
  2. * tree.c
  3. *
  4. * Created by Yigit Colakoglu on 07/06/2021.
  5. * Copyright yigit@yigitcolakoglu.com. 2021. All rights reserved.
  6. */
  7. #include "tree.h"
  8. #include "linkedlist.h"
  9. #include "urlparse.h"
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. extern TreeNode *root;
  14. TreeNode *addtree(TreeNode *parent, TreeNode *p) {
  15. if (parent == NULL)
  16. return p;
  17. int strdiff = strcmp(parent->path, p->path);
  18. if (!strdiff) {
  19. while (p->params != NULL) {
  20. if (p->params == NULL ||
  21. linkedlistfind(parent->params, p->params->data) == -1) {
  22. p->params = linkedlistadd(parent->params, p->params->data);
  23. }
  24. p->params = p->params->next;
  25. }
  26. } else if (strdiff < 0) {
  27. parent->left = addtree(parent->left, p);
  28. parent->left->parent = parent;
  29. } else {
  30. parent->right = addtree(parent->right, p);
  31. parent->right->parent = parent;
  32. }
  33. return parent;
  34. }
  35. void rotatetreeleft(TreeNode *p) {
  36. TreeNode *r = p->right;
  37. p->right = r->left;
  38. if (p->right)
  39. p->right->parent = p;
  40. r->parent = p->parent;
  41. if (p->parent == NULL)
  42. root = r;
  43. else if (p->parent->left == p)
  44. p->parent->left = r;
  45. else
  46. p->parent->right = r;
  47. r->left = p;
  48. p->parent = r;
  49. }
  50. void rotatetreeright(TreeNode *p) {
  51. TreeNode *l = p->left;
  52. p->left = l->right;
  53. if (p->left)
  54. p->left->parent = p;
  55. l->parent = p->parent;
  56. if (p->parent == NULL)
  57. root = l;
  58. else if (p->parent->left == p)
  59. p->parent->left = l;
  60. else
  61. p->parent->right = l;
  62. l->right = p;
  63. p->parent = l;
  64. }
  65. void balancetree(TreeNode *root, TreeNode *node) {
  66. TreeNode *p = NULL;
  67. TreeNode *gP = NULL;
  68. while (node->parent != NULL && node->parent->parent != NULL && node->red && node->parent->red ) {
  69. p = node->parent;
  70. gP = node->parent->parent;
  71. if (gP->left == p) {
  72. if (gP->right != NULL && gP->right->red) {
  73. gP->red = 1;
  74. gP->left->red = 0;
  75. gP->right->red = 0;
  76. node = gP;
  77. }else{
  78. if(p->right == node){
  79. rotatetreeleft(p);
  80. node = p;
  81. p = node->parent;
  82. }else{
  83. rotatetreeright(gP);
  84. int c = p->red;
  85. p->red = gP->red;
  86. gP->red = c;
  87. node = p;
  88. }
  89. }
  90. } else {
  91. if(gP->left != NULL && gP->left->red){
  92. gP->red = 1;
  93. gP->left->red = 0;
  94. gP->right->red = 0;
  95. node = gP;
  96. }else{
  97. if(p->left == node){
  98. rotatetreeright(p);
  99. node = p;
  100. p = node->parent;
  101. }else{
  102. rotatetreeleft(gP);
  103. int c = p->red;
  104. p->red = gP->red;
  105. gP->red = c;
  106. node = p;
  107. }
  108. }
  109. }
  110. }
  111. root->red = 0;
  112. }
  113. TreeNode *treealloc(void) { return (TreeNode *)malloc(sizeof(TreeNode)); }
  114. void printtree(TreeNode *root, FILE *out, char *payload, int minparams) {
  115. if (root != NULL) {
  116. printtree(root->left, out, payload, minparams);
  117. if(root->nparams >= minparams){
  118. fprintf(out, "%s", root->path);
  119. (!root->nparams) ? : fprintf(out, "%c",'?');
  120. linkedlistprint(root->params, out, payload);
  121. fprintf(out, "%c", '\n');
  122. }
  123. printtree(root->right, out, payload, minparams);
  124. }
  125. }