Professional Documents
Culture Documents
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)
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;
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; {
//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 {
//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");
}