AVL Tree - Xem lại lần làm thử - BK-LMS
AVL Tree - Xem lại lần làm thử - BK-LMS
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
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 << " ";
}
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;
}
}
int getBalance(Node*subroot){
if(!subroot) return 0;
return getHeightRec(subroot->pLeft)- getHeightRec(subroot->pRight);
}
};
};
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
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
Reset answer
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
Đú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
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
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 << " ";
}
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;
}
}
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();
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 }
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();
Đú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
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
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 << " ";
}
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;
}
}
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();
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 }
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
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 << " ";
}
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;
}
}
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();
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 }
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();
Đú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
In this question, you have to perform delete in AVL tree - balanced, L-L, R-L, E-L. Note that:
- 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 << " ";
}
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;
}
}
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();
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 }
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();
Đú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
In this question, you have to perform delete on AVL tree. Note that:
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 << " ";
}
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;
}
}
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();
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 }
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();
Đú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
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:
- There's a whitespace at the end when print the tree inorder in case the tree is not null.
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
}
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 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);
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