Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6. struct tree_node
  7. {
  8. int key;
  9. int value;
  10. struct tree_node *left_child, *right_child;
  11. } *root;
  12.  
  13. void add_node(struct tree_node **root, int key, int value)
  14. {
  15. if(*root=NULL)
  16. {
  17. if((*root)->key<=key)
  18. add_node(&(*root)->left_child,key,value);
  19. else
  20. add_node(&(*root)->right_child,key,value);
  21. }
  22. else
  23. {
  24. *root = (struct tree_node *)malloc(sizeof(struct tree_node));
  25. if(*root){
  26. (*root)->key = key;
  27. (*root)->value = value;
  28. (*root)->left_child = (*root)->right_child = NULL;
  29. }
  30. }
  31. }
  32.  
  33. void print_inorder(struct tree_node *root)
  34. {
  35. if(root){
  36. print_inorder(root->left_child);
  37. printf("%d ",root->key);
  38. print_inorder(root->right_child);
  39. }
  40. }
  41.  
  42. void print_preorder(struct tree_node *root)
  43. {
  44. if(root){
  45. printf("%d ",root->key);
  46. print_preorder(root->left_child);
  47. print_preorder(root->right_child);
  48. }
  49. }
  50.  
  51. void print_postorder(struct tree_node *root)
  52. {
  53. if(root){
  54. print_postorder(root->left_child);
  55. print_postorder(root->right_child);
  56. printf("%d ",root->key);
  57. }
  58. }
  59.  
  60. void remove_tree_nodes(struct tree_node **root)
  61. {
  62. if(*root)
  63. {
  64. remove_tree_nodes(&(*root)->left_child);
  65. remove_tree_nodes(&(*root)->right_child);
  66. free(*root);
  67. *root = NULL;
  68. }
  69. }
  70.  
  71. void ilosc_wew(struct tree_node *root, int *number)
  72. {
  73. if(root)
  74. {
  75. if(root->left_child || root->right_child) (*number)++;
  76. ilosc_wew(root->left_child,number);
  77. ilosc_wew(root->right_child,number);
  78. }
  79. }
  80.  
  81. void ilosc_zew(struct tree_node *root, int *number)
  82. {
  83. if(root)
  84. {
  85. if(!(root->left_child) && !(root->right_child)) (*number)++;
  86. ilosc_zew(root->left_child,number);
  87. ilosc_zew(root->right_child,number);
  88. }
  89. }
  90.  
  91. void czy_pelne(struct tree_node *root, int *number)
  92. {
  93. if(root)
  94. {
  95. czy_pelne(root->left_child,number);
  96. (*number)++;
  97. czy_pelne(root->right_child,number);
  98. }
  99. }
  100.  
  101. int height(struct tree_node *root)
  102. {
  103. if(root == NULL) return 0;
  104. else
  105. {
  106. int lheight = height(root->left_child);
  107. int rheight = height(root->right_child);
  108. if(lheight>rheight)
  109. return lheight+1;
  110. else
  111. return rheight+1;
  112. }
  113. }
  114.  
  115. int main()
  116. {
  117. struct tree_node *root = NULL;
  118.  
  119. add_node(&root, 8, 8);
  120. add_node(&root, 5, 5);
  121. add_node(&root, 1, 1);
  122. add_node(&root, 6, 6);
  123. add_node(&root, 10, 10);
  124. add_node(&root, 9, 9);
  125. add_node(&root, 12, 12);
  126. add_node(&root, 9, 9);
  127.  
  128. printf("Inorder : "); print_inorder(root);
  129.  
  130. puts(" ");
  131.  
  132. int wysokosc = height(root);
  133.  
  134. printf("\nWysokosc drzewa : %d \n", wysokosc);
  135.  
  136.  
  137. int licznik = 0;
  138. ilosc_wew(root, &licznik);
  139.  
  140. printf("\n\nWewnetrznych wezlow : %d \n", licznik);
  141.  
  142. licznik = 0;
  143. ilosc_zew(root, &licznik);
  144.  
  145. printf("Zewnetrznych wezlow : %d \n", licznik);
  146.  
  147.  
  148. int potega = (int)pow(2.0,height(root));
  149.  
  150. licznik = 0;
  151. czy_pelne(root, &licznik);
  152.  
  153. if(licznik == potega-1) puts("\nDrzewo jest pelne");
  154. else puts("\nDrzewo nie jest pelne");
  155.  
  156. remove_tree_nodes(&root);
  157.  
  158. return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement