Advertisement
Guest User

Untitled

a guest
May 20th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. #include "iostream"
  2. #include "stdio.h"
  3. #include "conio.h"
  4. #include "math.h"
  5. using namespace std;
  6. //---------------- Câu 1 ------------------------
  7. typedef struct tagTNode
  8. {
  9. int key;
  10. struct tagTNode *pLeft;
  11. struct tagTNode *pRight;
  12. }TNode;
  13. typedef TNode *Tree;
  14. void CreateTree(Tree &T)
  15. {
  16. T = NULL;
  17. }
  18. TNode *CreateTNode(int x)
  19. {
  20. TNode *p;
  21. p = new TNode;
  22. if (p == NULL)exit(1);
  23. else
  24. {
  25. p->key = x;
  26. p->pLeft = NULL;
  27. p->pRight = NULL;
  28. }
  29. return p;
  30. }
  31. int InsertNode(Tree &T, int x)
  32. {
  33. if (T)
  34. {
  35. if (T->key == x)return 0;
  36. if (T->key<x) return InsertNode(T->pRight, x);
  37. else if (T->key>x) return InsertNode(T->pLeft, x);
  38. }
  39. T = new TNode;
  40. if (T == NULL) return -1;
  41. T->key = x;
  42. T->pLeft = T->pRight = NULL;
  43. return 1;
  44. }
  45. void InsertTree(Tree &T)
  46. {
  47. int n = 11;
  48. int A[11] = { 50,31,71,20,45,60,90,10,55,65,100};
  49. for (int i = 0; i < n; i++)
  50. InsertNode(T, A[i]);
  51. }
  52. // ------------------- Câu 2 -------------------
  53. void LNR(Tree T)
  54. {
  55. if (T != NULL)
  56. {
  57. LNR(T->pLeft);
  58. cout << T->key << " ";
  59. LNR(T->pRight);
  60. }
  61. }
  62. void NLR(Tree T)
  63. {
  64. if (T != NULL)
  65. {
  66. cout << T->key << " ";
  67. NLR(T->pLeft);
  68. NLR(T->pRight);
  69. }
  70. }
  71. void RNL(Tree T)
  72. {
  73. if (T != NULL)
  74. {
  75. RNL(T->pRight);
  76. cout << T->key << " ";
  77. RNL(T->pLeft);
  78. }
  79. }
  80. //------------------- Câu 3 ----------------------
  81. TNode *SearchNode(Tree T, int x)
  82. {
  83. if (T != NULL)
  84. {
  85. if (T->key == x)return T;
  86. else if (T->key>x) return SearchNode(T->pLeft, x);
  87. else return SearchNode(T->pRight, x);
  88. }return NULL;
  89. }
  90. //-------------------- Câu 5-----------------------
  91. void demla(Tree T, int &la)
  92. {
  93. if (T)
  94. {
  95. if ((T->pLeft == NULL) && (T->pRight == NULL))la++;
  96. demla(T->pLeft, la);
  97. demla(T->pRight, la);
  98. }
  99. }
  100. void demtrai(Tree T, int &left)
  101. {
  102. if (T)
  103. {
  104. if ((T->pLeft != NULL) && (T->pRight == NULL))left++;
  105. demtrai(T->pLeft, left);
  106. demtrai(T->pRight, left);
  107. }
  108. }
  109. void demphai(Tree T, int &right)
  110. {
  111. if (T)
  112. {
  113. if ((T->pLeft == NULL) && (T->pRight != NULL))right++;
  114. demphai(T->pLeft, right);
  115. demphai(T->pRight, right);
  116. }
  117. }
  118. void demhaicon(Tree T, int &haicon)
  119. {
  120. if (T)
  121. {
  122. if ((T->pLeft != NULL) && (T->pRight != NULL))haicon++;
  123. demhaicon(T->pLeft, haicon);
  124. demhaicon(T->pRight, haicon);
  125. }
  126. }
  127. int nguyento(int n)
  128. {
  129. if (n == 1) return 0;
  130. float t = sqrt(float(n));
  131. for (int i = 2; i <= t; i++) if (n%i == 0)return 0;
  132. return 1;
  133. }
  134. void demsnt(Tree T, int &snt)
  135. {
  136. if (T)
  137. {
  138. if (nguyento(T->key) == 1)snt++;
  139. demsnt(T->pLeft, snt);
  140. demsnt(T->pRight, snt);
  141. }
  142. }
  143. //------------------- Câu 7 ------------------------------
  144. void thaythe(Tree &p, Tree&T)
  145. {
  146. if (T->pLeft != NULL) thaythe(p, T->pLeft);
  147. else
  148. {
  149. p->key = T->key;
  150. p = T;
  151. T = T->pRight;
  152. }
  153. }
  154. void deleteNode(Tree &T, int x)
  155. {
  156. if (T != NULL)
  157. {
  158. if (T->key<x)deleteNode(T->pRight, x);
  159. else
  160. {
  161. if (T->key>x)deleteNode(T->pLeft, x);
  162. else
  163. {
  164. TNode *p;
  165. p = T;
  166. if (T->pLeft == NULL)T = T->pRight;
  167. else
  168. {
  169. if (T->pRight == NULL)T = T->pLeft;
  170. else thaythe(p, T->pRight);
  171. }
  172. delete p;
  173. }
  174. }
  175. }
  176. else cout << "Khong tim thay node K de xoa";
  177.  
  178. }
  179. void menu()
  180. {
  181. cout << "\n\n---------------";
  182. cout << "\n0: Exit";
  183. cout << "\n1: Duyet NLR";
  184. cout << "\n2: Duyet LNR";
  185. cout << "\n3: Duyet RNL";
  186. cout << "\n4: Tim phan tu co khoa K tren cay";
  187. cout << "\n5: Dem cac nut la";
  188. cout << "\n6: Dem so node chi co cay con trai";
  189. cout << "\n7: Dem so node chi co cay con phai";
  190. cout << "\n8: Dem so node co 2 con";
  191. cout << "\n9: Dem cac so nguyen to trong cay";
  192. cout << "\n10: Xoa not K trong cay";
  193. }
  194. void main()
  195. {
  196. Tree T;
  197. int x;
  198. CreateTree(T);
  199. InsertTree(T);
  200. do
  201. {
  202. int c;
  203. menu();
  204. cout << "\n\nNhap lua chon: ";
  205. cin >> c;
  206. switch (c)
  207. {
  208. case 0: exit(1);
  209. case 1: {NLR(T); break; }
  210. case 2: {LNR(T); break; }
  211. case 3: {RNL(T); break; }
  212. case 4:
  213. {
  214. cout << "Nhap K can tim: ";
  215. cin >> x;
  216. if (SearchNode(T, x) != NULL)cout << "Co phan tu K trong cay";
  217. else cout << "Khong ton tai phan tu K trong cay";
  218. break;
  219. }
  220. case 5:
  221. {
  222. int la = 0;
  223. demla(T, la);
  224. cout << "So nut la trong cay la: " << la; break;
  225. }
  226. case 6:
  227. {
  228. int left = 0;
  229. demtrai(T, left);
  230. cout << "So nut la chi co cay con trai trong cay la: " << left; break;
  231. }
  232. case 7:
  233. {
  234. int right = 0;
  235. demtrai(T, right);
  236. cout << "So nut la chi co cay con phai trong cay la: " << right; break;
  237. }
  238. case 8:
  239. {
  240. int haicon = 0;
  241. demhaicon(T, haicon);
  242. cout << " So nut co 2 con trong cay la: " << haicon; break;
  243. }
  244. case 9:
  245. {
  246. int snt = 0;
  247. demsnt(T, snt);
  248. cout << "So node la so nguyen to trong cay la: " << snt;
  249. break;
  250. }
  251. case 10:
  252. {
  253. int x;
  254. cout << "Nhap gia tri node K can xoa: ";
  255. cin >> x;
  256. deleteNode(T, x);
  257. LNR(T);
  258. break;
  259. }
  260. }
  261. } while (1);
  262. _getch();
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement