AVL Tree - Attempt Review - BK-LMS
AVL Tree - Attempt Review - BK-LMS
Status Finished
Started Monday, 9 December 2024, 12:19 PM
Completed Tuesday, 17 December 2024, 11:48 AM
Duration 7 days 23 hours
Marks 7.00/7.00
Grade 10.00 out of 10.00 (100%)
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 1/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Question 1
Correct
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=4940183&cmid=473692 2/36
12/23/24, 2:33 PM AVL Tree: Attempt review | 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)
{
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 3/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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;
}
}
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:
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 4/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Test Result
Reset answer
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 5/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Node* rotateRight(Node* root) {
//TODO: Rotate and return new root after rotate
Node* newRoot = root->pLeft;
Node* newchild = newRoot->pRight;
newRoot->pRight= root;
root->pLeft = newchild;
return newRoot;
return newRoot;
}
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 6/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
// Test rotateRight After inserting 10, 9. Tree: After inserting 10, 9. Tree:
AVLTree<int> avl; 10 10
avl.insert(10); 9 9
avl.insert(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();
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 7/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Question 2
Correct
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=4940183&cmid=473692 8/36
12/23/24, 2:33 PM AVL Tree: Attempt review | 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)
{
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 9/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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() {}
};
};
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
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 10/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Node* rotateRight(Node* root) {
//TODO: Rotate and return new root after rotate
Node* newRoot = root->pLeft;
Node* childNewRoot = newRoot->pRight;
newRoot-> pRight = root;
root->pLeft = childNewRoot;
return newRoot;
}
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 11/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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();
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 12/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Question 3
Correct
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=4940183&cmid=473692 13/36
12/23/24, 2:33 PM AVL Tree: Attempt review | 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();
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 14/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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() {}
};
};
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();
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 15/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Answer: (penalty regime: 0 %)
Reset answer
node->balance = BalanceValue(balance);
return node;
}
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 16/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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();
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 17/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Question 4
Correct
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=4940183&cmid=473692 18/36
12/23/24, 2:33 PM AVL Tree: Attempt review | 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)
{
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 19/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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() {}
};
};
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
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 20/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
{
if(root== nullptr)
return 0;
node->balance = BalanceValue(balance);
return node;
}
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 21/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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();
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 22/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Question 5
Correct
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()
{
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 23/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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;
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:
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 24/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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; 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
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 25/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Node* minValue (Node* node)
{
Node* curr = node;
while(curr->pLeft != nullptr)
curr = curr->pLeft;
return curr;
}
Node* maxValue(Node* node)
{
Node* curr = node;
while(curr->pRight != nullptr)
curr = curr->pRight;
return curr;
}
int getBalance(Node* root)
{
if(root== nullptr)
return 0;
else
{
if (root->pLeft == nullptr || root->pRight == nullptr) {
Node* temp = root->pLeft ? root->pLeft : root->pRight;
delete root;
return temp;
}
// else if (root->pRight == nullptr) {
// Node* temp = root->pLeft;
// delete root;
// root = temp;
// }
else
{
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 26/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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();
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 27/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Question 6
Correct
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=4940183&cmid=473692 28/36
12/23/24, 2:33 PM AVL Tree: Attempt review | 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)
{
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 29/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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() {}
};
};
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();
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 30/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Reset answer
root->data = temp->data;
}
if(root == nullptr)
{
return root;
}
int balance = getBalance(root);
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 31/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
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();
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 32/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Question 7
Correct
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:
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 33/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
Test Result
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 34/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
void printInOrder(Node* current)
{
if(current == nullptr)
return ;
printInOrder(current->pLeft);
printInOrder(current->pRight);
}
void printInorder(){
//TODO
printInOrder(this->root);
}
bool search(const T& value , Node* current)
{
if(current == nullptr)
return false;
return true;
}
bool search(const T &value){
//TODO
return search(value, this->root);
}
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 35/36
12/23/24, 2:33 PM AVL Tree: Attempt review | BK-LMS
AVLTree<int> avl; 10 13 32 40 42 52 63 68 92 98 10 13 32 40 42 52 63 68 92
int arr[] = 99 100 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);
Correct
Marks for this submission: 1.00/1.00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 36/36