Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- / This code is use for: Binary tree ~ Lê Đăng Dũng UIT K13
- /* 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:
- Gồm 1 dòng chứa 1 dãy các số nguyên cách nhau bởi khoảng trắng.
- 2. Tính tổng tất cả các số chính phương trên cây.
- 3. Viết hàm duyệt cây sao cho kết quả in ra là mảng giảm dần.
- 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?
- 5. Tìm số chính phương nhỏ nhất trên cây.
- 6. Kết quả được trình bày ra màn hình theo thứ tự:
- - Câu 1: In cây ra màn hình theo LNR.
- - Câu 2: In Tổng các số chính phương ra màn hình
- - Câu 3: In Cây ra màn hình
- - Câu 4: In kết quả có hoặc không có x ra màn hình.
- - Câu 5: In kết quả chính phương nhỏ nhất ra màn hình
- */
- #include <iostream>
- #include <fstream>
- #include <stdlib.h>
- using namespace std;
- typedef struct Node
- {
- int data;
- struct Node *pLeft;
- struct Node *pRight;
- }NODE;
- typedef NODE *TREE;
- void Init(TREE &t)
- {
- t = NULL;
- }
- NODE* CreateNode(int value)
- {
- NODE *p = new NODE;
- if (p == NULL)
- return NULL;
- p->data = value;
- p->pLeft = p->pRight = NULL;
- return p;
- }
- void InsertNode(TREE &t, Node *p)
- {
- if (t == NULL)
- {
- t = p;
- t ->pLeft = t ->pRight = NULL;
- }
- else
- {
- if (t ->data > p ->data)
- {
- InsertNode(t ->pLeft, p);
- }
- else
- {
- InsertNode(t ->pRight, p);
- }
- }
- }
- void Input(const char *inpfile, TREE &t)
- {
- ifstream inp;
- inp.open(inpfile);
- if (!inp.is_open())
- cout << "Khong the mo file input.";
- int x;
- while (!inp.eof())
- {
- inp >> x;
- InsertNode(t, CreateNode(x));
- }
- inp.close();
- }
- void LNR(TREE t, int a[], int &i)
- {
- if(t != NULL)
- {
- LNR(t ->pLeft, a, i);
- a[i] = t ->data;
- i++;
- LNR(t ->pRight, a, i);
- }
- }
- void InLNR(TREE t)
- {
- if(t != NULL)
- {
- InLNR(t ->pLeft);
- cout << t ->data << " ";
- InLNR(t ->pRight);
- }
- }
- void RNL(TREE t)
- {
- if (t != NULL)
- {
- RNL(t ->pRight);
- cout << t ->data << " ";
- RNL(t ->pLeft);
- }
- }
- void Output(const char *outfile, TREE t)
- {
- int a[100];
- int i = 0;
- LNR(t, a, i);
- ofstream out;
- out.open(outfile);
- if (!out.is_open())
- {
- cout << "Khong the mo file.";
- }
- else
- {
- for (int j = 0; j < i; j++)
- {
- if (j < i)
- out << a[j] << " ";
- else
- out << a[j];
- }
- }
- }
- bool SCP(int n)
- {
- int i = 0;
- while (i*i <= n)
- {
- if (i*i == n)
- return true;
- else
- i++;
- }
- return false;
- }
- void SCP_export(TREE t)
- {
- if (t != NULL)
- {
- SCP_export(t->pLeft);
- if (SCP(t ->data))
- cout << t ->data << " ";
- SCP_export(t ->pRight);
- }
- }
- void SCP_export_sum(TREE t, int b[], int &k)
- {
- if (t != NULL)
- {
- SCP_export_sum(t->pLeft, b, k);
- if (SCP(t ->data))
- {
- b[k] = t ->data;
- k++;
- }
- SCP_export_sum(t ->pRight, b, k);
- }
- }
- void SCP_min(TREE t)
- {
- }
- NODE* searchNode(TREE t, int x)
- {
- if (t)
- {
- if (t ->data == x)
- return t;
- if (t ->data > x)
- return searchNode(t->pLeft, x);
- else
- return searchNode(t->pRight, x);
- }
- return NULL;
- }
- //int Find_x(TREE t, int x)
- //{
- // if (t != NULL)
- // {
- // Find_x(t ->pLeft, x);
- // if (t ->data == x)
- // return 1;
- // Find_x(t ->pRight, x);
- // }
- // return -1;
- //}
- int main()
- {
- TREE t;
- int x, k = 0, tong = 0;
- int b[100];
- Init(t);
- Input("input.txt", t);
- InLNR(t);
- Output("output.txt", t);
- // cout << "Cac so chinh phuong: ";
- // SCP_export(t);
- SCP_export_sum(t, b, k);
- for (int j = 0; j < k; j++)
- tong += b[j];
- cout << "\nTong cac so chinh phuong: " << tong << endl;
- InLNR(t);
- cout << "\nDuyet cay theo thu tu giam dan: ";
- RNL(t);
- cout << "\nMoi nhap x de tim, x=";
- cin >> x;
- if (searchNode(t, x))
- cout << "Tim thay x.\n";
- else
- cout << "Khong tim thay x trong cay.\n";
- cout << "So chinh phuong nho nhat: " << b[0];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement