Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class node{
  9. public:
  10. int key;
  11. node *left, *right;
  12. char chars[256];
  13. };
  14. class node* root;
  15. class node* init(int key){
  16. class node* new_node=new node;
  17. new_node->key = key;
  18. new_node->left = NULL;
  19. new_node->right = NULL;
  20. for (int i = 0; i < 256;i++){
  21. new_node->chars[i] = (rand() % 26) + 97;
  22. }
  23. return new_node;
  24. }
  25.  
  26. void insert(int key)
  27. {
  28. class node* new_node=init(key);
  29. class node* parent = NULL;
  30. class node*tmp = root;
  31. new_node->left = NULL;
  32. new_node->right = NULL;
  33. new_node->key = key;
  34. while (tmp != NULL)
  35. {
  36. if (tmp->key = key)return;//w drzewie jest juz element o takim kluczu
  37. parent = tmp;
  38. if (tmp->key > key)tmp = tmp->left;
  39. else tmp = tmp->right;
  40. }
  41. if (root = NULL)
  42. root = new_node;
  43. else if (parent->key > key)parent->left = new_node;
  44. else parent->right = new_node;
  45. return;
  46. };
  47. void insertmany(int X){
  48. int key;
  49. if(X > 0){
  50. for (int i = 0; i < X; i++)
  51. {
  52. insert(key);
  53. }
  54.  
  55. }
  56. };
  57.  
  58. class node* find(int key){
  59. node *tmp;
  60. while (tmp && (key != tmp->key)){
  61. if (tmp->key < key)
  62. tmp = tmp->right;
  63. else
  64. tmp = tmp->left;
  65. }
  66. if (tmp)cout << "Zmaleziono element \n";
  67. else cout << "Nie znaleziono elementu \n";
  68. return tmp;
  69. };
  70. void deletee(int key){
  71. class node* parent = NULL;
  72. class node*tmp = root;
  73. while ((tmp != NULL) && (key != tmp->key))
  74. {
  75. parent = tmp;
  76. if (tmp->key < key)tmp = tmp->right;
  77. else tmp = tmp->left;
  78. }
  79. if (tmp = NULL) return;
  80. if (parent == nullptr)return;
  81. if ((tmp->right = NULL) && (tmp->left = NULL))//usuwany wezel tmp jest lisciem
  82. {
  83. if (tmp = root)
  84. {
  85. root = NULL;
  86. return;
  87. }
  88. if (parent->right = tmp)parent->right = NULL;
  89. else parent->left = NULL;
  90. return;
  91. }
  92. if (tmp->right = NULL)//usuwany wezel tmp ma tylko lewe podrzewo
  93. {
  94. if (parent->right = tmp)
  95. parent->right = tmp->left;
  96. else
  97. parent->left = tmp->left;
  98. return;
  99. }
  100. if (tmp->left = NULL)//wezel tmp ma tylko prawe podrzewo
  101. {
  102. if (parent->right = tmp)
  103. parent->right = tmp->right;
  104. else
  105. parent->left = tmp->right;
  106. return;
  107. }
  108. //usuwany wezel tmp ma oba poddrzewa
  109. class node* preparent = tmp;
  110. class node* child = tmp->left;
  111. while (child->right != NULL)
  112. {
  113. preparent = child;
  114. child = child->right;
  115. }
  116. if (child = tmp->left)//poprzedniok jest lewym potomkiem usuwanego wezla
  117. {
  118. if (parent->right = tmp)parent->right = child;
  119. else parent->left = child;
  120. return;
  121. }
  122. //poprzednik nie jest lewym potomkiem p, lecz jego wnukiem lub praprawnukiem
  123. class node*grandchild = child->left;//ewentualny potomek poprzednika
  124. //adopcja potomstwa poprzednika przez jego rodzica
  125. if (preparent->right = child)preparent->right = grandchild;
  126. else preparent->left = grandchild;
  127.  
  128. //adopcja potomstwa usuwanego wezle przez jego porzednika
  129. child->left = tmp->left;
  130.  
  131. //adopcja poprzednika przez rodzica usuwanego wezla
  132. if (parent->right = tmp)
  133. parent->right = child;
  134. else
  135. parent->left = child;
  136. return;
  137. };
  138.  
  139. void INORDER(node*tmp){
  140.  
  141. if (tmp->left != NULL)INORDER(tmp->left);
  142. cout << tmp->key;
  143. if (tmp->right != NULL)INORDER(tmp->right);
  144. };
  145. void PREORDER(node*tmp){
  146. cout << tmp->key;
  147. if (tmp->left != NULL)PREORDER(tmp->left);
  148. if (tmp->right != NULL)PREORDER(tmp->right);
  149. };
  150. void POSTORDER(node*tmp){
  151. if (tmp->left != NULL)POSTORDER(tmp->left);
  152. if (tmp->right != NULL)POSTORDER(tmp->right);
  153. cout << tmp->key;
  154. };
  155. int main()
  156. {
  157. int X, k1, k2, k3, k4;
  158. FILE* fp = fopen("inlab03.txt", "r");
  159. if (fp == NULL)
  160. return -1;
  161. fscanf(fp, "%d %d %d %d %d", &X, &k1, &k2, &k3, &k4);
  162. fclose(fp);
  163.  
  164.  
  165. clock_t begin, end;
  166. double time_spent;
  167. begin = clock();
  168.  
  169. root = NULL;
  170. deletee(k1);
  171. insert(k1);
  172. insert(k2);
  173. insert(k3);
  174. insert(k4);
  175. deletee(k1);
  176. find(k1);
  177. deletee(k2);
  178. deletee(k3);
  179. deletee(k4);
  180.  
  181.  
  182. end = clock();
  183. time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  184. getchar();
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement