Khóa: Đề Số: I Ngày thi:……./……./200 Thời gian làm bài: 90 phút
Khóa: Đề Số: I Ngày thi:……./……./200 Thời gian làm bài: 90 phút
P N THI MN: CU TRC D LIU (thi ly im cho phn l thuyt cui k) KHA: S: I Ngy thi:././200 Thi gian lm bi: 90 pht
-----o0o----p n
//Dap an de 1: #include "stdio.h" #include "malloc.h" typedef int T; typedef struct tagTreeNode { T key; struct tagTreeNode *left; struct tagTreeNode *right; }TreeNode; TreeNode *Tree(int n) { if(n==0) return NULL; else { int x; int nl, nr; nl = n/2; nr = n - nl - 1; TreeNode *pNew; pNew = (TreeNode*)malloc(sizeof(TreeNode)); printf("Enter value of node: "); scanf("%d",&x); pNew->key = x; pNew->left = Tree(nl); pNew->right = Tree(nr); return pNew; } } void XuatGiua(TreeNode *root) { if(root) { XuatGiua(root->left); printf("\t%d",root->key); XuatGiua(root->right); } }
//Cau a: Tim phan tu bat ky trong cay void TimPT(TreeNode *r, T x, bool &flag) { if(r) { if(r->key == x) { flag = true; return; } TimPT(r->left,x,flag); TimPT(r->right,x,flag); } }// cuoi cau a //cau b: Dem so phan tu ben trai cua cay void DemPT(TreeNode *r, int &dem) { if(r) { dem++; DemPT(r->left,dem); DemPT(r->right,dem); } } void DemPTTrai(TreeNode *r, int &soPTTrai) { if(r) DemPT(r->left,soPTTrai); } //cuoi cau b //cau c: Thay the (sua) gia tri phai nhat cua cay void ThaythePhai(TreeNode *&root, T gitrimoi) { TreeNode *r = root; if(r) { while(r->right) r = r->right; r->key = gitrimoi; } } //cuoi cau c //cau d: Xoa phan tu trai nhat cua cay void XoaTrai(TreeNode *&root) { TreeNode *r = root; if(r) { if(r->left == NULL) { root = r->right; r->right = NULL; free(r); return; } else { TreeNode *p = r; while(r->left) {
p = r; r = r->left; } p->left = r->right; r->right = NULL; free(r); } } }//cuoi cau d void main() { int n, x, soPTTrai; bool flag = false; TreeNode *root; root = NULL; printf("\nNhap so phan tu de tao cay: "); scanf("%d",&n); root = Tree(n); printf("\nCay vua tao:\n"); XuatGiua(root); printf("\n"); //----------------cau a: ---------------flag = false; printf("\nNhap phan tu can tim (so nguyen): "); scanf("%d",&x); TimPT(root,x,flag); if(flag == true) printf("\nCo %d trong cay.\n", x); else printf("\nKhong co %d trong cay.\n", x); //----------------cuoi cau a---------------//----------------cau b:-----------------soPTTrai = 0; DemPTTrai(root,soPTTrai); printf("\nSo phan tu ben trai cua cay: %d\n",soPTTrai); //----------------cuoi cau b--------------//cau c: T gitrimoi; printf("\nNhap gi tri moi de thay the gia tri phai nhat cua cay: "); scanf("%d",&gitrimoi); ThaythePhai(root,gitrimoi); printf("\nSau khi sua:\n"); XuatGiua(root); printf("\n"); //----------------cuoi cau c---------------//----------------cau d: ---------------XoaTrai(root); printf("\nSau khi xoa trai:\n"); XuatGiua(root); printf("\n"); //----------------cuoi cau d---------------} //cuoi de 1
I HC DN LP VN LANG ---------
P N THI MN: CU TRC D LIU (thi ly im cho phn l thuyt cui k) KHA: S: II Ngy thi:././200 Thi gian lm bi: 90 pht
-----o0o----p n
//Dap an de 2: #include "stdio.h" #include "malloc.h" typedef int T; typedef struct tagTreeNode { T key; struct tagTreeNode *left; struct tagTreeNode *right; }TreeNode; TreeNode *Tree(int n) { if(n==0) return NULL; else { int x; int nl, nr; nl = n/2; nr = n - nl - 1; TreeNode *pNew; pNew = (TreeNode*)malloc(sizeof(TreeNode)); printf("Enter value of node: "); scanf("%d",&x); pNew->key = x; pNew->left = Tree(nl); pNew->right = Tree(nr); return pNew; } } void XuatGiua(TreeNode *root) { if(root) { XuatGiua(root->left); printf("\t%d",root->key); XuatGiua(root->right); } }
//Cau a: Tim phan tu bat ky trong cay void TimPT(TreeNode *r, T x, bool &flag) { if(r) { if(r->key == x) { flag = true; return; } TimPT(r->left,x,flag); TimPT(r->right,x,flag); } }// cuoi cau a //cau b: Dem so phan tu ben phai cua cay void DemPT(TreeNode *r, int &dem) { if(r) { dem++; DemPT(r->left,dem); DemPT(r->right,dem); } } void DemPTPhai(TreeNode *r, int &soPTPhai) { if(r) DemPT(r->right,soPTPhai); } //cuoi cau b //cau c: Thay the (sua) gia tri trai nhat cua cay void ThaytheTrai(TreeNode *&root, T gitrimoi) { TreeNode *r = root; if(r) { while(r->left) r = r->left; r->key = gitrimoi; } } //cuoi cau c //cau d: Xoa phan tu phai nhat cua cay void XoaPhai(TreeNode *&root) { TreeNode *r = root; if(r) { if(r->right== NULL) { root = r->left; r->left = NULL; free(r); return; } else { TreeNode *p = r; while(r->right) {
p = r; r = r->right; } p->right = r->left; r->left = NULL; free(r); } } }//cuoi cau d void main() { int n, x, soPTPhai; bool flag = false; TreeNode *root; root = NULL; printf("\nNhap so phan tu de tao cay: "); scanf("%d",&n); root = Tree(n); printf("\nCay vua tao:\n"); XuatGiua(root); printf("\n"); //----------------cau a: ---------------flag = false; printf("\nNhap phan tu can tim (so nguyen): "); scanf("%d",&x); TimPT(root,x,flag); if(flag == true) printf("\nCo %d trong cay.\n", x); else printf("\nKhong co %d trong cay.\n", x); //----------------cuoi cau a---------------//----------------cau b: ---------------soPTPhai = 0; DemPTPhai(root,soPTPhai); printf("\nSo phan tu ben phai cua cay: %d\n",soPTPhai); //----------------cuoi cau b---------------//----------------cau c: ---------------T gitrimoi; printf("\nNhap gi tri moi de thay the gia tri trai nhat cua cay: "); scanf("%d",&gitrimoi); ThaytheTrai(root,gitrimoi); printf("\nSau khi sua:\n"); XuatGiua(root); printf("\n"); //----------------cuoi cau c---------------//----------------cau d: ---------------XoaPhai(root); printf("\nSau khi xoa phai:\n"); XuatGiua(root); printf("\n"); //----------------cuoi cau d---------------}//cuoi de 2