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.

51 lines
1.2 KiB

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 <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. TreeNode *addtree(TreeNode *p, URL *url) {
  14. if (p == NULL) {
  15. TreeNode *newnode = treealloc();
  16. newnode->path = url->base;
  17. newnode->params = url->params;
  18. newnode->left = newnode->right = NULL;
  19. return newnode;
  20. }
  21. int strdiff = strcmp(url->base, p->path);
  22. if (!strdiff) {
  23. while(url->params != NULL){
  24. if(p->params == NULL || linkedlistfind(p->params, url->params->data) == -1){
  25. p->params = linkedlistadd(p->params, url->params->data);
  26. }
  27. url->params = url->params->next;
  28. }
  29. } else if (strdiff < 0) {
  30. p->left = addtree(p->left, url);
  31. } else {
  32. p->right = addtree(p->right, url);
  33. }
  34. return p;
  35. }
  36. TreeNode *treealloc(void){
  37. return (TreeNode *) malloc(sizeof(TreeNode));
  38. }
  39. void printtree(TreeNode *root, FILE *out){
  40. if(root != NULL){
  41. printtree(root->left, out);
  42. fprintf(out, "%s ", root->path);
  43. linkedlistprint(root->params, out);
  44. fprintf(out, "%c", '\n');
  45. printtree(root->right, out);
  46. }
  47. }