Advertisement
ltdpaste

Binary tree

May 25th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. / This code is use for: Binary tree ~ Lê Đăng Dũng UIT K13
  2. /* 1. Tạo cây nhị phân tìm kiếm từ tập tin data.txt có cấu trúc như sau:
  3.    Gồm 1 dòng chứa 1 dãy các số nguyên cách nhau bởi khoảng trắng.
  4.    2. Tính tổng tất cả các số chính phương trên cây.
  5.    3. Viết hàm duyệt cây sao cho kết quả in ra là mảng giảm dần.
  6.    4. Nhập vào 1 giá trị x từ bàn phím. Viết hàm Tìm x xem có trong cây hay không?
  7.    5. Tìm số chính phương nhỏ nhất trên cây.
  8.    6. Kết quả được trình bày ra màn hình theo thứ tự:
  9.     - Câu 1: In cây ra màn hình theo LNR.
  10.     - Câu 2: In Tổng các số chính phương ra màn hình
  11.     - Câu 3: In Cây ra màn hình
  12.     - Câu 4:  In kết quả có hoặc không có x ra màn hình.
  13.     - Câu 5: In kết quả chính phương nhỏ nhất ra màn hình
  14. */
  15.  
  16. #include <iostream>
  17. #include <fstream>
  18. #include <stdlib.h>
  19. using namespace std;
  20.  
  21. typedef struct Node
  22. {
  23.     int data;
  24.     struct Node *pLeft;
  25.     struct Node *pRight;
  26. }NODE;
  27. typedef NODE *TREE;
  28.  
  29. void Init(TREE &t)
  30. {
  31.     t = NULL;
  32. }
  33.  
  34. NODE* CreateNode(int value)
  35. {
  36.     NODE *p = new NODE;
  37.     if (p == NULL)
  38.         return NULL;
  39.     p->data = value;
  40.     p->pLeft = p->pRight = NULL;
  41.     return p;
  42. }
  43.  
  44. void InsertNode(TREE &t, Node *p)
  45. {
  46.     if (t == NULL)
  47.     {
  48.         t = p;
  49.         t ->pLeft = t ->pRight = NULL;
  50.     }
  51.     else
  52.     {
  53.         if (t ->data > p ->data)
  54.         {
  55.             InsertNode(t ->pLeft, p);
  56.         }
  57.         else
  58.         {
  59.             InsertNode(t ->pRight, p);
  60.         }
  61.     }
  62. }
  63. void Input(const char *inpfile, TREE &t)
  64. {
  65.     ifstream inp;
  66.     inp.open(inpfile);
  67.     if (!inp.is_open())
  68.         cout << "Khong the mo file input.";
  69.     int x;
  70.     while (!inp.eof())
  71.     {
  72.         inp >> x;
  73.         InsertNode(t, CreateNode(x));
  74.     }
  75.     inp.close();
  76. }
  77.  
  78. void LNR(TREE t, int a[], int &i)
  79. {
  80.     if(t != NULL)
  81.     {
  82.         LNR(t ->pLeft, a, i);
  83.         a[i] = t ->data;
  84.         i++;
  85.         LNR(t ->pRight, a, i);
  86.     }
  87. }
  88. void InLNR(TREE t)
  89. {
  90.     if(t != NULL)
  91.     {
  92.         InLNR(t ->pLeft);
  93.         cout << t ->data << "   ";
  94.         InLNR(t ->pRight);
  95.     }
  96. }
  97. void RNL(TREE t)
  98. {
  99.     if (t != NULL)
  100.     {
  101.         RNL(t ->pRight);
  102.         cout << t ->data << " ";
  103.         RNL(t ->pLeft);
  104.     }
  105. }
  106. void Output(const char *outfile, TREE t)
  107. {
  108.     int a[100];
  109.     int i = 0;
  110.     LNR(t, a, i);
  111.     ofstream out;
  112.     out.open(outfile);
  113.     if (!out.is_open())
  114.     {
  115.         cout << "Khong the mo file.";
  116.     }
  117.     else
  118.     {
  119.         for (int j = 0; j < i; j++)
  120.         {
  121.             if (j < i)
  122.                 out << a[j] << " ";
  123.             else
  124.                 out << a[j];
  125.         }
  126.     }
  127. }
  128.  
  129. bool SCP(int n)
  130. {
  131.     int i = 0;
  132.     while (i*i <= n)
  133.     {
  134.         if (i*i == n)
  135.             return true;
  136.         else
  137.             i++;
  138.     }
  139.     return false;
  140. }
  141. void SCP_export(TREE t)
  142. {
  143.     if (t != NULL)
  144.     {
  145.         SCP_export(t->pLeft);
  146.         if (SCP(t ->data))
  147.             cout << t ->data << " ";
  148.         SCP_export(t ->pRight);
  149.     }
  150. }
  151.  
  152. void SCP_export_sum(TREE t, int b[], int &k)
  153. {
  154.     if (t != NULL)
  155.     {
  156.         SCP_export_sum(t->pLeft, b, k);
  157.         if (SCP(t ->data))
  158.         {
  159.             b[k] = t ->data;
  160.             k++;
  161.         }
  162.         SCP_export_sum(t ->pRight, b, k);
  163.     }
  164. }
  165. void SCP_min(TREE t)
  166. {
  167.  
  168. }
  169. NODE* searchNode(TREE t, int x)
  170. {
  171.     if (t)
  172.     {
  173.         if (t ->data == x)
  174.             return t;
  175.         if (t ->data > x)
  176.             return searchNode(t->pLeft, x);
  177.         else
  178.             return searchNode(t->pRight, x);
  179.     }
  180.     return NULL;
  181. }
  182.  
  183. //int Find_x(TREE t, int x)
  184. //{
  185. //    if (t != NULL)
  186. //    {
  187. //        Find_x(t ->pLeft, x);
  188. //        if (t ->data == x)
  189. //            return 1;
  190. //        Find_x(t ->pRight, x);
  191. //    }
  192. //    return -1;
  193. //}
  194. int main()
  195. {
  196.     TREE t;
  197.     int x, k = 0, tong = 0;
  198.     int b[100];
  199.     Init(t);
  200.     Input("input.txt", t);
  201.     InLNR(t);
  202.     Output("output.txt", t);
  203. //    cout << "Cac so chinh phuong: ";
  204. //    SCP_export(t);
  205.     SCP_export_sum(t, b, k);
  206.     for (int j = 0; j < k; j++)
  207.         tong += b[j];
  208.     cout << "\nTong cac so chinh phuong: " << tong << endl;
  209.     InLNR(t);
  210.     cout << "\nDuyet cay theo thu tu giam dan: ";
  211.     RNL(t);
  212.     cout << "\nMoi nhap x de tim, x=";
  213.     cin >> x;
  214.     if (searchNode(t, x))
  215.         cout << "Tim thay x.\n";
  216.     else
  217.         cout << "Khong tim thay x trong cay.\n";
  218.     cout << "So chinh phuong nho nhat: " << b[0];
  219.     return 0;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement