Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct Node
- {
- int data;
- Node* pLeft;
- Node* pRight;
- };
- typedef Node* TREE;
- void createTREE(TREE& root)
- {
- root=NULL;
- }
- Node* createNode(int x)
- {
- Node* p= new Node;
- p->data=x;
- p->pLeft = p->pRight = NULL;
- return p;
- }
- void InsertNode(TREE& root, int x);
- //1. Nhap gia tri trong cay
- void Input(TREE& root);
- //2. Xuat gia tri NLR
- void Output_NLR(TREE root);
- //3. Xuat gia tri LNR
- void Output_LNR(TREE root);
- //4. Xuat gia tri LRN
- void Output_LRN(TREE root);
- //5. Tinh tong gia tri cac nut trong cay.
- int SumAllNodes(TREE root);
- //6. Tinh tong gia tri cac nut la trong cay.
- int SumAllLeaves(TREE root);
- //7. Tim gia tri lon nhat trong cay.
- int TreeMaxNumber(TREE root);
- //8. Tinh chieu cao cua cay.
- int TreeHeight(TREE root);
- //9. In ra cac nut o muc k cua cay theo LNR.
- void OutputLNR_levelK(TREE root, int k);
- //10. Tinh tong cac nut o muc k cua cay.
- int Sum_KthLevel_Nodes(TREE root, int k);
- //11. Dem so luong nut trong cay.
- int HowManyNodes(TREE root);
- //12. Dem so luong nut la trong cay.
- int HowManyLeaves(TREE root);
- //13. Dem so luong nut co dung 1 con trong cay.
- int HaveOneChild(TREE root);
- //14. Xuat ra gia tri va muc cua cac nut trong cay theo thu tu LNR.
- void Output_NodeAndIndex_LNR(TREE root, int level);
- //15. In ra muc cua nut co gia tri k. Neu khong co, xuat thong bao Khong co nut k.
- int GetLevel(TREE root, int data, int level);
- //17. Xoa mot node co gia tri k trong cay nhi phan.
- int LeftMostValue(TREE root);
- TREE DeleteNode(TREE& root, int k);
- int main()
- {
- TREE root;
- int choice, k;
- createTREE(root);
- cout << "\n1. Nhap gia tri trong cay.\n"
- << "2. Xuat gia tri trong cay theo thu tu NLR.\n"
- << "3. Xuat gia tri trong cay theo thu tu LNR.\n"
- << "4. Xuat gia tri trong cay theo thu tu LRN.\n"
- << "5. Tinh tong gia tri cac nut trong cay.\n"
- << "6. Tinh tong gia tri cac nut la trong cay.\n"
- << "7. Tim gia tri lon nhat trong cay.\n"
- << "8. Tinh chieu cao cua cay.\n"
- << "9. In ra cac nut o muc k cua cay theo LNR.\n"
- << "10. Tinh tong cac nut o muc k cua cay.\n"
- << "11. Dem so luong nut trong cay.\n"
- << "12. Dem so luong nut la trong cay.\n"
- << "13. Dem so luong nut co dung 1 con trong cay.\n"
- << "14. Xuat ra gia tri va muc cua cac nut trong cay theo thu tu LNR.\n"
- << "15. In ra muc cua nut co gia tri k. Neu khong co, xuat thong bao Khong co nut k.\n"
- << "16. Tim nut chua gia tri k va xuat ra muc, chieu cao, bac tai nut do. k la mot gia tri thuoc cay nhi phan.\n"
- << "17. Xoa mot node co gia tri k trong cay nhi phan.\n";
- do
- {
- cout << "\nNhap lua chon: ";
- cin>>choice;
- switch(choice)
- {
- case 1:
- Input(root);
- break;
- case 2:
- cout << "\nNLR: ";
- Output_NLR(root);
- break;
- case 3:
- cout << "\nLNR: ";
- Output_LNR(root);
- break;
- case 4:
- cout << "\nLRN: ";
- Output_LRN(root);
- break;
- case 5:
- cout<< "\nTong cac nut trong cay: " << SumAllNodes(root) << endl;
- break;
- case 6:
- cout<< "\nTong cac nut la trong cay: " << SumAllLeaves(root) << endl;
- break;
- case 7:
- cout << "\nGia tri Node lon nhat trong cay la: " << TreeMaxNumber(root) << endl;
- break;
- case 8:
- cout << "\nChieu cao cua cay: " << TreeHeight(root) <<endl;
- break;
- case 9:
- cout<<"\nNhap vao muc k: ";
- cin>>k;
- cout << "\nNode tai level k la: ";
- OutputLNR_levelK(root, k);
- break;
- case 10:
- cout<<"\nNhap vao muc k: ";
- cin>>k;
- if(k > TreeHeight(root) || k < 1)
- cout << "\nKhong ton tai muc " << k <<" nay trong cay." <<endl;
- else
- cout << "\nTong cac Node tai muc " << k << " la: " << Sum_KthLevel_Nodes(root, k) << endl;
- break;
- case 11:
- cout << "\nSo luong node trong cay: " << HowManyNodes(root) <<endl;
- break;
- case 12:
- cout << "\nSo luong node la trong cay: " << HowManyLeaves(root) <<endl;
- break;
- case 13:
- cout << "\nSo luong node chi co dung 1 con: " << HaveOneChild(root) << endl;
- break;
- case 14:
- cout << "\nGia tri va muc trong toan bo cay: \n";
- Output_NodeAndIndex_LNR(root,1);
- break;
- case 15:
- cout<< "\nNhap vao gia tri k: ";
- cin>>k;
- if(GetLevel(root,k,1) == 0)
- cout<< "\nKhong co gia tri " << k <<endl;
- else
- cout <<"\nGia tri " << k << " nam o muc " << GetLevel(root,k,1) - 1 << endl;
- break;
- case 16:
- cout<< "\nNhap vao gia tri k: ";
- cin>>k;
- cout << "\nGia tri " << k << " vua nhap nam o \n\t- Muc: " << GetLevel(root,k,1) - 1 << endl;
- cout << "\t- Chieu cao: " << GetLevel(root,k,1) <<"\n\t- Bac: " << GetLevel(root,k,1) << endl;
- break;
- case 17:
- cout<< "\nNhap vao gia tri k muon xoa: ";
- cin>>k;
- *root = *DeleteNode(root, k);
- break;
- default:
- break;
- }
- } while(choice>=1 && choice<=17);
- return 0;
- }
- void InsertNode(TREE& root, int x)
- {
- Node* p = createNode(x);
- if(root==NULL)
- root = p;
- else
- {
- if(x == root->data)
- return;
- else
- {
- if(x < (root->data))
- InsertNode(root->pLeft,x);
- else
- InsertNode(root->pRight,x);
- }
- }
- }
- //1. Nhap gia tri trong cay
- void Input(TREE& root)
- {
- int x;
- do
- {
- cout << "\nNhap vao gia tri Node (nhap 0 de ngung lai): ";
- cin >> x;
- if(x>0)
- InsertNode(root,x);
- } while(x>0);
- }
- //2. Xuat gia tri NLR
- void Output_NLR(TREE root)
- {
- if(root != NULL)
- {
- cout << root->data << "\t";
- Output_NLR(root->pLeft);
- Output_NLR(root->pRight);
- }
- }
- //3. Xuat gia tri LNR
- void Output_LNR(TREE root)
- {
- if(root != NULL)
- {
- Output_LNR(root->pLeft);
- cout << root->data << "\t";
- Output_LNR(root->pRight);
- }
- }
- //4. Xuat gia tri LRN
- void Output_LRN(TREE root)
- {
- if(root != NULL)
- {
- Output_LRN(root->pLeft);
- Output_LRN(root->pRight);
- cout << root->data << "\t";
- }
- }
- //5. Tinh tong gia tri cac nut trong cay.
- int SumAllNodes(TREE root)
- {
- if(root == NULL)
- return 0;
- return (root->data + SumAllNodes(root->pLeft) + SumAllNodes(root->pRight));
- }
- //6. Tinh tong gia tri cac nut la trong cay.
- int SumAllLeaves(TREE root)
- {
- if(root == NULL)
- return 0;
- if(root->pLeft == NULL && root->pRight == NULL)
- return root->data;
- return SumAllLeaves(root->pLeft) + SumAllLeaves(root->pRight);
- }
- //7. Tim gia tri lon nhat trong cay.
- int TreeMaxNumber(TREE root)
- {
- if(root == NULL)
- return 0;
- int res = root->data;
- int lres = TreeMaxNumber(root->pLeft);
- int rres = TreeMaxNumber(root->pRight);
- if (lres > res)
- res = lres;
- if (rres > res)
- res = rres;
- return res;
- }
- //8. Tinh chieu cao cua cay.
- int TreeHeight(TREE root)
- {
- if(root == NULL)
- return 0;
- int left = TreeHeight(root->pLeft);
- int right = TreeHeight(root->pRight);
- int res = (left > right) ? left : right;
- return 1 + res;
- }
- //9. In ra cac nut o muc k cua cay theo LNR.
- void OutputLNR_levelK(TREE root, int k)
- {
- if(k > TreeHeight(root))
- {
- cout<<"\nKhong ton tai muc " << k <<" cua cay.\n";
- return;
- }
- if(k < 1)
- return;
- if(k==1)
- cout << root->data << " ";
- OutputLNR_levelK(root->pLeft, k-1);
- OutputLNR_levelK(root->pRight, k-1);
- }
- //10. Tinh tong cac nut o muc k cua cay.
- int Sum_KthLevel_Nodes(TREE root, int k)
- {
- if(k > TreeHeight(root)||k < 1)
- {
- return 0;
- }
- if(k == 1)
- return root->data;
- return Sum_KthLevel_Nodes(root->pRight,k-1) + Sum_KthLevel_Nodes(root->pLeft, k-1);
- }
- //11. Dem so luong nut trong cay.
- int HowManyNodes(TREE root)
- {
- if(root == NULL)
- return 0;
- return 1 + HowManyNodes(root->pLeft) + HowManyNodes(root->pRight);
- }
- //12. Dem so luong nut la trong cay.
- int HowManyLeaves(TREE root)
- {
- if(root == NULL)
- return 0;
- if(root->pLeft == NULL && root->pRight == NULL)
- return 1;
- if(root->pLeft == NULL && root->pRight != NULL)
- return 0 + HowManyLeaves(root->pRight);
- if(root->pLeft != NULL && root->pRight == NULL)
- return 0 + HowManyLeaves(root->pLeft);
- return 0 + HowManyLeaves(root->pLeft) + HowManyLeaves(root->pRight);
- }
- //13. Dem so luong nut co dung 1 con trong cay.
- int HaveOneChild(TREE root)
- {
- if(root == NULL)
- return 0;
- if(root->pLeft == NULL && root->pRight != NULL)
- return 1 + HaveOneChild(root->pRight);
- if(root->pLeft != NULL && root->pRight == NULL)
- return 1 + HaveOneChild(root->pLeft);
- return 0 + HaveOneChild(root->pLeft) + HaveOneChild(root->pRight);
- }
- //14. Xuat ra gia tri va muc cua cac nut trong cay theo thu tu LNR.
- void Output_NodeAndIndex_LNR(TREE root, int level)
- {
- if(root != NULL)
- {
- Output_NodeAndIndex_LNR(root->pLeft, level+1);
- cout << root->data << "\t" << level << endl;
- Output_NodeAndIndex_LNR(root->pRight, level +1);
- }
- }
- //15. In ra muc cua nut co gia tri k. Neu khong co, xuat thong bao Khong co nut k.
- int GetLevel(TREE root, int data, int level)
- {
- if(root == NULL)
- return 0;
- if(root->data == data)
- return level;
- int left = GetLevel(root->pLeft, data, level+1);
- if(left != 0)
- return left;
- int right = GetLevel(root->pRight, data, level+1);
- return right;
- }
- void MakeNewTree_NLR(TREE& root, TREE p)
- {
- if(p != NULL)
- {
- cout <<"\nMake new tree " << p->data;
- InsertNode(root, p->data);
- MakeNewTree_NLR(root, p->pLeft);
- MakeNewTree_NLR(root, p->pRight);
- }
- }
- int LeftMostValue(TREE root)
- {
- while ( root->pLeft != NULL )
- root = root->pLeft;
- return root->data;
- }
- //17. Xoa mot node co gia tri k trong cay nhi phan.
- TREE DeleteNode(TREE& root, int k)
- {
- if(root == NULL)
- return root;
- if(k < root->data)
- root->pLeft = DeleteNode(root->pLeft, k);
- else if(k > root->data)
- root->pRight = DeleteNode(root->pRight, k);
- else
- {
- if(root->pLeft == NULL)
- {
- TREE newRoot = root->pRight;
- root = createNode(0);
- return newRoot;
- }
- if(root->pRight == NULL)
- {
- TREE newRoot = root->pLeft;
- root = createNode(0);
- return newRoot;
- }
- root->data = LeftMostValue(root->pRight);
- root->pRight = DeleteNode(root->pRight, root->data);
- }
- return root;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement