Vadodara 💋 Call Girl 7737669865 Call Girls in Vadodara Escort service book now
Bài tập và bài tập nâng cao của cây AVL.docx
1. Bài tập AVL
Áp dụng – Nâng cao:
1. Sinh viên tự cài đặt thêm chức năng cho phép người dùng nhập vào khóa x và kiểm tra xem khóa x
có nằm trong cây AVL hay không.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Node* Tree_CB::search_x(Node* p, int data)
{
if(p == NULL)
return NULL;
else
{
if(p->Getkey() > data)
{
search_x(p->Getleft(), data);
}
else if(p->Getkey() < data)
{
search_x(p->Getright(), data);
}
else
return p;
}
}
2. Tạo cây AVL từ dãy A:
1 3 5 7 9 12 15 17 21 23 25 27
. Cho biết số phép so sánh cần thực hiện để tìm phần tử 21 trên cây AVL vừa tạo.
Số phép so sánh cần thực hiện để tìm phần tử thứ 21 là 3
2. 2. Cài đặt chương trình đọc các số nguyên từ tập tin input.txt (không biết trước số lượng số nguyên
trên tập tin) và tạo cây AVL từ dữ liệu đọc được
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int b;
Tree_CB* tree_file = new Tree_CB();
ifstream input("input.txt");
if (!input) {
cout << "Khong mo duoc tap tin input.txt" << endl;
return 1;
}
while (input >> b) {
tree_file->InsertNode(new Node(b));
}
input.close();
cout << "Cay AVL nhap tu file" << endl;
tree_file->TravelNLR();
cout <<endl<< "Nhap So Can Xoa: "; cin >> b;
tree_file->deleteNode(tree_file->search_x(tree_file->Getroot(),
b));
cout << "Cay AVL sau khi xoa " << b <<" : " << endl;
tree_file->TravelNLR();
cout << endl;
input.close();
3. Cài đặt cây cân bằng AVL trong đó mỗi node trên cây lưu thông tin sinh viên.
Student.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
string Name;
string Student_ID;
string Class_Name;
int Age;
int bal;//thuoc tinh cho biet gia tri can bang
// 0, -1, 1: can bang, 2: lech trai, -2: lech phai
int height;
Student* parent;
Student* left;
Student* right;
public:
Student(string, string, string, int);
~Student();
Student *Getleft() { return left; }
void Setleft(Student *val) { left = val; }