Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "conio.h"
  3. #include <iostream>
  4. #include <fstream>
  5. using namespace std;
  6. struct NODE
  7. {
  8. char Key;
  9. int count;
  10. NODE *pLeft;
  11. NODE *pRight;
  12. };
  13. void NhapVanBan(FILE* fp)
  14. {
  15. char c=fgetc(fp);
  16. //ofstream f;
  17. fp=fopen("input.txt","rt");
  18. int i = 0;
  19. while(!feof(fp))
  20. {
  21. c = fgetc(fp);
  22.  
  23. while ((c >= 'a' && c <= 'z')||(c>='A' && c<='Z') || (c>='0' && c<='9'))
  24. {
  25. c = fgetc(fp);
  26. }
  27. }
  28. }
  29. /*void NhapVanBan1()
  30. {
  31. fstream file;
  32. char* file_name = (char*)"input.txt";
  33. FILE* f = fopen(file_name, "r");
  34. //if (f == NULL) cout<<"file not found"<<endl;
  35. file.open(f);
  36. char* a;
  37. //readFile(f, a);
  38. if(file.is_open() == false)
  39. {
  40. file.close();
  41. return;
  42. }
  43.  
  44. for(int i=0;; i++)
  45. {
  46. file>>a[i];
  47. if(a[i]=='\0') break;
  48. }
  49. file.close();
  50. cout<<"Khong mo duoc file"<<endl;
  51. }*/
  52.  
  53. void XuatKetQua()
  54. {
  55. }
  56. void Init(NODE *&TREE)
  57. {
  58. TREE = NULL;
  59. }
  60. void Insert(NODE *&pRoot, char x)
  61. {
  62. if (pRoot == NULL)
  63.  
  64. {
  65. NODE *q;
  66. q = new NODE;
  67. q->Key = x;
  68. if (q->count==NULL)
  69. q->count =1;
  70. else
  71. q->count = q->count + 1;
  72. q->pLeft = q->pRight = NULL;
  73. pRoot = q;
  74. }
  75. else
  76. {
  77. if (x < pRoot->Key)
  78. Insert (pRoot->pLeft, x);
  79. else if (x > pRoot->Key)
  80. Insert (pRoot->pRight, x);
  81. }
  82. }
  83. void CreateTreeFile(NODE *&pRoot, char *a)
  84. {
  85. int Data;
  86. int i=0;
  87. do
  88. {
  89. Data = a[i];
  90. Insert(pRoot, Data);
  91. i++;
  92. }
  93. while (a[i]=='\0');
  94. }
  95. void CreateTree(NODE *&pRoot)
  96. {
  97. int Data;
  98. do
  99. {
  100. printf("Nhap vao du lieu, -1 de ket thuc: ");
  101. scanf("%d", &Data);
  102. if (Data == -1)
  103. break;
  104. //for (
  105. Insert(pRoot, Data);
  106. }
  107. while(1);
  108. }
  109. void NLR(NODE* pTree)
  110. {
  111. if(pTree != NULL)
  112. {
  113. printf("%4d", pTree->Key);
  114. NLR(pTree->pLeft);
  115. NLR(pTree->pRight);
  116. }
  117. }
  118. int SumTree(NODE* pTree)
  119. {
  120. if (pTree!= NULL)
  121. return 0;
  122. else
  123. {
  124. return (pTree->Key + SumTree(pTree->pLeft) + SumTree(pTree->pRight));
  125. }
  126. }
  127. int CountNode(NODE* pTree)
  128. { //Ham nay de dem co bao nhieu nut trong cay
  129. if(pTree == NULL) return 0;
  130. else
  131. return 1+ CountNode(pTree->pLeft) +CountNode(pTree->pRight);
  132. }
  133.  
  134. int MaxTree(NODE* pTree)
  135. {
  136. if (pTree->pRight ==NULL)
  137. return pTree->Key;
  138. else
  139. return MaxTree(pTree->pRight);
  140. }
  141. int MinTree(NODE* pTree)
  142. {
  143. if (pTree->pLeft ==NULL)
  144. return pTree->Key;
  145. else
  146. return MinTree(pTree->pLeft);
  147. }
  148.  
  149. int DemTree(NODE* Tree, char a)// so luong nut co du lieu bang a
  150. {
  151. if(Tree == NULL) return 0;
  152. else
  153. if(Tree->Key == a)
  154. return 1;
  155. else
  156. return DemTree(Tree->pLeft, a)+DemTree(Tree->pRight, a);
  157. }
  158.  
  159. NODE* Search(NODE* pRoot, char x)
  160. {
  161. if(pRoot == NULL)
  162. return NULL;
  163. if(x < pRoot->Key)
  164. Search(pRoot->pLeft, x);
  165. else
  166. if(x > pRoot->Key)
  167. Search(pRoot->pRight, x);
  168. else
  169. {
  170. return pRoot;
  171. }
  172. }
  173. int Height(NODE* pNode)
  174. {
  175. if(pNode == NULL)
  176. return 0;
  177. int HL, HR;
  178. HL = Height(pNode->pLeft);
  179. HR = Height(pNode->pRight);
  180. if(HL > HR)
  181. return (1 + HL);
  182. return (1 + HR);
  183. }
  184. void SearchStandFor(NODE* &Tree, NODE* &q)
  185. {
  186. if (Tree->pRight)
  187. SearchStandFor(Tree->pRight,q);
  188. else
  189. {
  190. q->Key = Tree->Key;
  191. q = Tree;
  192. Tree = Tree->pLeft;
  193. }
  194. }
  195. void RemoveNode(NODE* &Tree, char x)
  196. {
  197. NODE* p;
  198. if(Tree == NULL)
  199. printf("%d khong co trong cay", x);
  200. else
  201. {
  202. if (x < Tree->Key)
  203. RemoveNode(Tree->pLeft,x);
  204. else
  205. if (x > Tree->Key)
  206. RemoveNode(Tree->pRight,x);
  207. else
  208. {
  209. p = Tree;
  210. if(p->pRight == NULL)
  211. Tree = p->pLeft;
  212. else
  213. if (p->pLeft == NULL)
  214. Tree = p->pRight;
  215. else
  216. {
  217. SearchStandFor(Tree->pLeft,p);
  218. }
  219. delete p;
  220. }
  221. }
  222. }
  223. void main()
  224. {
  225. NODE* pTree, *p;
  226. char x;
  227. char a[100];
  228.  
  229. char* file_name = (char*)"input.txt";
  230. FILE* f = fopen(file_name, "r");
  231. NhapVanBan(f);
  232. Init(pTree);
  233. CreateTreeFile(pTree,a);
  234. NLR(pTree);
  235. printf("\nNhap vao 1 gia tri de tim: ");
  236. scanf("%d", &x);
  237. p = Search(pTree, x);
  238. if(p != NULL)
  239. {
  240. printf ("%d %d co xuat hien trong cay.\n", x, DemTree(p,x));
  241. printf("Chieu cao cua nut %d la %d\n", x, Height(p));
  242. RemoveNode(pTree, x);
  243. NLR(pTree);
  244. }
  245. else
  246. printf("%d khong co trong cay.\n", x);
  247.  
  248. //getch();
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement