0% found this document useful (0 votes)
26 views36 pages

AVL Tree - Attempt Review - BK-LMS

Uploaded by

quan.buivietanh
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)
26 views36 pages

AVL Tree - Attempt Review - BK-LMS

Uploaded by

quan.buivietanh
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/ 36

12/23/24, 2:33 PM 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

Mark 1.00 out of 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=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 << " ";
}

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

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:

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

// 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'. Tree:
avl.printTreeStructure(); 1
avl.insert(2); 0 2
cout << endl << "After inserting 2, perform 'rotateLeft'. Tree:" << endl;
avl.printTreeStructure();

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

Ace editor not ready. Perhaps reload page?


Falling back to raw text area.

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;

Node* rotateLeft(Node* root) {


//TODO: Rotate and return new root after rotate
Node* newRoot = root->pRight;
Node* newchild = newRoot->pLeft;
newRoot->pLeft = root;
root->pRight = newchild;

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 Expected Got

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


AVLTree<int> avl; 0 0
avl.insert(0); 1 1
avl.insert(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. 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();

Passed all tests! 

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

Mark 1.00 out of 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=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 << " ";
}

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

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

Ace editor not ready. Perhaps reload page?


Falling back to raw text area.

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

Node* rotateLeft(Node* root) {


//TODO: Rotate and return new root after rotate
Node* newRoot = root->pRight;
Node* childNewRoot = newRoot->pLeft;
newRoot-> pLeft = root;
root->pRight = childNewRoot;
return newRoot;
}

int getBalance(Node* root)


{
if(root== nullptr)
return 0;

return getHeightRec(root->pLeft) - getHeightRec(root-> pRight);


}
Node* helper(Node* node, T value)
{
if(node == nullptr)
return new Node(value);

if(value < node-> data)


node->pLeft = helper(node->pLeft , value);

else if(value >= node->data)


node->pRight = helper(node->pRight , value);

int balance = getBalance(node);

if(balance > 1 && value < node->pLeft-> data)


return rotateRight(node);

if(balance <-1 && value > node->pRight-> data)


return rotateLeft(node);

if(balance > 1 && value > node->pLeft->data)


{
node->pLeft = rotateLeft(node->pLeft);
return rotateRight(node);
}

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

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! 

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

Mark 1.00 out of 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=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 << " ";
}

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

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

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

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

Ace editor not ready. Perhaps reload page?


Falling back to raw text area.
{
if(root== nullptr)
return 0;

return getHeightRec(root->pLeft) - getHeightRec(root-> pRight);


}
Node* helper(Node* node, T value)
{
if(node == nullptr)
return new Node(value);

if(value < node-> data)


node->pLeft = helper(node->pLeft , value);

else if(value >= node->data)


node->pRight = helper(node->pRight , value);

int balance = getBalance(node);

if(balance > 1 && value < node->pLeft-> data)


return rotateRight(node);

if(balance <-1 && value > node->pRight-> data)


return rotateLeft(node);

if(balance > 1 && value > node->pLeft->data)


{
node->pLeft = rotateLeft(node->pLeft);
return rotateRight(node);
}

if(balance < -1 && value < node->pRight->data)


{
node->pRight = rotateRight(node->pRight);
return rotateLeft(node);
}

node->balance = BalanceValue(balance);
return node;
}

void insert(const T &value){


//TODO
this->root = this->helper(this->root, value);

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

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! 

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

Mark 1.00 out of 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=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 << " ";
}

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

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

Ace editor not ready. Perhaps reload page?


Falling back to raw text area.

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;

return getHeightRec(root->pLeft) - getHeightRec(root-> pRight);


}
Node* helper(Node* node, T value)
{
if(node == nullptr)
return new Node(value);

if(value < node-> data)


node->pLeft = helper(node->pLeft , value);

else if(value >= node->data)


node->pRight = helper(node->pRight , value);

int balance = getBalance(node);

if(balance > 1 && value < node->pLeft-> data)


return rotateRight(node);

if(balance <-1 && value > node->pRight-> data)


return rotateLeft(node);

if(balance > 1 && value > node->pLeft->data)


{
node->pLeft = rotateLeft(node->pLeft);
return rotateRight(node);
}

if(balance < -1 && value < node->pRight->data)


{
node->pRight = rotateRight(node->pRight);
return rotateLeft(node);
}

node->balance = BalanceValue(balance);
return node;
}

void insert(const T &value){


//TODO
this->root = this->helper(this->root, value);

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

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! 

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

Mark 1.00 out of 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()
{
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;
}
}

void remove(const T &value)


{ 
//TODO
}

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

Answer: (penalty regime: 0 %)

Reset answer

Ace editor not ready. Perhaps reload page?


Falling back to raw text area.

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;

return getHeightRec(root->pLeft) - getHeightRec(root-> pRight);


}
Node* helper(Node* root, T value)
{
if(root == nullptr)
{
return root;
}

if(value < root->data)


root->pLeft = helper(root->pLeft, value);

else if(value > root-> data)


root->pRight = helper(root->pRight, value);

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

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! 

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

Mark 1.00 out of 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=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 << " ";
}

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

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

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

Ace editor not ready. Perhaps reload page?


Falling back to raw text area.
else
{
Node* temp = maxValue(root->pLeft);

root->data = temp->data;

root->pLeft = helper(root->pLeft, temp->data);


}

}
if(root == nullptr)
{
return root;
}
int balance = getBalance(root);

if(balance > 1 && getBalance(root->pLeft) >= 0)


{
return rotateRight(root);
}
if(balance > 1 && getBalance(root->pLeft) < 0)
{
root->pLeft = rotateLeft(root->pLeft);
return rotateRight(root);
}

if(balance < - 1 && getBalance(root->pRight) <= 0 )


{
return rotateLeft(root);
}
if(balance < -1 && getBalance(root->pRight) > 0)
{
root->pRight = rotateRight(root->pRight);
return rotateLeft(root);
}

root->balance = (balance < 0) ? RH : (balance > 0) ? LH : EH;


return root;

void remove(const T &value){


//TODO
if(root == nullptr)
return;

this->root = this->helper(this->root, value);
}

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

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! 

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

Mark 1.00 out of 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:

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

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

Ace editor not ready. Perhaps reload page?


Falling back to raw text area.

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

cout<< current->data<< " ";

printInOrder(current->pRight);

}
void printInorder(){
//TODO
printInOrder(this->root);
}
bool search(const T& value , Node* current)
{
if(current == nullptr)
return false;

if(current->data > value)


return search(value, current->pLeft);

if( current->data < value)


return search(value, current->pRight);

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

Test Expected Got

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

Passed all tests! 

Correct
Marks for this submission: 1.00/1.00.

https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4940183&cmid=473692 36/36

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy