0% found this document useful (0 votes)
53 views29 pages

AVL Tree - Xem lại lần làm thử - BK-LMS

The document details a quiz on AVL Trees, where the user successfully completed two questions related to implementing rotation and insertion functions. The user achieved a perfect score of 10/10, demonstrating proficiency in managing AVL tree operations. Code snippets and test cases are provided to illustrate the required functionalities.

Uploaded by

Thanh Nhân
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views29 pages

AVL Tree - Xem lại lần làm thử - BK-LMS

The document details a quiz on AVL Trees, where the user successfully completed two questions related to implementing rotation and insertion functions. The user achieved a perfect score of 10/10, demonstrating proficiency in managing AVL tree operations. Code snippets and test cases are provided to illustrate the required functionalities.

Uploaded by

Thanh Nhân
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS

Trạng thái Đã xong


Bắt đầu vào lúc Thứ Sáu, 13 tháng 12 2024, 1:32 PM
Kết thúc lúc Thứ Sáu, 13 tháng 12 2024, 1:59 PM
Thời gian thực 27 phút 22 giây
hiện
Điểm 7,00/7,00
Điểm 10,00 trên 10,00 (100%)

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 1/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS

Câu hỏi 1
Đúng

Đạt điểm 1,00 trên 1,00

In this question, you have to perform rotate nodes on AVL tree. Note that:
- When adding a node which has the same value as parent node, add it in the right sub tree.

Your task is to implement function: rotateRight, rotateLeft. You could define one or more functions to achieve this task.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 2/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
#include <iostream>
#include <math.h>
#include <queue>
using namespace std;
#define SEPARATOR "#<ab@17943918#@>#"

enum BalanceValue
{
LH = -1,
EH = 0,
RH = 1
};

void printNSpace(int n)
{
for (int i = 0; i < n - 1; i++)
cout << " ";
}

void printInteger(int &n)


{
cout << n << " ";
}

template<class T>
class AVLTree
{
public:
class Node;
private:
Node *root;
protected:
int getHeightRec(Node *node)
{
if (node == NULL)
return 0;
int lh = this->getHeightRec(node->pLeft);
int rh = this->getHeightRec(node->pRight);
return (lh > rh ? lh : rh) + 1;
}
public:
AVLTree() : root(nullptr) {}
~AVLTree(){}
int getHeight()
{
return this->getHeightRec(this->root);
}
void printTreeStructure()
{
int height = this->getHeight();
if (this->root == NULL)
{
cout << "NULL\n";
return;
}
queue<Node *> q;
q.push(root);
Node *temp;
int count = 0;
int maxNode = 1;
int level = 0;
int space = pow(2, height);
printNSpace(space / 2);
while (!q.empty())
{
temp = q.front();
q.pop();
if (temp == NULL)
{ 
cout << " ";
q.push(NULL);
q.push(NULL);
}
else
{
cout << temp->data;
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 3/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
q.push(temp->pLeft);
q.push(temp->pRight);
}
printNSpace(space);
count++;
if (count == maxNode)
{
cout << endl;
count = 0;
maxNode *= 2;
level++;
space /= 2;
printNSpace(space / 2);
}
if (level == height)
return;
}
}

void insert(const T &value);

int getBalance(Node*subroot){
if(!subroot) return 0;
return getHeightRec(subroot->pLeft)- getHeightRec(subroot->pRight);
}

Node* rotateLeft(Node* subroot)

//TODO: Rotate and return new root after rotate

};

Node* rotateRight(Node* subroot)

//TODO: Rotate and return new root after rotate

};

class Node
{
private:
T data;
Node *pLeft, *pRight;
BalanceValue balance;
friend class AVLTree<T>;

public:
Node(T value) : data(value), pLeft(NULL), pRight(NULL), balance(EH) {}
~Node() {}
};
};

For example:

Test Result

// Test rotateLeft After inserting 0, 1. Tree:


AVLTree<int> avl; 0
avl.insert(0); 1
avl.insert(1);
cout << "After inserting 0, 1. Tree:" << endl; After inserting 2, perform 'rotateLeft'.
avl.printTreeStructure(); Tree:
avl.insert(2); 1
cout << endl << "After inserting 2, perform 'rotateLeft'. Tree:" << 0 2

endl;
avl.printTreeStructure();

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 4/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS

Test Result

// Test rotateRight After inserting 10, 9. Tree:


AVLTree<int> avl; 10
avl.insert(10); 9
avl.insert(9);
cout << "After inserting 10, 9. Tree:" << endl; After inserting 8, perform 'rotateRight'.
avl.printTreeStructure(); Tree:
avl.insert(8); 9
cout << endl << "After inserting 8, perform 'rotateRight'. Tree:" 8 10
<< endl;
avl.printTreeStructure();

Answer: (penalty regime: 0 %)

Reset answer

1 ▼ Node* rotateRight(Node* root) {


2 //TODO: Rotate and return new root after rotate
3 Node* newnode = root->pLeft;
4 root->pLeft = newnode->pRight;
5 newnode->pRight = root;
6
7 return newnode;
8 }
9
10 ▼ Node* rotateLeft(Node* root) {
11 //TODO: Rotate and return new root after rotate
12 Node* newnode = root->pRight;
13 root->pRight = newnode->pLeft;
14 newnode->pLeft = root;
15
16 return newnode;
17 }

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 5/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS

Test Expected Got

 // Test rotateLeft After inserting 0, 1. After inserting 0, 1. 


AVLTree<int> avl; Tree: Tree:
avl.insert(0); 0 0
avl.insert(1); 1 1
cout << "After inserting 0, 1. Tree:" <<
endl; After inserting 2, perform After inserting 2, perform
avl.printTreeStructure(); 'rotateLeft'. Tree: 'rotateLeft'. Tree:
avl.insert(2); 1 1
cout << endl << "After inserting 2, perform 0 2 0 2
'rotateLeft'. Tree:" << endl;
avl.printTreeStructure();

 // Test rotateRight After inserting 10, 9. After inserting 10, 9. 


AVLTree<int> avl; Tree: Tree:
avl.insert(10); 10 10
avl.insert(9); 9 9
cout << "After inserting 10, 9. Tree:" <<
endl; After inserting 8, perform After inserting 8, perform
avl.printTreeStructure(); 'rotateRight'. Tree: 'rotateRight'. Tree:
avl.insert(8); 9 9
cout << endl << "After inserting 8, perform 8 10 8 10
'rotateRight'. Tree:" << endl;
avl.printTreeStructure();

Passed all tests! 

Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 6/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS

Câu hỏi 2
Đúng

Đạt điểm 1,00 trên 1,00

In this question, you have to perform add on AVL tree. Note that:

- When adding a node which has the same value as parent node, add it in the right sub tree.
Your task is to implement function: insert. The function should cover at least these cases:

+ Balanced tree

+ Left of left unbalanced tree

+ Right of left unbalanced tree

You could define one or more functions to achieve this task.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 7/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
#include <iostream>
#include <math.h>
#include <queue>
using namespace std;
#define SEPARATOR "#<ab@17943918#@>#"

enum BalanceValue
{
LH = -1,
EH = 0,
RH = 1
};

void printNSpace(int n)
{
for (int i = 0; i < n - 1; i++)
cout << " ";
}

void printInteger(int &n)


{
cout << n << " ";
}

template<class T>
class AVLTree
{
public:
class Node;
private:
Node *root;
protected:
int getHeightRec(Node *node)
{
if (node == NULL)
return 0;
int lh = this->getHeightRec(node->pLeft);
int rh = this->getHeightRec(node->pRight);
return (lh > rh ? lh : rh) + 1;
}
public:
AVLTree() : root(nullptr) {}
~AVLTree(){}
int getHeight()
{
return this->getHeightRec(this->root);
}
void printTreeStructure()
{
int height = this->getHeight();
if (this->root == NULL)
{
cout << "NULL\n";
return;
}
queue<Node *> q;
q.push(root);
Node *temp;
int count = 0;
int maxNode = 1;
int level = 0;
int space = pow(2, height);
printNSpace(space / 2);
while (!q.empty())
{
temp = q.front();
q.pop();
if (temp == NULL)
{ 
cout << " ";
q.push(NULL);
q.push(NULL);
}
else
{
cout << temp->data;
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 8/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
q.push(temp->pLeft);
q.push(temp->pRight);
}
printNSpace(space);
count++;
if (count == maxNode)
{
cout << endl;
count = 0;
maxNode *= 2;
level++;
space /= 2;
printNSpace(space / 2);
}
if (level == height)
return;
}
}

void insert(const T &value)


{
//TODO
}

class Node
{
private:
T data;
Node *pLeft, *pRight;
BalanceValue balance;
friend class AVLTree<T>;

public:
Node(T value) : data(value), pLeft(NULL), pRight(NULL), balance(EH) {}
~Node() {}
};
};

For example:

Test Result

AVLTree<int> avl; -3
for (int i = 0; i >= -10; i--){ -7 -1
avl.insert(i); -9 -5 -2 0
} -10 -8 -6 -4
avl.printTreeStructure();

AVLTree<int> avlTree; 6
avlTree.insert(5); 5 7
avlTree.insert(7);
avlTree.insert(6);
avlTree.printTreeStructure();

Answer: (penalty regime: 0 %)

Reset answer

1 //Helping functions
2
3 Node* insertRec(Node* node, const T& value)
4 ▼ {
5 if (!node)
6 return new Node(value);
7
8 if (value < node->data)
9 node->pLeft = insertRec(node->pLeft, value);
10 else 
11 node->pRight = insertRec(node->pRight, value);
12
13 node = balance(node);
14
15 return node;
16 }
17
18 ▼ Node* balance(Node* node){

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 9/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
( ){
19 int balanceFactor = getHeightRec(node->pLeft) - getHeightRec(node->pRight);
20
21 if (balanceFactor > 1)
22 ▼ {
23 if (getHeightRec(node->pLeft->pLeft) >= getHeightRec(node->pLeft->pRight))
24 return rotateRight(node);
25 else
26 ▼ {
27 node->pLeft = rotateLeft(node->pLeft);
28 return rotateRight(node);
29 }
30 }
31
32 if (balanceFactor < -1)
33 ▼ {
34 if (getHeightRec(node->pRight->pRight) >= getHeightRec(node->pRight->pLeft))
35 return rotateLeft(node);
36 else
37 ▼ {
38 node->pRight = rotateRight(node->pRight);
39 return rotateLeft(node);
40 }
41 }
42
43 return node;
44 }
45
46 ▼ Node* rotateRight(Node* root) {
47 //TODO: Rotate and return new root after rotate
48 Node* newnode = root->pLeft;
49 root->pLeft = newnode->pRight;
50 newnode->pRight = root;
51
52 return newnode;
53 }
54
55 ▼ Node* rotateLeft(Node* root) {
56 //TODO: Rotate and return new root after rotate
57 Node* newnode = root->pRight;
58 root->pRight = newnode->pLeft;
59 newnode->pLeft = root;
60
61 return newnode;
62 }

Test Expected Got

 AVLTree<int> avl; -3 -3 
for (int i = 0; i >= -10; i--){ -7 -1 -7 -1
avl.insert(i); -9 -5 -2 0 -9 -5 -2 0
} -10 -8 -6 -4 -10 -8 -6 -4
avl.printTreeStructure();

 AVLTree<int> avlTree; 6 6 
avlTree.insert(5); 5 7 5 7
avlTree.insert(7);
avlTree.insert(6);
avlTree.printTreeStructure();

Passed all tests! 

Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 10/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS

Câu hỏi 3
Đúng

Đạt điểm 1,00 trên 1,00

In this question, you have to perform add on AVL tree. Note that:

When adding a node which has the same value as parent node, add it in the right sub tree.

Your task is to implement function: insert. The function should cover at least these cases:
Balanced tree
Right of right unbalanced tree
Left of right unbalanced tree

You could define one or more functions to achieve this task.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 11/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
#include <iostream>
#include <math.h>
#include <queue>
using namespace std;
#define SEPARATOR "#<ab@17943918#@>#"

enum BalanceValue
{
LH = -1,
EH = 0,
RH = 1
};

void printNSpace(int n)
{
for (int i = 0; i < n - 1; i++)
cout << " ";
}

void printInteger(int &n)


{
cout << n << " ";
}

template<class T>
class AVLTree
{
public:
class Node;
private:
Node *root;
protected:
int getHeightRec(Node *node)
{
if (node == NULL)
return 0;
int lh = this->getHeightRec(node->pLeft);
int rh = this->getHeightRec(node->pRight);
return (lh > rh ? lh : rh) + 1;
}
public:
AVLTree() : root(nullptr) {}
~AVLTree(){}
int getHeight()
{
return this->getHeightRec(this->root);
}
void printTreeStructure()
{
int height = this->getHeight();
if (this->root == NULL)
{
cout << "NULL\n";
return;
}
queue<Node *> q;
q.push(root);
Node *temp;
int count = 0;
int maxNode = 1;
int level = 0;
int space = pow(2, height);
printNSpace(space / 2);
while (!q.empty())
{
temp = q.front();

q.pop();
if (temp == NULL)
{
cout << " ";
q.push(NULL);
q.push(NULL);
}

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 12/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
else
{
cout << temp->data;
q.push(temp->pLeft);
q.push(temp->pRight);
}
printNSpace(space);
count++;
if (count == maxNode)
{
cout << endl;
count = 0;
maxNode *= 2;
level++;
space /= 2;
printNSpace(space / 2);
}
if (level == height)
return;
}
}

void insert(const T &value)


{
//TODO
}

class Node
{
private:
T data;
Node *pLeft, *pRight;
BalanceValue balance;
friend class AVLTree<T>;

public:
Node(T value) : data(value), pLeft(NULL), pRight(NULL), balance(EH) {}
~Node() {}
};
};

For example:

Test Result

AVLTree<int> avl; 3
int nums[] = {3, 1, 6, 2, 4, 8, 5, 7, 9}; 1 6
for (int i = 0; i < 9; i++){ 2 4 8
avl.insert(nums[i]); 5 7 9
}
avl.printTreeStructure();

AVLTree<int> avl; 6
int nums[] = {6, 8, 3, 5, 7, 9, 1, 2, 4}; 3 8
for (int i = 0; i < 9; i++){ 1 5 7 9
avl.insert(nums[i]); 2 4
}
avl.printTreeStructure();

Answer: (penalty regime: 0 %)

Reset answer

1 //Helping functions
2
3 Node* insertRec(Node* node, const T& value) 
4 ▼ {
5 if (!node)
6 return new Node(value);
7
8 if (value < node->data)
9 node->pLeft = insertRec(node->pLeft, value);
10 else
11 node->pRight = insertRec(node->pRight, value);
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 13/29
12/22/24, 9:43 PM g AVL
g Tree: Xem lại lần làm thử | BK-LMS
12
13 node = balance(node);
14
15 return node;
16 }
17
18 ▼ Node* balance(Node* node){
19 int balanceFactor = getHeightRec(node->pLeft) - getHeightRec(node->pRight);
20
21 if (balanceFactor > 1)
22 ▼ {
23 if (getHeightRec(node->pLeft->pLeft) >= getHeightRec(node->pLeft->pRight))
24 return rotateRight(node);
25 else
26 ▼ {
27 node->pLeft = rotateLeft(node->pLeft);
28 return rotateRight(node);
29 }
30 }
31
32 if (balanceFactor < -1)
33 ▼ {
34 if (getHeightRec(node->pRight->pRight) >= getHeightRec(node->pRight->pLeft))
35 return rotateLeft(node);
36 else
37 ▼ {
38 node->pRight = rotateRight(node->pRight);
39 return rotateLeft(node);
40 }
41 }
42
43 return node;
44 }
45
46 ▼ Node* rotateRight(Node* root) {
47 //TODO: Rotate and return new root after rotate
48 Node* newnode = root->pLeft;
49 root->pLeft = newnode->pRight;
50 newnode->pRight = root;
51
52 return newnode;
53 }
54
55 ▼ Node* rotateLeft(Node* root) {
56 //TODO: Rotate and return new root after rotate
57 Node* newnode = root->pRight;
58 root->pRight = newnode->pLeft;
59 newnode->pLeft = root;
60
61 return newnode;
62 }

Test Expected Got

 AVLTree<int> avl; 3 3 
int nums[] = {3, 1, 6, 2, 4, 8, 5, 7, 9}; 1 6 1 6
for (int i = 0; i < 9; i++){ 2 4 8 2 4 8
avl.insert(nums[i]); 5 7 9 5 7 9
}
avl.printTreeStructure();

 AVLTree<int> avl; 6 6 
int nums[] = {6, 8, 3, 5, 7, 9, 1, 2, 4}; 3 8 3 8
for (int i = 0; i < 9; i++){ 1 5 7 9 1 5 7 9
avl.insert(nums[i]); 2 4 2 4
}
avl.printTreeStructure();


Passed all tests! 

Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 14/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS

Câu hỏi 4
Đúng

Đạt điểm 1,00 trên 1,00

In this question, you have to perform add on AVL tree. Note that:

- When adding a node which has the same value as parent node, add it in the right sub tree.
Your task is to implement function: insert. You could define one or more functions to achieve this task.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 15/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
#include <iostream>
#include <math.h>
#include <queue>
using namespace std;
#define SEPARATOR "#<ab@17943918#@>#"

enum BalanceValue
{
LH = -1,
EH = 0,
RH = 1
};

void printNSpace(int n)
{
for (int i = 0; i < n - 1; i++)
cout << " ";
}

void printInteger(int &n)


{
cout << n << " ";
}

template<class T>
class AVLTree
{
public:
class Node;
private:
Node *root;
protected:
int getHeightRec(Node *node)
{
if (node == NULL)
return 0;
int lh = this->getHeightRec(node->pLeft);
int rh = this->getHeightRec(node->pRight);
return (lh > rh ? lh : rh) + 1;
}
public:
AVLTree() : root(nullptr) {}
~AVLTree(){}
int getHeight()
{
return this->getHeightRec(this->root);
}
void printTreeStructure()
{
int height = this->getHeight();
if (this->root == NULL)
{
cout << "NULL\n";
return;
}
queue<Node *> q;
q.push(root);
Node *temp;
int count = 0;
int maxNode = 1;
int level = 0;
int space = pow(2, height);
printNSpace(space / 2);
while (!q.empty())
{
temp = q.front();
q.pop();
if (temp == NULL)
{ 
cout << " ";
q.push(NULL);
q.push(NULL);
}
else
{
cout << temp->data;
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 16/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
q.push(temp->pLeft);
q.push(temp->pRight);
}
printNSpace(space);
count++;
if (count == maxNode)
{
cout << endl;
count = 0;
maxNode *= 2;
level++;
space /= 2;
printNSpace(space / 2);
}
if (level == height)
return;
}
}

void insert(const T &value)


{
//TODO
}

class Node
{
private:
T data;
Node *pLeft, *pRight;
BalanceValue balance;
friend class AVLTree<T>;

public:
Node(T value) : data(value), pLeft(NULL), pRight(NULL), balance(EH) {}
~Node() {}
};
};

For example:

Test Result

AVLTree<int> avl; 3
for (int i = 0; i < 9; i++){ 1 5
avl.insert(i); 0 2 4 7
} 6 8
avl.printTreeStructure();

AVLTree<int> avl; 7
for (int i = 10; i >= 0; i--){ 3 9
avl.insert(i); 1 5 8 10
} 0 2 4 6
avl.printTreeStructure();

Answer: (penalty regime: 0 %)

Reset answer

1 //Helping functions
2
3 Node* insertRec(Node* node, const T& value)
4 ▼ {
5 if (!node)
6 return new Node(value);
7
8 if (value < node->data)
9 node->pLeft = insertRec(node->pLeft, value);
10 else 
11 node->pRight = insertRec(node->pRight, value);
12
13 node = balance(node);
14
15 return node;
16 }
17
18 ▼ Node* balance(Node* node){

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 17/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
( ){
19 int balanceFactor = getHeightRec(node->pLeft) - getHeightRec(node->pRight);
20
21 if (balanceFactor > 1)
22 ▼ {
23 if (getHeightRec(node->pLeft->pLeft) >= getHeightRec(node->pLeft->pRight))
24 return rotateRight(node);
25 else
26 ▼ {
27 node->pLeft = rotateLeft(node->pLeft);
28 return rotateRight(node);
29 }
30 }
31
32 if (balanceFactor < -1)
33 ▼ {
34 if (getHeightRec(node->pRight->pRight) >= getHeightRec(node->pRight->pLeft))
35 return rotateLeft(node);
36 else
37 ▼ {
38 node->pRight = rotateRight(node->pRight);
39 return rotateLeft(node);
40 }
41 }
42
43 return node;
44 }
45
46 ▼ Node* rotateRight(Node* root) {
47 //TODO: Rotate and return new root after rotate
48 Node* newnode = root->pLeft;
49 root->pLeft = newnode->pRight;
50 newnode->pRight = root;
51
52 return newnode;
53 }
54
55 ▼ Node* rotateLeft(Node* root) {
56 //TODO: Rotate and return new root after rotate
57 Node* newnode = root->pRight;
58 root->pRight = newnode->pLeft;
59 newnode->pLeft = root;
60
61 return newnode;
62 }

Test Expected Got

 AVLTree<int> avl; 3 3 
for (int i = 0; i < 9; i++){ 1 5 1 5
avl.insert(i); 0 2 4 7 0 2 4 7
} 6 8 6 8
avl.printTreeStructure();

 AVLTree<int> avl; 7 7 
for (int i = 10; i >= 0; i--){ 3 9 3 9
\tavl.insert(i); 1 5 8 10 1 5 8 10
} 0 2 4 6 0 2 4 6
avl.printTreeStructure();

Passed all tests! 

Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 18/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS

Câu hỏi 5
Đúng

Đạt điểm 1,00 trên 1,00

In this question, you have to perform delete in AVL tree - balanced, L-L, R-L, E-L. Note that:

- Provided insert function already.

- You must adjust the balance factor, not the height of tree.

Your task is to implement function: remove to perform re-balancing (balanced, left of left, right of left, equal of left). When deleting a
root with both left and right tree, please choose the maximum value of left tree to replace the root. You could define one or more
functions to achieve this task.

#include <iostream>
#include <math.h>
#include <queue>
using namespace std;
#define SEPARATOR "#<ab@17943918#@>#"

enum BalanceValue
{
LH = -1,
EH = 0,
RH = 1
};

void printNSpace(int n)
{
for (int i = 0; i < n - 1; i++)
cout << " ";
}

void printInteger(int &n)


{
cout << n << " ";
}

template<class T>
class AVLTree
{
public:
class Node;
private:
Node *root;
protected:
int getHeightRec(Node *node)
{
if (node == NULL)
return 0;
int lh = this->getHeightRec(node->pLeft);
int rh = this->getHeightRec(node->pRight);
return (lh > rh ? lh : rh) + 1;
}
public:
AVLTree() : root(nullptr) {}
~AVLTree(){}
int getHeight()
{ 
return this->getHeightRec(this->root);
}
void printTreeStructure()
{
int height = this->getHeight();

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 19/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
if (this->root == NULL)
{
cout << "NULL\n";
return;
}
queue<Node *> q;
q.push(root);
Node *temp;
int count = 0;
int maxNode = 1;
int level = 0;
int space = pow(2, height);
printNSpace(space / 2);
while (!q.empty())
{
temp = q.front();
q.pop();
if (temp == NULL)
{
cout << " ";
q.push(NULL);
q.push(NULL);
}
else
{
cout << temp->data;
q.push(temp->pLeft);
q.push(temp->pRight);
}
printNSpace(space);
count++;
if (count == maxNode)
{
cout << endl;
count = 0;
maxNode *= 2;
level++;
space /= 2;
printNSpace(space / 2);
}
if (level == height)
return;
}
}

void remove(const T &value)


{
//TODO
}

class Node
{
private:
T data;
Node *pLeft, *pRight;
BalanceValue balance;
friend class AVLTree<T>;

public:
Node(T value) : data(value), pLeft(NULL), pRight(NULL), balance(EH) {}
~Node() {}
};
};

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 20/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
For example:

Test Result

AVLTree<int> avl; 7
int arr[] = {10, 5, 15, 7}; 5 10
for (int i = 0; i < 4; i++)
{
avl.insert(arr[i]);
}
avl.remove(15);
avl.printTreeStructure();

AVLTree<int> avl; 5
int arr[] = {10, 5, 15, 3}; 3 10
for (int i = 0; i < 4; i++)
{
avl.insert(arr[i]);
}
avl.remove(15);
avl.printTreeStructure();

Answer: (penalty regime: 0 %)

Reset answer

1 //Helping functions
2
3 ▼ Node* removeRec(Node* node, const T &value){
4 if (!node) return nullptr;
5
6 if (value < node->data)
7 ▼ {
8 node->pLeft = removeRec(node->pLeft, value);
9 }
10 else if (value > node->data)
11 ▼ {
12 node->pRight = removeRec(node->pRight, value);
13 }
14 else
15 ▼ {
16 if (!node->pLeft || !node->pRight)
17 ▼ {
18 Node* temp = node->pLeft ? node->pLeft : node->pRight;
19 delete node;
20 return temp;
21 }
22 else
23 ▼ {
24 Node* maxNode = findMax(node->pLeft);
25 node->data = maxNode->data;
26 node->pLeft = removeRec(node->pLeft, maxNode->data);
27 }
28 }
29
30 return balance(node);
31 }
32
33 ▼ Node* findMax(Node* node){
34 while(node->pRight)
35 node = node->pRight;
36 return node;
37 }
38
39 Node* balance(Node* node)
40 ▼ {
41 int balanceFactor = getBalance(node);
42
43 if (balanceFactor > 1)
44 ▼ { 
45 if (getBalance(node->pLeft) >= 0)
46 return rotateRight(node);
47 else
48 ▼ {
49 node->pLeft = rotateLeft(node->pLeft);
50 return rotateRight(node);
51 }
52 }
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 21/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
52 }
53
54 if (balanceFactor < -1)
55 ▼ {
56 if (getBalance(node->pRight) <= 0)
57 return rotateLeft(node);
58 else
59 ▼ {
60 node->pRight = rotateRight(node->pRight);
61 return rotateLeft(node);
62 }

Test Expected Got

 AVLTree<int> avl; 7 7 
int arr[] = {10, 5, 15, 7}; 5 10 5 10
for (int i = 0; i < 4; i++)
{
avl.insert(arr[i]);
}
avl.remove(15);
avl.printTreeStructure();

 AVLTree<int> avl; 5 5 
int arr[] = {10, 5, 15, 3}; 3 10 3 10
for (int i = 0; i < 4; i++)
{
avl.insert(arr[i]);
}
avl.remove(15);
avl.printTreeStructure();

Passed all tests! 

Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 22/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS

Câu hỏi 6
Đúng

Đạt điểm 1,00 trên 1,00

In this question, you have to perform delete on AVL tree. Note that:

- Provided insert function already.

Your task is to implement two functions: remove. You could define one or more functions to achieve this task.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 23/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
#include <iostream>
#include <math.h>
#include <queue>
using namespace std;
#define SEPARATOR "#<ab@17943918#@>#"

enum BalanceValue
{
LH = -1,
EH = 0,
RH = 1
};

void printNSpace(int n)
{
for (int i = 0; i < n - 1; i++)
cout << " ";
}

void printInteger(int &n)


{
cout << n << " ";
}

template<class T>
class AVLTree
{
public:
class Node;
private:
Node *root;
protected:
int getHeightRec(Node *node)
{
if (node == NULL)
return 0;
int lh = this->getHeightRec(node->pLeft);
int rh = this->getHeightRec(node->pRight);
return (lh > rh ? lh : rh) + 1;
}
public:
AVLTree() : root(nullptr) {}
~AVLTree(){}
int getHeight()
{
return this->getHeightRec(this->root);
}
void printTreeStructure()
{
int height = this->getHeight();
if (this->root == NULL)
{
cout << "NULL\n";
return;
}
queue<Node *> q;
q.push(root);
Node *temp;
int count = 0;
int maxNode = 1;
int level = 0;
int space = pow(2, height);
printNSpace(space / 2);
while (!q.empty())
{
temp = q.front();
q.pop();
if (temp == NULL)
{ 
cout << " ";
q.push(NULL);
q.push(NULL);
}
else
{
cout << temp->data;
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 24/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
q.push(temp->pLeft);
q.push(temp->pRight);
}
printNSpace(space);
count++;
if (count == maxNode)
{
cout << endl;
count = 0;
maxNode *= 2;
level++;
space /= 2;
printNSpace(space / 2);
}
if (level == height)
return;
}
}

void remove(const T &value)


{
//TODO
}

class Node
{
private:
T data;
Node *pLeft, *pRight;
BalanceValue balance;
friend class AVLTree<T>;

public:
Node(T value) : data(value), pLeft(NULL), pRight(NULL), balance(EH) {}
~Node() {}
};
};

For example:

Test Result

AVLTree<int> avl; 52
int arr[] = {10,52,98,32,68,92,40,13,42,63}; 32 92
for (int i = 0; i < 10; i++){ 13 40 68 98
avl.insert(arr[i]); 42 63
}
avl.remove(10);
avl.printTreeStructure();

AVLTree<int> avl; 52
int arr[] = {10,52,98,32,68,92,40,13,42,63,99,100}; 32 92
for (int i = 0; i < 12; i++){ 10 40 68 99
avl.insert(arr[i]); 42 63 98 100
}
avl.remove(13);
avl.printTreeStructure();

Answer: (penalty regime: 0 %)

Reset answer

1 //Helping functions
2
3 ▼ Node* removeRec(Node* node, const T &value){
4 if (!node) return nullptr;
5
6 if (value < node->data) 
7 ▼ {
8 node->pLeft = removeRec(node->pLeft, value);
9 }
10 else if (value > node->data)
11 ▼ {
12 node->pRight = removeRec(node->pRight, value);
13 }
14 else
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 25/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
14 else
15 ▼ {
16 if (!node->pLeft || !node->pRight)
17 ▼ {
18 Node* temp = node->pLeft ? node->pLeft : node->pRight;
19 delete node;
20 return temp;
21 }
22 else
23 ▼ {
24 Node* maxNode = findMax(node->pLeft);
25 node->data = maxNode->data;
26 node->pLeft = removeRec(node->pLeft, maxNode->data);
27 }
28 }
29
30 return balance(node);
31 }
32
33 ▼ Node* findMax(Node* node){
34 while(node->pRight)
35 node = node->pRight;
36 return node;
37 }
38
39 Node* balance(Node* node)
40 ▼ {
41 int balanceFactor = getBalance(node);
42
43 if (balanceFactor > 1)
44 ▼ {
45 if (getBalance(node->pLeft) >= 0)
46 return rotateRight(node);
47 else
48 ▼ {
49 node->pLeft = rotateLeft(node->pLeft);
50 return rotateRight(node);
51 }
52 }
53
54 if (balanceFactor < -1)
55 ▼ {
56 if (getBalance(node->pRight) <= 0)
57 return rotateLeft(node);
58 else
59 ▼ {
60 node->pRight = rotateRight(node->pRight);
61 return rotateLeft(node);
62 }

Test Expected Got

 AVLTree<int> avl; 52 52 
int arr[] = {10,52,98,32,68,92,40,13,42,63}; 32 92 32 92
for (int i = 0; i < 10; i++){ 13 40 68 98 13 40 68 98
\tavl.insert(arr[i]); 42 63 42 63
}
avl.remove(10);
avl.printTreeStructure();

 AVLTree<int> avl; 52 52 
int arr[] = {10,52,98,32,68,92,40,13,42,63,99,100}; 32 92 32 92
for (int i = 0; i < 12; i++){ 10 40 68 99 10 40 68 99
\tavl.insert(arr[i]); 42 63 98 100 42 63 98 100
}
avl.remove(13);
avl.printTreeStructure();

Passed all tests! 


Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 26/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS

Câu hỏi 7
Đúng

Đạt điểm 1,00 trên 1,00

In this question, you have to search and print inorder on AVL tree. You have o implement functions: search and printInorder to
complete the task. Note that:

- When the tree is null, don't print anything.

- There's a whitespace at the end when print the tree inorder in case the tree is not null.

- When tree contains value, search return true.


#include <iostream>
#include <queue>
using namespace std;
#define SEPARATOR "#<ab@17943918#@>#"

enum BalanceValue
{
LH = -1,
EH = 0,
RH = 1
};

template<class T>
class AVLTree
{
public:
class Node;
private:
Node *root;
public:
AVLTree() : root(nullptr) {}
~AVLTree(){}

void printInorder(){
//TODO
}

bool search(const T &value){


//TODO
}

class Node
{
private:
T data;
Node *pLeft, *pRight;
BalanceValue balance;
friend class AVLTree<T>;

public:
Node(T value) : data(value), pLeft(NULL), pRight(NULL), balance(EH) {}
~Node() {}
};
};

For example:

Test Result

AVLTree<int> avl; 10 13 32 40 42 52 63 68 92 98 99 100


int arr[] = {10,52,98,32,68,92,40,13,42,63,99,100}; 1
for (int i = 0; i < 12; i++){
avl.insert(arr[i]);
}
avl.printInorder(); 
cout << endl;
cout << avl.search(10);

Answer: (penalty regime: 0 %)


1 ▼ void printInorder() {
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 27/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
2 if (!root) return;
3 printInorderRec(root);
4 }
5 ▼ void printInorderRec(Node* node){
6 if (!node) return;
7 printInorderRec(node->pLeft);
8 cout << node->data << " ";
9 printInorderRec(node->pRight);
10 }
11 ▼ bool search(const T &value){
12 return searchRec(root, value);
13 }
14 ▼ bool searchRec(Node* node, const T& value){
15 if (!node) return false;
16 if (node->data == value) return true;
17 if (value < node->data)
18 return searchRec(node->pLeft, value);
19 return searchRec(node->pRight, value);
20 }

Test Expected Got

 AVLTree<int> avl; 10 13 32 40 42 52 63 68 10 13 32 40 42 52 63 68 
int arr[] = 92 98 99 100 92 98 99 100
{10,52,98,32,68,92,40,13,42,63,99,100}; 1 1
for (int i = 0; i < 12; i++){
\tavl.insert(arr[i]); 
}
avl.printInorder();
cout << endl;
cout << avl.search(10);

Passed all tests! 

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 28/29
12/22/24, 9:43 PM AVL Tree: Xem lại lần làm thử | BK-LMS
Đúng
Marks for this submission: 1,00/1,00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4975005&cmid=474525 29/29

You might also like