Advertisement
huutho_96

Cây Nhị Phân

Dec 17th, 2015
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.44 KB | None | 0 0
  1. //https://www.facebook.com/CungHocLapTrinhUIT/
  2. #include <iostream>
  3. #include <queue>
  4. #include <conio.h>
  5.  
  6. using namespace std;
  7. struct TreeNode
  8. {
  9.     int key;
  10.     int value;
  11.     TreeNode *pLeft, *pRight;
  12. };
  13. typedef TreeNode* TREE;
  14. void CreateTree(TREE &Root)
  15. {
  16.     Root = NULL;
  17. }
  18. int InsertNode(TREE &Root, int key)
  19. {
  20.     if (Root)
  21.     {
  22.         if (Root->key == key)
  23.         {
  24.             Root->value++;
  25.             return 0;
  26.         }
  27.         if (Root->key > key) return InsertNode(Root->pLeft, key);
  28.         return InsertNode(Root->pRight, key);
  29.     }
  30.     TreeNode *p = new TreeNode;
  31.     if (p == NULL) return -1;
  32.     p->pLeft = p->pRight = NULL;
  33.     p->key = key;
  34.     p->value = 1;
  35.     Root = p;
  36.     return 1;
  37. }
  38. void NLR(TREE Root)
  39. {
  40.     if (Root)
  41.     {
  42.         cout << Root->key << endl;
  43.         NLR(Root->pLeft);
  44.         NLR(Root->pRight);
  45.     }
  46. }
  47. void LNR(TREE Root)
  48. {
  49.     if (Root)
  50.     {
  51.         LNR(Root->pLeft);
  52.         cout << Root->key << endl;
  53.         LNR(Root->pRight);
  54.     }
  55. }
  56. void LRN(TREE Root)
  57. {
  58.     if (Root)
  59.     {
  60.         LRN(Root->pLeft);
  61.         LRN(Root->pRight);
  62.         cout << Root->key << endl;
  63.     }
  64. }
  65. int SearchNode(TREE Root, int key)
  66. {
  67.     if (Root)
  68.     {
  69.         if (Root->key == key)
  70.             return 1;
  71.         if (Root->key > key) return SearchNode(Root->pLeft, key);
  72.         return SearchNode(Root->pRight, key);
  73.     }
  74.     return 0;
  75. }
  76. void DeleteTree(TREE &Root)
  77. {
  78.     if (Root)
  79.     {
  80.         DeleteTree(Root->pLeft);
  81.         DeleteTree(Root->pRight);
  82.         delete Root;
  83.     }
  84.     Root = NULL;
  85. }
  86. void CopyTree(TREE Root1, TREE &Root2)
  87. {
  88.     if (Root1)
  89.     {
  90.         TreeNode * p = new TreeNode;
  91.         if (p == NULL) exit(1);
  92.         p->pLeft = p->pRight = NULL;
  93.         p->key = Root1->key;
  94.         p->value = Root1->value;
  95.         Root2 = p;
  96.         CopyTree(Root1->pLeft, Root2->pLeft);
  97.         CopyTree(Root1->pRight, Root2->pRight);
  98.     }
  99. }
  100. void Output(TREE R)
  101. {
  102.     if (R)
  103.     {
  104.         Output(R->pRight);
  105.         cout << R->key << "\tXuat hien: " << R->value << endl;
  106.         Output(R->pLeft);
  107.     }
  108. }
  109. int Height(TREE Root)
  110. {
  111.     if (!Root)
  112.         return 0;
  113.     int a = Height(Root->pLeft);
  114.     int b = Height(Root->pRight);
  115.     if (a > b) return (a + 1);
  116.     else return (b + 1);
  117. }
  118. int DemNode(TREE Root)
  119. {
  120.     if (Root == NULL)
  121.         return 0;
  122.     return (1 + DemNode(Root->pLeft) + DemNode(Root->pRight));
  123. }
  124. void DemLa(TREE Root, int &count)
  125. {
  126.     if (Root)
  127.     {
  128.         if (Root->pLeft == NULL && Root->pRight == NULL) count++;
  129.         DemLa(Root->pLeft, count);
  130.         DemLa(Root->pRight, count);
  131.     }
  132. }
  133. void DemNode1CayCon(TREE Root, int &count)
  134. {
  135.     if (Root)
  136.     {
  137.         if (Root->pLeft != Root->pRight && (Root->pRight == NULL || Root->pLeft == NULL)) count++;
  138.         DemNode1CayCon(Root->pLeft, count);
  139.         DemNode1CayCon(Root->pRight, count);
  140.     }
  141. }
  142. void DemNode2CayCon(TREE Root, int &count)
  143. {
  144.     if (Root)
  145.     {
  146.         if (Root->pRight != NULL && Root->pLeft != NULL) count++;
  147.         DemNode2CayCon(Root->pLeft, count);
  148.         DemNode2CayCon(Root->pRight, count);
  149.     }
  150. }
  151. void DemNode_Muc(TREE Root, int muc, int &count)
  152. {
  153.     if (Root)
  154.     {
  155.         if (Height(Root) == muc) count++;
  156.         DemNode_Muc(Root->pLeft, muc, count);
  157.         DemNode_Muc(Root->pRight, muc, count);
  158.     }
  159. }
  160. int SNT(int n)
  161. {
  162.     if (n < 2) return 0;
  163.     for (int i = 2; i * i <= n; i++)
  164.         if (n % i == 0) return 0;
  165.     return 1;
  166. }
  167. void Dem_SNT(TREE Root, int &count)
  168. {
  169.     if (Root)
  170.     {
  171.         if (SNT(Root->key)) count++;
  172.         Dem_SNT(Root, count);
  173.         Dem_SNT(Root, count);
  174.     }
  175. }
  176. void MackDeletion(TREE &Root)
  177. {
  178.     TreeNode *p = Root;
  179.     if (Root->pLeft == NULL && Root->pRight == NULL)
  180.     {
  181.         Root = NULL;
  182.         delete p;
  183.         return;
  184.     }
  185.     if (Root->pLeft == NULL)
  186.     {
  187.         Root = p->pRight;
  188.         delete p;
  189.         return;
  190.     }
  191.     if (Root->pRight == NULL)
  192.     {
  193.         Root = p->pLeft;
  194.         delete p;
  195.         return;
  196.     }
  197.     TreeNode *pPtr = Root->pRight;
  198.     while (pPtr->pLeft) pPtr = pPtr->pLeft;
  199.     pPtr->pLeft = p->pLeft;
  200.     Root = p->pRight;
  201.     delete p;
  202.  
  203. }
  204. int Deletenode(TREE &Root, int x)
  205. {
  206.     if (Root)
  207.     {
  208.         if (Root->key == x)
  209.         {
  210.             MackDeletion(Root);
  211.             return 1;
  212.         }
  213.         if (Root->key > x) return Deletenode(Root->pLeft, x);
  214.         return Deletenode(Root->pRight, x);
  215.     }
  216.     return 0;
  217. }
  218. void Input(TREE &Root)
  219. {
  220.     int x;
  221.     do
  222.     {
  223.         cout << "Nhap x = 0 de thoat nhap:  x = ";
  224.         cin >> x;
  225.         if (x == 0) break;
  226.         InsertNode(Root, x);
  227.     } while (1);
  228. }
  229. void DuyetTheoMuc(TREE &Root)
  230. {
  231.     TREE p = Root;
  232.     queue<TREE> q;
  233.     if (Root == NULL) return;
  234.     q.push(p);
  235.     while (!q.empty())
  236.     {
  237.         p = q.front();
  238.         q.pop();
  239.         cout << p->key << "\t";
  240.         if (p->pLeft) q.push(p->pLeft);
  241.         if (p->pRight) q.push(p->pRight);
  242.     }
  243. }
  244.  
  245. void menu()
  246. {
  247.     system("cls");
  248.     cout << "1.\t Duyet cay" << endl;
  249.     cout << "2.\t Chen Node" << endl;
  250.     cout << "3.\t Xoa Node" << endl;
  251.     cout << "4.\t Dem Node" << endl;
  252.     cout << "5.\t So lan xuat hien khi chen" << endl;
  253.     cout << "6.\t Chieu cao cay" << endl;
  254.     cout << "7.\t Copy cay" << endl;
  255.     cout << "8.\t Xoa cay" << endl;
  256.     cout << "9.\t Tim Node" << endl;
  257.     cout << "10.\t Nhap cay" << endl;
  258.     cout << "0.\t Thoat" << endl;
  259.     cout << "Lua Chon: ";
  260. }
  261. void main()
  262. {
  263.     TREE Root;
  264.     CreateTree(Root);
  265.     int x;
  266.     int Value;
  267.     int dem = 0;
  268.     do
  269.     {
  270.         menu();
  271.         cin >> x;
  272.         switch (x)
  273.         {
  274.         case 1:
  275.             system("cls");
  276.             cout << "LNR:\n";
  277.             LNR(Root);
  278.             getch();
  279.             system("cls");
  280.             cout << "LRN:\n";
  281.             LRN(Root);
  282.             getch();
  283.             system("cls");
  284.             cout << "NLR:\n";
  285.             NLR(Root);
  286.             getch();
  287.             break;
  288.         case 2:
  289.             system("cls");
  290.             cout << "Nhap gia tri chen: ";
  291.             cin >> Value;
  292.             InsertNode(Root, Value);
  293.             getch();
  294.             break;
  295.         case 3:
  296.             system("cls");
  297.             cout << "Nhap gia tri xoa: ";
  298.             cin >> Value;
  299.             Deletenode(Root, Value);
  300.             getch();
  301.             break;
  302.         case 4:
  303.             system("cls");
  304.             cout << "So node: " << DemNode(Root) << endl;
  305.             dem = 0;
  306.             DemLa(Root, dem);
  307.             cout << "So node la: " << dem << endl;
  308.             dem = 0;
  309.             DemNode1CayCon(Root, dem);
  310.             cout << "So node 1 cay con: " << dem << endl;
  311.             dem = 0;
  312.             DemNode2CayCon(Root, dem);
  313.             cout << "So node 2 cay con: " << dem << endl;
  314.             getch();
  315.             break;
  316.         case 5:
  317.             system("cls");
  318.             Output(Root);
  319.             getch();
  320.             break;
  321.         case 6:
  322.             system("cls");
  323.             cout << "Chieu cao cay: " << Height(Root) << endl;
  324.             getch();
  325.             break;
  326.         case 7:
  327.             system("cls");
  328.             TREE Root2;
  329.             CreateTree(Root2);
  330.             CopyTree(Root, Root2);
  331.             LNR(Root2);
  332.             DeleteTree(Root2);
  333.             getch();
  334.             break;
  335.         case 8:
  336.             system("cls");
  337.             DeleteTree(Root);
  338.             getch();
  339.             break;
  340.         case 9:
  341.             system("cls");
  342.             cout << "Nhap gia tri can tim: ";
  343.             cin >> Value;
  344.             if (SearchNode(Root, Value)) cout << "Co gia tri " << Value << " trong cay\n";
  345.             else cout << "Khong co gia tri " << Value << " trong cay\n";
  346.             getch();
  347.             break;
  348.         case 10:
  349.             system("cls");
  350.             Input(Root);
  351.             break;
  352.         }
  353.     } while (x != 0);
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement