fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct Node* Node;
  5. struct Node {
  6. int value;
  7. Node left;
  8. Node right;
  9. };
  10.  
  11. typedef struct Tree* Tree;
  12. struct Tree {
  13. Node root;
  14. };
  15.  
  16. void Tree_add(Tree tree, int value) {
  17. Node n;
  18. Node p;
  19.  
  20. n = malloc(sizeof(struct Node));
  21. n->value = value;
  22. n->left = NULL;
  23. n->right = NULL;
  24.  
  25. if (tree->root == NULL) {
  26. tree->root = n;
  27. } else {
  28. p = tree->root;
  29. while (1) {
  30. if (value < p->value) {
  31. if (p->left == NULL) {
  32. p->left = n;
  33. break;
  34. } else {
  35. p = p->left;
  36. }
  37. } else {
  38. if (p->right == NULL) {
  39. p->right = n;
  40. break;
  41. } else {
  42. p = p->right;
  43. }
  44. }
  45. }
  46. }
  47. }
  48.  
  49. void Tree_dump(Tree tree) {
  50.  
  51. }
  52.  
  53. int main(void) {
  54. Tree tree = malloc(sizeof(struct Tree));
  55. Tree_add(tree, 3);
  56. Tree_add(tree, 1);
  57. Tree_add(tree, 5);
  58. Tree_add(tree, 0);
  59. Tree_add(tree, 2);
  60. Tree_add(tree, 4);
  61. Tree_add(tree, 6);
  62. Tree_dump(tree);
  63. return EXIT_SUCCESS;
  64. }
  65.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty