Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. #define LH -1
  6. #define EH 0
  7. #define RH 1
  8.  
  9. struct AVLNode
  10. {
  11. int x;
  12. char CSCB;
  13. AVLNode *pLeft;
  14. AVLNode *pRight;
  15. };
  16.  
  17. typedef AVLNode * AVLTree;
  18.  
  19. void CreateAVLTree(AVLTree &Root)
  20. {
  21. Root = NULL;
  22. }
  23.  
  24. AVLNode *CreateAVLNode(int x)
  25. {
  26. AVLNode *p = new AVLNode;
  27.  
  28. if (!p) exit(1);
  29.  
  30. p->CSCB = EH;
  31. p->x = x;
  32. p->pLeft = NULL;
  33. p->pRight = NULL;
  34.  
  35. return p;
  36. }
  37.  
  38. //cay con trai lech trai
  39. void Rotate_Left_Left(AVLTree &Root)
  40. {
  41. AVLNode *p;
  42.  
  43. p = Root->pLeft;
  44. Root->pLeft = p->pRight;
  45. p->pRight = Root;
  46.  
  47. switch (p->CSCB)
  48. {
  49. case LH:
  50. Root->CSCB = EH;
  51. p->CSCB = EH;
  52. break;
  53. case EH:
  54. p->CSCB = RH;
  55. Root->CSCB = LH;
  56. break;
  57. }
  58.  
  59. Root = p;
  60. }
  61.  
  62. //cay con phai lech phai
  63. void Rotate_Right_Right(AVLTree &Root)
  64. {
  65. AVLNode *p;
  66.  
  67. p = Root->pRight;
  68. Root->pRight = p->pLeft;
  69. p->pLeft = Root;
  70.  
  71. switch (p->CSCB)
  72. {
  73. case EH:
  74. Root->CSCB = RH;
  75. p->CSCB = EH;
  76. break;
  77. case RH:
  78. p->CSCB = EH;
  79. Root->CSCB = EH;
  80. break;
  81. }
  82.  
  83. Root = p;
  84. }
  85.  
  86. //cay con phai lech trai
  87. void Rotate_Right_Left(AVLTree &Root)
  88. {
  89. AVLNode *p1, *p2;
  90.  
  91. p1 = Root->pRight;
  92. p2 = p1->pLeft;
  93. Root->pRight = p2->pLeft;
  94. p1->pLeft = p2->pRight;
  95. p2->pLeft = Root;
  96. p2->pRight = p1;
  97.  
  98. switch (p2->CSCB)
  99. {
  100. case LH:
  101. Root->CSCB = EH;
  102. p1->CSCB = RH;
  103. break;
  104. case EH:
  105. Root->CSCB = EH;
  106. p1->CSCB = EH;
  107. break;
  108. case RH:
  109. Root->CSCB = LH;
  110. p1->CSCB = EH;
  111. break;
  112. }
  113.  
  114. p2->CSCB = EH;
  115. Root = p2;
  116. }
  117.  
  118. //cay con trai lech phai
  119. void Rotate_Left_Right(AVLTree &Root)
  120. {
  121. AVLNode *p1, *p2;
  122.  
  123. p1 = Root->pLeft;
  124. p2 = p1->pRight;
  125. Root->pLeft = p2->pRight;
  126. p1->pRight = p2->pLeft;
  127. p2->pRight = Root;
  128. p2->pLeft = p1;
  129.  
  130. switch (p2->CSCB)
  131. {
  132. case LH:
  133. p1->CSCB = EH;
  134. Root->CSCB = RH;
  135. break;
  136. case EH:
  137. Root->CSCB = EH;
  138. p1->CSCB = EH;
  139. break;
  140. case RH:
  141. Root->CSCB = EH;
  142. p1->CSCB = LH;
  143. break;
  144. }
  145.  
  146. p2->CSCB = EH;
  147. Root = p2;
  148. }
  149.  
  150. //Can bang khi cay lech trai
  151. int BalanceLeft(AVLTree &Root)
  152. {
  153. AVLNode *p;
  154.  
  155. p = Root->pLeft;
  156.  
  157. switch (p->CSCB)
  158. {
  159. case LH:
  160. Rotate_Left_Left(Root);
  161. return 2;
  162. case EH:
  163. Rotate_Left_Left(Root);
  164. return 1;
  165. case RH:
  166. Rotate_Left_Right(Root);
  167. return 2;
  168. }
  169. }
  170.  
  171. //can bang cay lech phai
  172. int BalanceRight(AVLTree &Root)
  173. {
  174. AVLNode *p;
  175.  
  176. p = Root->pRight;
  177.  
  178. switch (p->CSCB)
  179. {
  180. case RH:
  181. Rotate_Right_Right(Root);
  182. return 2;
  183. case EH:
  184. Rotate_Right_Right(Root);
  185. return 1;
  186. case LH:
  187. Rotate_Right_Left(Root);
  188. return 2;
  189. }
  190. }
  191.  
  192. //Chen 1 node vao cay AVL
  193. int InsertNode(AVLTree &Root, int x)
  194. {
  195. int Res;
  196. if (Root)
  197. {
  198. //gia tri da co trong cay
  199. if (Root->x == x) return 0;
  200.  
  201. //Root->x > x
  202. //chen vao ben trai
  203. if (Root->x > x)
  204. {
  205. Res = InsertNode(Root->pLeft, x);
  206. if (Res < 2) return Res;
  207.  
  208. //Res >= 2
  209. switch (Root->CSCB)
  210. {
  211. case RH:
  212. Root->CSCB = EH;
  213. return 1;
  214. case EH:
  215. Root->CSCB = LH;
  216. return 2;
  217. case LH:
  218. BalanceLeft(Root);
  219. return 1;
  220. }
  221. }
  222.  
  223. //Root->x < x
  224. //chen vao ben phai
  225. else
  226. {
  227. Res = InsertNode(Root->pRight, x);
  228.  
  229. if (Res < 2) return Res;
  230.  
  231. //Res >= 2
  232. switch (Root->CSCB)
  233. {
  234. case LH:
  235. Root->CSCB = EH;
  236. return 1;
  237. case EH:
  238. Root->CSCB = RH;
  239. return 2;
  240. case RH:
  241. BalanceRight(Root);
  242. return 1;
  243. }
  244. }
  245. }
  246.  
  247. Root = CreateAVLNode(x);
  248. return 2;
  249. }
  250.  
  251. //Tim node the mang
  252. int SearchStandFor(AVLTree &Root, AVLNode *&p)
  253. {
  254. int Res;
  255.  
  256. if (p->pLeft)
  257. {
  258. Res = SearchStandFor(Root, p->pLeft);
  259.  
  260. if (Res < 2) return Res;
  261.  
  262. switch (p->CSCB)
  263. {
  264. case LH:
  265. p->CSCB = EH;
  266. return 1;
  267. case EH:
  268. p->CSCB = RH;
  269. return 2;
  270. case RH:
  271. return BalanceRight(Root);
  272. }
  273. }
  274.  
  275. Root->x = p->x;
  276. Root = p;
  277. p = p->pRight;
  278. return 2;
  279. }
  280.  
  281. //Xoa 1 node tren cay
  282. int DelNode(AVLTree &Root, int x)
  283. {
  284. int Res;
  285.  
  286. //Khong ton tai node nay tren cay
  287. if (!Root) return 0;
  288.  
  289. //Qua duoc if tren tuc la ton tai
  290. //Root->x > x => Sang ben trai tim xoa
  291. if (Root->x > x)
  292. {
  293. Res = DelNode(Root->pLeft, x);
  294.  
  295. //Neu co xoa thi Res = 1 hoac 2. 2 tuc thay doi chieu cao cay
  296. if (Res < 2) return Res;
  297.  
  298. //Chieu cao bi thay doi
  299. switch (Root->CSCB)
  300. {
  301. case LH:
  302. Root->CSCB = EH;
  303. return 1;
  304. case EH:
  305. Root->CSCB = RH;
  306. return 2;
  307. case RH:
  308. return BalanceRight(Root);
  309. }
  310. }
  311.  
  312. if (Root->x < x)
  313. {
  314. Res = DelNode(Root->pRight, x);
  315.  
  316. if (Res < 2) return Res;
  317.  
  318. switch (Root->CSCB)
  319. {
  320. case LH:
  321. return BalanceLeft(Root);
  322. case EH:
  323. Root->CSCB = LH;
  324. return 1;
  325. case RH:
  326. Root->CSCB = EH;
  327. return 2;
  328. }
  329. }
  330. else
  331. {
  332. //Root->x = x
  333. AVLNode *p1 = Root;
  334.  
  335. if (Root->pLeft == NULL)
  336. {
  337. Root = Root->pRight;
  338. Res = 2;
  339. }
  340. else
  341. {
  342. if (Root->pRight == NULL)
  343. {
  344. Root = Root->pLeft;
  345. Res = 2;
  346. }
  347. else
  348. {
  349. Res = SearchStandFor(p1, Root->pRight);
  350.  
  351. if (Res < 2) return Res;
  352. switch (Root->CSCB)
  353. {
  354. case RH:
  355. Root->CSCB = EH;
  356. return 2;
  357. case EH:
  358. Root->CSCB = LH;
  359. return 1;
  360. case LH:
  361. return BalanceRight(Root);
  362. }
  363. }
  364. delete p1;
  365. return Res;
  366. }
  367. }
  368.  
  369. }
  370.  
  371. //Duyet theo muc
  372. void Level(AVLTree Root)
  373. {
  374. queue<AVLTree> q;
  375. AVLTree p;
  376.  
  377. if (Root == NULL) return;
  378.  
  379. p = Root;
  380. q.push(p);
  381.  
  382. while (!q.empty())
  383. {
  384. p = q.front();
  385. q.pop();
  386. cout << p->x << endl;
  387.  
  388. if (p->pLeft) q.push(p->pLeft);
  389. if (p->pRight) q.push(p->pRight);
  390. }
  391. }
  392.  
  393. void Input(AVLTree &Root)
  394. {
  395. int x;
  396. do
  397. {
  398. cout << "x = 0 de thoat: x = ";
  399. cin >> x;
  400. if (x == 0) break;
  401. InsertNode(Root, x);
  402. } while (1);
  403. }
  404.  
  405. void main()
  406. {
  407. AVLTree Root;
  408. CreateAVLTree(Root);
  409. InsertNode(Root, 866);
  410. InsertNode(Root, 790);
  411. InsertNode(Root, 879);
  412. InsertNode(Root, 777);
  413. InsertNode(Root, 864);
  414. InsertNode(Root, 870);
  415. InsertNode(Root, 900);
  416. InsertNode(Root, 475);
  417. InsertNode(Root, 863);
  418. InsertNode(Root, 865);
  419. InsertNode(Root, 867);
  420. InsertNode(Root, 875);
  421. InsertNode(Root, 888);
  422. InsertNode(Root, 915);
  423. DelNode(Root, 777);
  424. DelNode(Root, 790);
  425. Level(Root);
  426. system("pause");
  427. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement