You are on page 1of 10

Bài tập.

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>

} using namespace std;


struct node{ return countLeafNode(t->left) +
countLeafNode(t->right);
int data;
}
node *left;
int main(){
node *right;
int n, temp;
};
cin >> n;
node *insert(node *t, int x){
node * t = NULL;
if (t == NULL){
for (int i = 0; i < n; i++){
node *temp = new node;
cin >> temp;
temp->data =x;
t = insert(t, temp);
temp->left = NULL;
}
temp->right = NULL;
cout << countLeafNode(t);
return temp;

} 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>
}

bool isLeafNode(node *l){ using namespace std;


return (l->left == NULL && l->right ==
NULL);
struct node{
}
int data;
int countLeafNode(node *t){
node *left;
if (t == NULL) return 0;
node *right;
if (isLeafNode(t)) return 1;
}; cin >> n;

node *insert(node *t, int x){ node * t = NULL;

if (t == NULL){ for (int i = 0; i < n; i++){

node *temp = new node; cin >> temp;

temp->data =x; t = insert(t, temp);

temp->left = NULL; }

temp->right = NULL; cout << treeLevel(t);

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>

bool isLeafNode(node *l){ #include<math.h>

return (l->left == NULL && l->right ==


NULL); using namespace std;
}

int treeLevel(node *t){ struct node{


if (t == NULL) return -1; int data;
return 1 + max(treeLevel(t->left), node *left;
treeLevel(t->right));
node *right;
}
};
int main(){
node *insert(node *t, int x){
int n, temp;
if (t == NULL){
node *temp = new node; cin >> n;

temp->data =x; node * t = NULL;

temp->left = NULL; for (int i = 0; i < n; i++){

temp->right = NULL; cin >> temp;

return temp; t = insert(t, temp);

} else{ }

if (x < t->data){ cout << checkAvl(t) << endl;

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){

if (t == NULL) return true;


struct node{
if (abs(treeLevel(t->left) - treeLevel(t-
>right)) > 1) return false; int data;

return checkAvl(t->left) && checkAvl(t- node *left;


>right);
node *right;
}
};
int main(){
node *insert(node *t, int x){
int n, temp;
if (t == NULL){
node *temp = new node; node *d = b->right;

temp->data =x; a->left = d;

temp->left = NULL; b->right = a;

temp->right = NULL; return b;

return temp; }

} else{ node *turnLeft(node *a){

if (x < t->data){ node *b = a->right;

t->left = insert(t->left, node *c = b->left;


x);
a->right = c;
} else{
b->left = a;
t->right = insert(t-
return b;
>right, x);
}
}
void printTree(node *t){
}
if (t != NULL){

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>
}

if (t->left != NULL) t->left =


updateTreeAvl(t->left); using namespace std;

if (t->right != NULL) t->right =


updateTreeAvl(t->right);
struct node{
return t;
int data;
}
node *left;
int main(){
node *right;
int n, temp;
};
cin >> n;
node *insert(node *t, int x){
node * t = NULL;
if (t == NULL){
node *temp = new node; node *d = b->right;

temp->data =x; a->left = d;

temp->left = NULL; b->right = a;

temp->right = NULL; return b;

return temp; }

} else{ node *turnLeft(node *a){

if (x < t->data){ node *b = a->right;

t->left = insert(t->left, node *c = b->left;


x);
a->right = c;
} else{
b->left = a;
t->right = insert(t-
return b;
>right, x);
}
}
void printTree(node *t){
}
if (t != NULL){

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);

t = turnRight(t); return count(t->left, x);

} else{ }

t->left = int main(){


turnLeft(t->left);
int n, x, temp;
t = turnRight(t);
cin >> n;
}
node * t = NULL;
} else {
for (int i = 0; i < n; i++){
node *p = t->right;
cin >> temp;
if (treeLevel(p->right)
t = insert(t, temp);
>= treeLevel(p->left)){
}
t = turnLeft(t);
while(!checkAvl(t)){
} else{
t = updateTreeAvl(t);
t->right =
turnRight(t->right); }
t = turnLeft(t); cin >> x;

cout << count(t, x);


} return 0;
} }
} 7.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
if (t->left != NULL) t->left =
sau, phần tử đầu tiên được chọn làm gốc, các
updateTreeAvl(t->left);
phần tử tiếp theo được chèn và cây nhị phần
if (t->right != NULL) t->right = theo cách: node con bên trái luôn nhỏ hơn
updateTreeAvl(t->right); node cha, node con bên phải luôn lớn hơn học
bằng node cha.
return t;
Cuối cùng nhập một sô nguyên x, hãy xóa
}
những node có giá trị bằng x (các node con của
int count(node *t, int x){ nó cũng bị xóa theo). In các phần tử trong cây
theo cách duyệt trung thứ tự. Nếu cây rỗng in
if (t == NULL) return 0; ra "NULL".
if (t->data == x) return 1 + count(t->left, #include<iostream>
x) + count(t->right, x);
#include<math.h>
printTree(t->right);

using namespace std; }

struct node{ void deleteNode(node *t){

int data; if(t != NULL){

node *left; if (t->left != NULL)


deleteNode(t->left);
node *right;
if (t->right != NULL)
};
deleteNode(t->right);
node *insert(node *t, int x){
delete(t);
if (t == NULL){
}
node *temp = new node;
}
temp->data =x;
node *deleteNumber(node *t, int x){
temp->left = NULL;
if (t != NULL){
temp->right = NULL;
if (t->data == x){
return temp;
deleteNode(t->left);
} else{
deleteNode(t->right);
if (x < t->data){
t = NULL;
t->left = insert(t->left,
}
x);
else if (t->data > x) t->left =
} else{
deleteNumber(t->left, x);
t->right = insert(t-
else t->right = deleteNumber(t-
>right, x);
>right, x);
}
}
}
return t;
}
}
void printTree(node *t){
int main(){
if (t != NULL){
int n, x, temp;
printTree(t->left);
cin >> n;
cout << t->data << " ";
node * t = NULL;
for (int i = 0; i < n; i++){

cin >> temp;

t = insert(t, temp);

cin >> x;

t = deleteNumber(t, x);

if (t == NULL) cout <<"NULL";

else printTree(t);

return 0;

You might also like