fork download
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. typedef struct node{
  5. int data;
  6. node *left;
  7. node *right;
  8. node *parent;
  9. } node;
  10.  
  11. void InorderTraversal(struct node *root){
  12. if(root != NULL){
  13. InorderTraversal(root->left);
  14. printf("%d ", root->data);
  15. InorderTraversal(root->right);
  16. }
  17. }
  18.  
  19. void TreeInsert(struct node *root, struct node *z){
  20. struct node *y = NULL;
  21. struct node *x = root;
  22.  
  23. while(x != NULL){
  24. y = x;
  25. if(z->data < x->data)
  26. x = x->left;
  27. else
  28. x = x->right;
  29. }
  30. z->parent = y;
  31. if(y == NULL)
  32. root = z;
  33. else{
  34. if(z->data < y->data)
  35. y->left = z;
  36. else
  37. y->right = z;
  38. }
  39. }
  40.  
  41. int main(void) {
  42. node *tree;
  43. node *p;
  44.  
  45. tree = NULL;
  46. for(int i = 0; i < 10; i++){
  47. p = (struct node *)malloc(sizeof(struct node));
  48. p->data = i;
  49. p->left = NULL;
  50. p->right = NULL;
  51. TreeInsert(tree, p);
  52. }
  53.  
  54. InorderTraversal(tree);
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 3408KB
stdin
Standard input is empty
stdout
Standard output is empty