Advertisement
UniC

BTREE_FULL

Nov 30th, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5.  
  6. // Khai báo
  7. struct TNode
  8. {
  9.     int Info;
  10.     TNode *Left;
  11.     TNode *Right;
  12. };
  13.  
  14. struct BTree
  15. {
  16.     TNode *Root;
  17. };
  18.  
  19. // Khời tạo
  20. void InitBTree(BTree &bt)
  21. {
  22.     bt.Root = NULL;
  23. }
  24.  
  25. // Tạo TNode
  26. TNode *CreateTNode(int data)
  27. {
  28.     TNode *p = new TNode();
  29.     if(!p)
  30.     {
  31.         printf("\nMemory low");
  32.         getch();
  33.         return NULL;
  34.     }
  35.  
  36.     p ->Info = data;
  37.     p ->Left = NULL;
  38.     p ->Right = NULL;
  39.  
  40.     return p;
  41. }
  42.  
  43. // Show TNode
  44. void ShowTNode(TNode *p)
  45. {
  46.     printf("%d ", p ->Info);
  47. }
  48.  
  49. // Thêm TNode p vào Tree
  50. int InsertTNode(TNode *&root, TNode *p)
  51. {
  52.     // Kiểm tra bộ nhớ
  53.     if(!p)
  54.         return 0;
  55.  
  56.     // Nếu gốc rỗng
  57.     if(!root)
  58.     {
  59.         root = p;
  60.         return 1;
  61.     }
  62.  
  63.     // trùng
  64.     if(root ->Info == p ->Info)
  65.         return 0;
  66.     if(p ->Info < root ->Info)
  67.         InsertTNode(root -> Left, p); // thêm vào trái
  68.     else
  69.         InsertTNode(root -> Right, p);
  70.     return 1;
  71. }
  72.  
  73. void CreateAuToBTree(BTree &bt, int n)
  74. {
  75.     int data;
  76.     InitBTree(bt);
  77.     srand((unsigned)time(NULL));
  78.     for(int i = 0; i < n; i++)
  79.     {      
  80.         data = rand() % 100;
  81.         TNode *p = CreateTNode(data);
  82.         InsertTNode(bt.Root, p);       
  83.     }
  84. }
  85.  
  86. void NLR(TNode *p)
  87. {
  88.     if(!p)
  89.         return;
  90.     printf("%d ", p ->Info);
  91.     NLR(p ->Left);
  92.     NLR(p ->Right);
  93. }
  94.  
  95. void LNR(TNode *root)
  96. {
  97.     if(!root)
  98.         return;
  99.     LNR(root -> Left);
  100.     printf("%d ", root ->Info);
  101.     LNR(root -> Right);
  102. }
  103.  
  104. int IsLeaf(TNode *root)
  105. {
  106.     if(root ->Left || root ->Right)
  107.         return 0;
  108.     return 1;
  109. }
  110.  
  111. // Đếm số Node
  112. int CountNode(TNode *root)
  113. {
  114.     if(!root)
  115.         return 0;
  116.     int nl = CountNode(root -> Left);
  117.     int nr = CountNode(root -> Right);
  118.  
  119.     return (1 + nl + nr);
  120. }
  121.  
  122. // Đếm số nút lá
  123. int CountNodeLeaf(TNode *root)
  124. {
  125.     if(!root)
  126.         return 0;
  127.     if(!root -> Left && !root -> Right) // nếu là lá
  128.         return 1;
  129.     int nl = CountNodeLeaf(root -> Left);
  130.     int nr = CountNodeLeaf(root -> Right);
  131.  
  132.     return (nl + nr);
  133. }
  134.  
  135.  
  136. int CountNodeLeaf_v2(TNode *root)
  137. {
  138.     if(!root)
  139.         return 0;
  140.  
  141.     int nl = CountNodeLeaf(root -> Left);
  142.     int nr = CountNodeLeaf(root -> Right);
  143.  
  144.     if(!root -> Left && !root -> Right) // nếu là lá
  145.         return (1 + nl + nr);
  146.     return (nl + nr);
  147. }
  148.  
  149. // đếm nút trung gian
  150. int CountNodeMedium(TNode *root)
  151. {
  152.     if(!root)
  153.         return 0;
  154.     if(!root -> Left && !root -> Right) // nếu là lá
  155.         return 0;
  156.     int nl = CountNodeMedium(root -> Left);
  157.     int nr = CountNodeMedium(root -> Right);
  158.  
  159.     return (1 + nl + nr);
  160. }
  161.  
  162. // đếm nút có 2 con
  163. int CountNodeHaveTwoChild(TNode *root)
  164. {  
  165.     if(!root)
  166.         return 0;
  167.     if(!root ->Left || !root -> Right)
  168.         return 0;
  169.     int nl = CountNodeHaveTwoChild(root -> Left);  
  170.     int nr = CountNodeHaveTwoChild(root ->Right);
  171.  
  172.     return (1 + nl + nr);  
  173. }
  174.  
  175. // đếm nút có 2 con ver 2
  176. int CountNodeHaveTwoChild_v2(TNode *root)
  177. {
  178.     if(!root)
  179.         return 0;
  180.  
  181.     int nl = CountNodeHaveTwoChild_v2(root ->Left);
  182.     int nr = CountNodeHaveTwoChild_v2(root ->Right);
  183.  
  184.     if(root ->Left && root ->Right)
  185.         return (1 + nl + nr);
  186.     return nl + nr;
  187. }
  188.  
  189. // Đếm nút chỉ có 1 con
  190. int CountNodeHaveOnceChild(TNode *root)
  191. {
  192.     if(!root)
  193.         return 0;
  194.     int nl = CountNodeHaveOnceChild(root -> Left);
  195.     int nr = CountNodeHaveOnceChild(root -> Right);
  196.  
  197.     if(!root -> Left && root -> Right || root -> Left && !root -> Right)
  198.         return (1 + nl + nr);
  199.     return nl + nr;
  200. }
  201.  
  202.  
  203. // tính tổng các Node
  204. int SumAllnode(TNode *root)
  205. {
  206.     if(!root)
  207.         return 0;
  208.     int suml = SumAllnode(root ->Left);
  209.     int sumr = SumAllnode(root ->Right);
  210.  
  211.     return (root ->Info + suml + sumr);
  212. }
  213.  
  214. // Kiểm tra 1 nút có phải là nút lá
  215.  
  216.  
  217. // Đếm nút con là nút lá
  218. int CountNodeHaveChildIsLeaf(TNode *root)
  219. {
  220.     if(!root)
  221.         return 0;
  222.  
  223.     int nl = CountNodeHaveChildIsLeaf(root -> Left);
  224.     int nr = CountNodeHaveChildIsLeaf(root -> Right);
  225.  
  226.     if(IsLeaf(root ->Left) || IsLeaf(root ->Right))
  227.         return (1 + nl + nr);
  228.     return (nl + nr);
  229. }
  230.  
  231. int CalHightTree(TNode *root)
  232. {
  233.     if(!root)
  234.         return 0;
  235.     int hl = CalHightTree(root -> Left);
  236.     int hr = CalHightTree(root ->Right);
  237.  
  238.     if(hl > hr)
  239.         return 1 + hl;
  240.     return 1 + hr;
  241. }
  242.  
  243. // Tìm Node lớn nhất
  244. int NodeMax(TNode *root)
  245. {
  246.     TNode *p = root;
  247.     while(p -> Right)
  248.         p = p -> Right;
  249.     return p -> Info;
  250. }
  251.  
  252. int SumOfTree(TNode *root)
  253. {
  254.     if(!root)
  255.         return 0;
  256.     int sl = SumOfTree(root ->Left);
  257.     int sr = SumOfTree(root ->Right);
  258.  
  259.     return (root ->Info + sl + sr);
  260. }
  261.  
  262. // Xuất ra màn hình các nút ở mức K
  263. void ShowNodeOfLevelK(TNode *root, int k)
  264. {
  265.     if(!root)
  266.         return;
  267.     k--;
  268.     ShowNodeOfLevelK(root -> Left, k);
  269.     ShowNodeOfLevelK(root ->Right, k);
  270.  
  271.     if(k == 0)
  272.         printf("%d ", root -> Info);
  273. }
  274.  
  275. // Xuất ra màn hình các nút lá ở mức k
  276. void ShowNodeLeafOfLvK(TNode *root, int k)
  277. {
  278.     if(!root)
  279.         return;
  280.     k--;
  281.     ShowNodeLeafOfLvK(root ->Left, k);
  282.     ShowNodeLeafOfLvK(root -> Right, k);
  283.  
  284.     if(k == 0 && (!root -> Left && !root ->Right))
  285.         printf("%d ", root ->Info);
  286. }
  287.  
  288. int CountNodeOfLvK(TNode *root, int k)
  289. {
  290.     if(!root)
  291.         return 0;
  292.     k--;
  293.     int cl = CountNodeOfLvK(root ->Left, k);
  294.     int cr = CountNodeOfLvK(root ->Right, k);
  295.  
  296.     if(k == 0)
  297.         return (1 + cl + cr); // mỗi lần tìm thấy tăng lên 1.
  298.     return cl + cr; // ngược lại vẫn tiếp tiếp đếm cl, cr.   
  299. }
  300.  
  301. // Đếm Node ở mức k
  302. int CountNodeLeafOfLVK(TNode *root, int k)
  303. {
  304.     if(!root)
  305.         return 0;
  306.     k--;
  307.     int cl = CountNodeLeafOfLVK(root ->Left, k);
  308.     int cr = CountNodeLeafOfLVK(root ->Right, k);
  309.  
  310.     if(k == 0 && !root ->Left && !root ->Right)
  311.         return (1 + cl + cr);
  312.     return (cl + cr);
  313. }
  314.  
  315. int SumNodeLeafOfLVK(TNode *root, int k)
  316. {
  317.     if(!root)
  318.         return 0;
  319.     k--;
  320.     int sl = SumNodeLeafOfLVK(root ->Left, k);
  321.     int sr = SumNodeLeafOfLVK(root ->Right, k);
  322.  
  323.     if(k == 0 && !root ->Left && !root ->Right)
  324.         return (root ->Info + sl + sr);
  325.     return sl + sr;
  326. }
  327.  
  328. int TimNutCoGiatriGanXNhat(TNode *root, int x)
  329. {
  330.     if(!root)
  331.         return 0;
  332.  
  333.     int min = root ->Info;
  334.     int mindis = abs(x - min); // khoảng cách nhỏ nhất
  335.     while(root)
  336.     {
  337.         if(root ->Info == x)
  338.             return x;
  339.         if(abs(x - root ->Info) < mindis)
  340.         {
  341.             min = root ->Info;
  342.             mindis = abs(x - min);
  343.         }
  344.         if(x > root ->Info)
  345.             root = root ->Right;
  346.         else
  347.             root = root ->Left;
  348.     }
  349.     return min;
  350. }
  351.  
  352. void InRaNutCoChieuCao_Trai_3(TNode *root, int k)
  353. {
  354.     if(!root)
  355.         return;
  356.     k--;
  357.     InRaNutCoChieuCao_Trai_3(root -> Left, k);
  358.     if(k == 0)
  359.         printf("%d ", root -> Info);
  360. }
  361.  
  362. void InRaNutCoGiaTri_le_Co1Child(TNode *root)
  363. {
  364.     if(!root)
  365.         return;
  366.     InRaNutCoGiaTri_le_Co1Child(root ->Left);
  367.     InRaNutCoGiaTri_le_Co1Child(root ->Right);
  368.  
  369.     if(root ->Info % 2 && (root ->Left || root ->Right))
  370.         printf("%d ", root ->Info);
  371. }
  372.  
  373. int main()
  374. {              
  375.     int n;
  376.     do{
  377.         printf("\nNhap vao so nut cua cay: ");
  378.         scanf("%d", &n);
  379.     }while(n < 0);
  380.  
  381.     BTree bt;
  382.     CreateAuToBTree(bt, n);
  383.     printf("\nShow list: ");
  384.     NLR(bt.Root);
  385.  
  386.  
  387.     int child = CountNodeHaveTwoChild(bt.Root);
  388.     printf("\nCount node have two child: %d", child);
  389.  
  390.     int sum = SumAllnode(bt.Root);
  391.     printf("\nSum all Node: %d", sum);
  392.  
  393.     int node_count = CountNode(bt.Root);
  394.     printf("\nNode had been count:  %d", node_count);
  395.  
  396.     int leaf_count = CountNodeLeaf(bt.Root);
  397.     printf("\nLeaf had been count:  %d", leaf_count);
  398.  
  399.  
  400.  
  401.     int nodemedium_count = CountNodeMedium(bt.Root);
  402.     printf("\nMedium Node had been count:  %d", nodemedium_count);
  403.  
  404.     //int childisleaf_cout = CountNodeHaveChildIsLeaf(bt.Root);
  405.     //  printf("\nChild is Leaf Node had been count:  %d", childisleaf_cout);
  406.  
  407.     int hight_tree = CalHightTree(bt.Root);
  408.     printf("\nHight of tree: %d", hight_tree);
  409.  
  410.     printf("\nNut co chieu cao trai la 3: ");
  411.     InRaNutCoChieuCao_Trai_3(bt.Root, 3);
  412.  
  413.     printf("\nIn ra nut co gia tri le co it nhat 1 con: ");
  414.     InRaNutCoGiaTri_le_Co1Child(bt.Root);
  415.  
  416.     int k;
  417.     do
  418.     {  
  419.         printf("\nNhap vao chieu cao: ");
  420.         scanf("%d", &k);
  421.         printf("\nShow node level k = %d: ", k);
  422.         ShowNodeOfLevelK(bt.Root, k);
  423.  
  424.  
  425.         int count_lvk = CountNodeOfLvK(bt.Root, k);
  426.         printf("\nCount node of level k = %d: %d", k, count_lvk);
  427.        
  428.     }while(k != 0);
  429.  
  430.    
  431.  
  432.  
  433.     getch();
  434.     return 0;
  435. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement