You are on page 1of 100

Câu 1:

Viết chương trình bằng ngôn ngữ lập trình C thực hiện các yêu cầu sau:
1. Tạo cây nhị phân tìm kiếm bằng cách truyền mảng các nút như hình bên dưới. (trường khóa của mỗi nút trên cây là một số
nguyên)

2. Duyệt cây theo thứ tự giữa.


3. Tìm và cho biết đường đi đến nút là số chính phương lớn nhất trên cây nhị phân tìm kiếm.
4. Tìm tất cả các nút trên cây có tổng giá trị của các nút con bên trái bằng tổng giá trị của các nút con bên phải

10

6 30

2 9 12 37

25

Ví dụ:
2. Nút là số chính phương lớn nhất : 25. Đường đi: 10 30 12 25
3. Nút thỏa điều kiện : 30
#include <stdio.h>
#include <stdlib.h> struct node *createTree(int *arr, int n) {
#include <stdbool.h> struct node *root = NULL;
#include <math.h> for (int i = 0; i < n; i++) {
insertNode(&root, arr[i]);
}
struct node { return root;
int data; }
struct node *left;
struct node *right; void inorderTraversal(struct node *root) {
}; if (root == NULL) {
return;
struct node *createNode(int data) { }
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = data; inorderTraversal(root->left);
newNode->left = NULL; printf("%d ", root->data);
newNode->right = NULL; inorderTraversal(root->right);
return newNode; }
}
bool scp(int n){
void insertNode(struct node **root, int data) { int sqr = sqrt(n);
if (*root == NULL) { return (sqr*sqr == n);
*root = createNode(data); }
return;
} int main() {

if (data < (*root)->data) { int arr[] = {10, 6, 9, 2, 30, 12, 37, 25};
insertNode(&(*root)->left, data); int n = sizeof(arr) / sizeof(arr[0]);
} else {
insertNode(&(*root)->right, data); struct node *root = createTree(arr, n);
}
}
printf("Duyet cay theo thu tu giua: ");
inorderTraversal(root);
printf("\n");
int max = 0;

for(int x = 0;x < n; x++){


if(scp(arr[x])){
if(arr[x] > max)
max = arr[x];
}
}
printf("\nSo chinh phuong lon nhat la: %d", max);

return 0;
}

Câu 2:
Giả sử ta đã có danh sách liên kết đơn với cấu trúc dữ liệu như sau:
+ Định nghĩa một nút:
struct ttNode
{
int key; // Chứa số nguyên
int dem; // Chứa số lần xuất hiện của key
struct ttNode *Next;
}Node;
+ Định nghĩa một danh sách liên kết đơn: typedef struct ttList
{
Node *Head;
Node *Tail;
}List;
a. Viết hàm phát sinh ngẫu nhiên 100 số trong khoảng từ 1->1000
b. Viết hàm tạo danh sách liên kết đơn từ 100 số nguyên được phát sinh ở trên với điều kiện các phần tử không trùng nhau :
i. Thuộc tính dem của một node cho biết số lần xuất hiện của số đó.
ii. Cho biết phần tử xuất hiện nhiều nhất trong danh sách.
iii. Cài đặt giải thuật sắp xếp SelectionSort cho danh sách trên.

#include <iostream>
#include <stdlib.h> //a. Viet ham phat sinh ngau nhien 100 so trong khoang tu 1->1000
#include <time.h> int *arr;
void ngaunhien100(int *arr)
using namespace std; {

struct ttNode srand(time(NULL));


{
int key; for (int i = 0; i < 100; i++) {
int dem; arr[i] = rand() % 1000 + 1;
struct ttNode *Next; }
}; }

//b. Viet ham tao danh sach lien ket don tu 100 so nguyen duoc phat
typedef struct ttList sinh o trên voi dieuu kien các phan tu:
{ //i. Thuoc tinh dem cua mot node cho biet so lan xuat hien cua
ttNode *Head; so do.
ttNode *Tail;
}List; int search(List *list, int key)
{ } else {

int found = 0; if (search(list, node->key)) {


ttNode *p = list->Head;
for (ttNode *p = list->Head; p != NULL; p = p->Next) { while (p->Next != NULL && p->Next->key != node->key) {
if (p->key == key) { p = p->Next;
found = 1; }
break; p->Next->dem++;
} } else {
}
return found; node->Next = list->Head;
} list->Head = node;
}
List *create_list(int *arr) }
{ }
// Khoi tao danh sach lien ket don
List *list = (List *)malloc(sizeof(List)); return list;
list->Head = NULL; }
list->Tail = NULL;
//ii. Cho biet phan tu xuat hien nhieu nhat trong danh sach.
// Vong lap for de duyet qua mang arr
for (int i = 0; i < 100; i++) { int xuathiennhieunhat(List *list) {
// Tao nut moi int num = list->Head->key;
ttNode *node = (ttNode *)malloc(sizeof(ttNode)); int num_count = list->Head->dem;
node->key = arr[i];
node->dem = 1; for (ttNode *p = list->Head->Next; p != NULL; p = p->Next) {
node->Next = NULL; if (p->dem > num_count) {
num = p->key;
num_count = p->dem;
if (list->Head == NULL) { }
list->Head = node; }
list->Tail = node;
return num; int main()
} {

//iii. Cai dat giai thuat sap xep SelectionSort cho danh sach tren. int arr[100];
void swap(int &xp, int &yp)
{ ngaunhien100(arr);
int temp = xp;
xp = yp; List *list = create_list(arr);
yp = temp;
} for (ttNode *p = list->Head; p != NULL; p = p->Next) {
void selectionSort(int *arr, int n) printf("%d: %d\n", p->key, p->dem);
{ }
int i, j, min_idx;
for (i = 0; i < n-1; i++) int num_find = xuathiennhieunhat(list);
{
min_idx = i; cout << "So xuat hien nhieu nhat la: " << num_find << endl;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx]) int n = sizeof(arr)/sizeof(arr[0]);
min_idx = j; selectionSort(arr, n);
printf("Sorted array: \n");
swap(arr[min_idx], arr[i]); printArray(arr, n);
} return 0;
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

You might also like