Professional Documents
Culture Documents
1.Cho một dãy n số nguyên. Hãy cài đặt các số void printTree(node *t){
trong dãy đó vào kiểu dữ liệu cây. In cây theo
if (t != NULL){
cách duyệt trung thứ tự (xem lý thuyết). Sau
mỗi phần tử có đúng một khoảng trắng. printTree(t->left);
#include<iostream> cout << t->data << " " ;
printTree(t->right);
using namespace std; }
}
struct node{ int main(){
int data; int n, x;
node *left; cin >> n;
node *right; node * t = NULL;
}; for (int i = 0; i < n; i++){
node *insert(node *t, int x){ cin >> x;
if (t == NULL){ t = insert(t, x);
node *temp = new node; }
temp->data =x; printTree(t);
temp->left = NULL; }
temp->right = NULL; 2.Cho một dãy gồm n số nguyên, hãy cài đặt
dãy đó vào cây nhị phân tìm kiếm bằng cách
return temp;
sau, phần tử đầu tiên được chọn làm gốc, các
} else{ phần tử tiếp theo được chèn và cây nhị phần
theo cách: node con bên trái luôn nhỏ hơn
if (x < t->data){
node cha, node con bên phải luôn lớn hơn học
t->left = insert(t->left, bằng node cha.
x);
Hãy đếm xem cấu trúc cây đó có bao nhiêu nút
} else{ lá (nút mà không có bất kỳ nút con nào). Kết
quả là một số nguyên duy nhất.
t->right = insert(t-
>right, x); #include<iostream>
} else{
return 0;
if (x < t->data){
}
t->left = insert(t->left,
x); 3.Cho một dãy gồm n số nguyên, hãy cài đặt
dãy đó vào cây nhị phân tìm kiếm bằng cách
} else{
sau, phần tử đầu tiên được chọn làm gốc, các
t->right = insert(t- phần tử tiếp theo được chèn và cây nhị phần
>right, x); theo cách: node con bên trái luôn nhỏ hơn
node cha, node con bên phải luôn lớn hơn học
} bằng node cha.
} Hãy đưa ra bậc của cây đó.
#include<iostream>
}
temp->left = NULL; }
return temp;
} else{ return 0;
if (x < t->data){ }
t->left = insert(t->left, 4.Cho một dãy gồm n số nguyên, hãy cài đặt
x); dãy đó vào cây nhị phân tìm kiếm bằng cách
sau, phần tử đầu tiên được chọn làm gốc, các
} else{
phần tử tiếp theo được chèn và cây nhị phần
t->right = insert(t- theo cách: node con bên trái luôn nhỏ hơn
>right, x); node cha, node con bên phải luôn lớn hơn học
bằng node cha.
}
Hãy kiểm tra xem cây đó có phải là câu AVL nhị
} phân tìm kiếm hay không. (Xem thêm phần lý
thuyết).
} #include<iostream>
} else{ }
t->left = insert(t->left,
x);
return 0;
} else{
}
t->right = insert(t-
5.Cho một dãy gồm n số nguyên, hãy cài đặt
>right, x);
dãy đó vào cây nhị phân tìm kiếm bằng cách
} sau, phần tử đầu tiên được chọn làm gốc, các
phần tử tiếp theo được chèn và cây nhị phần
}
theo cách: node con bên trái luôn nhỏ hơn
node cha, node con bên phải luôn lớn hơn học
bằng node cha.
}
Hãy biến đổi cây đó thành cây AVL. Hãy đưa ra
int treeLevel(node *t){ bậc của cây đó.
if (t == NULL) return -1; #include<iostream>
return 1 + max(treeLevel(t->left), #include<math.h>
treeLevel(t->right));
}
using namespace std;
bool checkAvl(node *t){
return temp; }
printTree(t->left);
}
cout << t->data << " ";
int treeLevel(node *t){
if (t->left != NULL) cout <<t-
if (t == NULL) return -1;
>left->data << " ";
return 1 + max(treeLevel(t->left),
if (t->right != NULL) cout << t-
treeLevel(t->right));
>right->data << " ";
}
cout << endl;
bool checkAvl(node *t){
printTree(t->right);
if (t == NULL) return true;
}
if (abs(treeLevel(t->left) - treeLevel(t-
}
>right)) > 1) return false;
node *updateTreeAvl(node *t){
return checkAvl(t->left) && checkAvl(t-
>right); if (abs(treeLevel(t->left) - treeLevel(t-
>right)) > 1){
}
if (treeLevel(t->left) >
node *turnRight(node *a){
treeLevel(t->right)){
node *b = a->left;
node *p = t->left;
if (treeLevel(p->left) >= for (int i = 0; i < n; i++){
treeLevel(p->right)){
cin >> temp;
t = turnRight(t);
t = insert(t, temp);
} else{
}
t->left =
while(!checkAvl(t)){
turnLeft(t->left);
t = updateTreeAvl(t);
t = turnRight(t);
}
}
cout << treeLevel(t);
} else {
return 0;
node *p = t->right;
}
if (treeLevel(p->right)
>= treeLevel(p->left)){ 6.Cho một dãy gồm n số nguyên, hãy cài đặt
dãy đó vào cây nhị phân tìm kiếm bằng cách
t = turnLeft(t);
sau, phần tử đầu tiên được chọn làm gốc, các
} else{ phần tử tiếp theo được chèn và cây nhị phần
theo cách: node con bên trái luôn nhỏ hơn
t->right =
node cha, node con bên phải luôn lớn hơn học
turnRight(t->right);
bằng node cha.
t = turnLeft(t);
Hãy biến đổi cây đó thành cây AVL. Nhập vào
một số nguyên x, hãy đếm số lượng phần tử
có giá trị bằng x trong cây đó.
}
#include<iostream>
}
#include<math.h>
}
return temp; }
printTree(t->left);
}
cout << t->data << " ";
int treeLevel(node *t){
if (t->left != NULL) cout <<t-
if (t == NULL) return -1;
>left->data << " ";
return 1 + max(treeLevel(t->left),
if (t->right != NULL) cout << t-
treeLevel(t->right));
>right->data << " ";
}
cout << endl;
bool checkAvl(node *t){
printTree(t->right);
if (t == NULL) return true;
}
if (abs(treeLevel(t->left) - treeLevel(t-
}
>right)) > 1) return false;
node *updateTreeAvl(node *t){
return checkAvl(t->left) && checkAvl(t-
>right); if (abs(treeLevel(t->left) - treeLevel(t-
>right)) > 1){
}
if (treeLevel(t->left) >
node *turnRight(node *a){
treeLevel(t->right)){
node *b = a->left;
node *p = t->left;
if (treeLevel(p->left) >= else if (t->data < x) return count(t-
treeLevel(p->right)){ >right, x);
} else{ }
t = insert(t, temp);
cin >> x;
t = deleteNumber(t, x);
else printTree(t);
return 0;