fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct t
  5. {
  6. char key[50];
  7. struct t *r, *l;
  8. };
  9.  
  10. // void
  11. // bin_tree_search_inorder(struct t *t, char *key)
  12. // {
  13. // if (t != NULL)
  14. // {
  15. // bin_tree_search_inorder(t->l, key);
  16. // // do check here
  17. // if (strstr(t->key, key) != 0)
  18. // printf("\nMatch: [%s]", t->key);
  19. // else
  20. // printf("\nDoesn't match: [%s]", t->key);
  21. // bin_tree_search_inorder(t->r, key);
  22. // } else
  23. // return;
  24. // }
  25.  
  26. void
  27. bin_tree_search_inorder(struct t *t, char *key)
  28. {
  29. int res;
  30. if (t == NULL)
  31. return;
  32. if (strstr(t->key, key) != 0)
  33. {
  34. printf("\nMatch: [%s]", t->key);
  35. bin_tree_search_inorder(t->l, key);
  36. bin_tree_search_inorder(t->r, key);
  37. } else {
  38. printf("\nDoesn't match: [%s]", t->key);
  39. if (strlen(t->key) >= strlen(key))
  40. {
  41. res = strcmp(key, t->key);
  42. if (res > 0)
  43. bin_tree_search_inorder(t->l, key);
  44. else
  45. bin_tree_search_inorder(t->r, key);
  46. }
  47. }
  48. }
  49.  
  50. int main(void) {
  51. struct t root, l, r, rl, rr, ll, lr;
  52. strcpy(&root.key, "tree");
  53. strcpy(&l.key, "traversal");
  54. strcpy(r.key, "trees");
  55. root.l = &l;
  56. root.r = &r;
  57. l.l = l.r = r.l = r.r = NULL;
  58. strcpy(rl.key, "tre");
  59. strcpy(rr.key, "tx");
  60. r.l = &rl;
  61. r.r = &rr;
  62. rl.l = rl.r = rr.l = rr.r = NULL;
  63. strcpy(ll.key, "ta");
  64. strcpy(lr.key, "travvv");
  65. l.l = &ll;
  66. l.r = &lr;
  67. ll.l = ll.r = lr.l = lr.r = NULL;
  68. bin_tree_search_inorder(&root, "tr");
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
Match: [tree]
Match: [traversal]
Doesn't match: [ta]
Match: [travvv]
Match: [trees]
Match: [tre]
Doesn't match: [tx]