Advertisement
leonard007

Untitled

Oct 17th, 2022
1,182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct node
  4. {
  5. int value;
  6. struct node *left_child, *right_child;
  7. };
  8. struct node *new_node(int value)
  9. {
  10. struct node *tmp = (struct node *)malloc(sizeof(struct node));
  11. tmp->value = value;
  12. tmp->left_child = tmp->right_child = NULL;
  13. return tmp;
  14. }
  15. void print(struct node *root_node) // displaying the nodes!
  16. {
  17. if (root_node != NULL)
  18. {
  19. print(root_node->left_child);
  20. printf("%d \n", root_node->value);
  21. print(root_node->right_child);
  22. }
  23. }
  24. struct node* insert_node(struct node* node, int value) // inserting nodes!
  25. {
  26. if (node == NULL) return new_node(value);
  27. if (value < node->value)
  28. {
  29. node->left_child = insert_node(node->left_child, value);
  30. }
  31. else if (value > node->value)
  32. {
  33. node->right_child = insert_node(node->right_child, value);
  34. }
  35. return node;
  36. }
  37. void afisarePreordine(struct node *root)
  38. {
  39.     if(root != NULL)
  40.     {
  41.     printf("%d ",root->value);
  42.     afisarePreordine(root->left_child);
  43.     afisarePreordine(root->right_child);
  44.     }
  45. }
  46.  
  47. void afisareInordine(struct node *root)
  48. {
  49.     if(root != NULL)
  50.     {
  51.     afisareInordine(root->left_child);
  52.     printf("%d ",root->value);
  53.     afisareInordine(root->right_child);
  54.     }
  55. }
  56.  
  57. int main()
  58. {
  59.     struct node *root_node = NULL;
  60.     root_node = insert_node(root_node, 10);
  61.     insert_node(root_node, 10);
  62.     insert_node(root_node, 30);
  63.     insert_node(root_node, 25);
  64.     insert_node(root_node, 36);
  65.     insert_node(root_node, 56);
  66.     insert_node(root_node, 78);
  67.     print(root_node);
  68.  
  69.     printf("Afisare preordine");
  70.     printf("\n");
  71.     afisarePreordine(root_node);
  72.     printf("Afisare inordine");
  73.     afisareInordine(root_node);
  74. return 0;
  75. }
  76.  
Tags: AA 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement