Professional Documents
Culture Documents
EX5:
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 (replace by the maximum left node). 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();
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() {}
};
};
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();
//Helping functions
int balanceFactor (Node* _root) {
return getHeightRec(_root->pLeft) - getHeightRec(_root->pRight);
}
Node* rotation (Node* _root) {
if (balanceFactor(_root) == 2) {
if (balanceFactor(_root->pLeft) == 1)
_root = rotateRight(_root);
else {
_root->pLeft = rotateLeft(_root->pLeft);
_root = rotateRight(_root);
}
}
else{
if (balanceFactor(_root->pRight) == -1)
_root = rotateLeft(_root);
else {
_root->pRight = rotateRight(_root->pRight);
_root = rotateLeft(_root);
}
}
return _root;
}
Node* getMaxLeft (Node* _root) {
Node *pCurr = _root;
while (pCurr && pCurr->pRight != NULL)
pCurr = pCurr->pRight;
return pCurr;
}
Node* recursiveDlt (Node* _root, T value) {
if (_root == NULL)
return NULL;
if (value < _root->data) {
_root->pLeft = recursiveDlt(_root->pLeft, value);
while (true) {
if (balanceFactor(_root) == 1) {
_root->balance = LH;
break;
}
if (balanceFactor(_root) == 0) {
_root->balance = EH;
break;
}
if (balanceFactor(_root) == -1) {
_root->balance = RH;
break;
}
_root = rotation(_root);
}
}
else if (value > _root->data) {
_root->pRight = recursiveDlt(_root->pRight, value);
while (true) {
if (balanceFactor(_root) == 1) {
_root->balance = LH;
break;
}
if (balanceFactor(_root) == 0) {
_root->balance = EH;
break;
}
if (balanceFactor(_root) == -1) {
_root->balance = RH;
break;
}
_root = rotation(_root);
}
}
else {
if (_root->pLeft == NULL && _root->pRight == NULL)
return NULL;
else if (_root->pLeft == NULL) {
Node *dltPtr = _root->pRight;
delete _root;
return dltPtr;
}
else if (_root->pRight == NULL) {
Node *dltPtr = _root->pLeft;
delete _root;
return dltPtr;
}
Node* dltPtr = getMaxLeft(_root->pLeft);
_root->data = dltPtr->data;
_root->pLeft = recursiveDlt(_root->pLeft, dltPtr->data);
}
return _root;
}
AVLTree<int> avl; 52 52
Test Expected Got
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();
AVLTree<int> avl; 52 52
int arr[] = {10,52,98,32,68,92,40,13,42,63,99,100}; 40 92 40 92
for (int i = 0; i < 12; i++){ 32 42 68 99 32 42 68 99
\tavl.insert(arr[i]); 63 98 100 63 98 100
}
avl.remove(10);
avl.remove(13);
avl.printTreeStructure();
AVLTree<int> avl; 52 52
int arr[] = {10,52,98,32,68,92,40,13,42,63,99,100}; 32 68 32 68
for (int i = 0; i < 12; i++){ 10 40 63 99 10 40 63 99
\tavl.insert(arr[i]); 13 42 98 100 13 42 98 100
}
avl.remove(92);
avl.printTreeStructure();
AVLTree<int> avl; 42 42
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]); 13 63 98 100 13 63 98 100
}
avl.remove(52);
avl.printTreeStructure();
AVLTree<int> avl; 52 52
int arr[] = {10,52,98,32,68,92,40,13,42,63,99,100}; 32 68 32 68
for (int i = 0; i < 12; i++){ 10 40 63 92 10 40 63 92
\tavl.insert(arr[i]); 13 42 13 42
}
avl.remove(100);
avl.remove(98);
avl.remove(99);
avl.printTreeStructure();
AVLTree<int> avl; 52 52
int arr[] = {10,52,98,32,68,92,40,13,42,63,99,100}; 32 99 32 99
for (int i = 0; i < 12; i++){ 10 40 92 100 10 40 92 100
\tavl.insert(arr[i]); 13 42 13 42
}
Test Expected Got
avl.remove(63);
avl.remove(98);
avl.remove(68);
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]); 13 42 63 98 100 13 42 63 98 100
}
avl.remove(0);
avl.remove(1);
avl.remove(8);
avl.printTreeStructure();
AVLTree<int> avl; 42 42
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++){ 13 40 68 99 13 40 68 99
\tavl.insert(arr[i]); 63 100 63 100
} 42 42
avl.remove(10); 13 92 13 92
avl.remove(52); 40 63 99 40 63 99
avl.remove(98); 100 100
avl.printTreeStructure();
avl.remove(32);
avl.remove(68);
avl.printTreeStructure();
EX6:
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.
#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;
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
} 68
avl.printTreeStructure();
//Helping functions
int balanceFactor (Node* _root) {
return getHeightRec(_root->pLeft) - getHeightRec(_root->pRight);
}
EX7:
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
void printInorder(){
//TODO
recursivePrintIn(this->root);
}
void recursivePrintIn (Node* root){
if (root != nullptr){
recursivePrintIn (root->pLeft);
cout<< root->data<<' ';
recursivePrintIn (root->pRight);
}
}
bool search(const T &value){
//TODO
return recursiveSearch (this->root, value);
}
bool recursiveSearch (Node* root, const T &value){
if (root == nullptr)
return false;
if (value < root->data)
return recursiveSearch (root->pLeft, value);
else if (value > root->data)
return recursiveSearch (root->pRight, value);
else
return true;
}
void clear(){};
AVLTree<int> avl; 10 13 32 40 42 52 63 68 92 98 99 10 13 32 40 42 52 63 68 92 98
int arr[] = 100 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);
AVLTree<int> avl; 10 13 32 40 42 52 63 68 92 98 99 10 13 32 40 42 52 63 68 92 98
int arr[] = 100 99 100
{10,52,98,32,68,92,40,13,42,63,99,100}; 0 0
for (int i = 0; i < 12; i++){
\tavl.insert(arr[i]);
}
avl.printInorder();
cout << endl;
cout << avl.search(0);
AVLTree<int> avl; 0 0
int arr[] = 10 13 32 40 42 52 63 68 92 98 99 10 13 32 40 42 52 63 68 92 98
{10,52,98,32,68,92,40,13,42,63,99,100}; 100 99 100
for (int i = 0; i < 12; i++){ 1 1
\tavl.insert(arr[i]); 10 13 32 40 42 45 52 63 68 92 98 10 13 32 40 42 45 52 63 68 92
} 99 100 98 99 100
cout << avl.search(0);
cout << endl;
avl.printInorder();
cout << endl;
avl.insert(45);
cout << avl.search(45);
cout << endl;
avl.printInorder();
AVLTree<int> avl; 0 0
avl.printInorder();
cout << avl.search(0);
Test Expected Got
EX8:
Your task is to implement heap sort (in ascending order) on an unsorted array.
#define SEPARATOR "#<ab@17943918#@>#"
#ifndef SORTING_H
#define SORTING_H
#include <iostream>
using namespace std;
template <class T>
class Sorting {
public:
/* Function to print an array */
static void printArray(T *start, T *end)
{
long size = end - start;
for (int i = 0; i < size - 1; i++)
cout << start[i] << ", ";
cout << start[size - 1];
cout << endl;
}
};
#endif /* SORTING_H */
For example:
Test Result
int arr[4]={4,2,9,1}; 1, 2, 4, 9
Test Result
Sorting<int>::heapSort(&arr[0],&arr[4]);
}
}
static void heapSort(T* start, T* end){
//TODO
long size = end - start;
int pos = size/2 - 1;
while (pos>= 0){
reHeapDown (start,pos, size - 1);
pos = pos - 1;
}
int last = size - 1;
while (last > 0){
swap (start[0], start[last]);
last = last - 1;
reHeapDown(start,0, last);
}
Sorting<T>::printArray(start,end);
}
Test Expected Got
int arr[4]={4,2,9,1}; 1, 2, 4, 9 1, 2, 4, 9
Sorting<int>::heapSort(&arr[0],&arr[4]);
EX10:
Implement functions: Peek, Pop, Size, Empty, Contains to a maxHeap. If the function cannot
execute, return -1.
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define SEPARATOR "#<ab@17943918#@>#"
template<class T>
class Heap {
protected:
T* elements;
int capacity;
int count;
public:
Heap()
{
this->capacity = 10;
this->count = 0;
this->elements = new T[capacity];
}
~Heap()
{
delete[]elements;
}
void push(T item);
void clear();
bool isEmpty();
bool contains(T item);
T peek();
bool pop();
int size();
void printHeap()
{
cout << "Max Heap [ ";
for (int i = 0; i < count; i++)
cout << elements[i] << " ";
cout << "]\n";
}
private:
void ensureCapacity(int minCapacity);
void reheapUp(int position);
void reheapDown(int position);
};
//Your code goes here
For example:
Test Result
Heap<int> maxHeap; 10
for (int i=0;i<10;i++){
maxHeap.push(i);
}
cout << maxHeap.size();
Heap<int> maxHeap; 0
for (int i=0;i<10;i++){
maxHeap.push(i);
}
cout << maxHeap.isEmpty();
template<class T>
int Heap<T>::size(){
return this->count;
}
template<class T>
bool Heap<T>::isEmpty(){
if (this->count == 0) return true;
return false;
}
template<class T>
T Heap<T>::peek(){
if (this->size() == 0) return -1;
return *elements;
}
template<class T>
bool Heap<T>::contains(T item){
bool flag = false;
if (this->size() == 0) return false;
for (int i=0; i<(this->size()-1); i++){
if (*(elements+i) == item)
flag = true;
}
return flag;
}
template<class T>
void reHeapDown (T &elements, int pos, int lastPos){
int leftchild = 2*pos + 1;
int rightchild = 2*pos + 2;
int largechild = 0;
if (leftchild <= lastPos){
if ((rightchild <= lastPos) && (*(elements+rightchild) > *(elements+leftchild)) )
largechild = rightchild;
else
largechild = leftchild;
if (*(elements+largechild) > *(elements+pos)){
swap (*(elements+largechild), *(elements+pos));
reHeapDown (elements, largechild, lastPos);
}
}
}
template<class T>
void swap (T* a, T* b){
T temp = *a;
*a = *b;
*b = temp;
}
template<class T>
bool Heap<T>::pop(){
/*if (this->size() == 0) return false;
for (int i=0; i<(this->size()-1); i++){
*(this->elements+i) = *(this->elements+i+1);
}
this->count--;*/
if (this->size() == 0) return false;
int lastPos = this->count -1;
*elements = *(elements+lastPos);
lastPos--;
this->count--;
reHeapDown (elements, 0, lastPos);
return true;
}
Heap<int> maxHeap; 10 10
for (int i=0;i<10;i++){
maxHeap.push(i);
}
cout << maxHeap.size();
Heap<int> maxHeap; 0 0
for (int i=0;i<10;i++){
maxHeap.push(i);
}
cout << maxHeap.isEmpty();
Passed all tests!
EX11:
Implement function push to push a new item to a maxHeap. You also have to
implement ensureCapacity and reheapUp to help you achieve that.
template
class Heap{
protected:
T *elements;
int capacity;
int count;
public:
Heap()
this->capacity = 10;
this->count = 0;
~Heap()
delete []elements;
void printHeap()
private:
};
For example:
Test Result
template<class T>
void Heap<T>::push(T item){
if (this->count == this->capacity)
ensureCapacity(this->capacity);
int lastPos = this->count;
*(elements+lastPos) = item;
this->count++;
reheapUp(lastPos);
}
template<class T>
void Heap<T>::ensureCapacity(int minCapacity){
this->capacity = (this->capacity*3)/2 + 1;
T *temp = new T[this->capacity];
for (int i=0; i<(this->count); i++)
*(temp+i) = *(elements+i);
delete[] elements;
elements = nullptr;
elements = temp;
}
template<class T>
void swap (T* a, T* b){
T temp = *a;
*a = *b;
*b = temp;
}
template<class T>
void Heap<T>::reheapUp(int position){
if (position > 0){
int parent = (position - 1) / 2;
if (*(elements+position) > *(elements+parent)){
swap (*(elements+position), *(elements+parent));
reheapUp (parent);
}
}
}
Test Expected Got
EX12:
Given an array which the elements in it are random. Now we want to build a Max heap from this
array. Implement functions Reheap up and Reheap down to heapify element at index position. We
will use it to build a heap in next question.
To keep things simple, this question will separate the heap array, not store it in the class heap
void reheapDown(int maxHeap[], int numberOfElements, int index);
void reheapUp(int maxHeap[], int numberOfElements, int index);
}
}
void reheapUp(int maxHeap[], int numberOfElements, int index)
{
if (index > 0){
int parent = (index - 1) / 2;
if (maxHeap[index] > maxHeap[parent]){
swap (maxHeap[index], maxHeap[parent]);
reheapUp (maxHeap, numberOfElements, parent);
}
}
}
int arr[] = [ 14 2 13 4 5 1 3 8 9 10 11 12 6 7 [ 14 2 13 4 5 1 3 8 9 10 11 12 6 7
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 15 ] 15 ]
int size = sizeof(arr)/sizeof(arr[0]);
reheapUp(arr,size,13);
reheapUp(arr,size,12);
cout << "[ ";
for(int i=0;i<size;i++)
cout << arr[i] << " ";
cout << "]";
Your code failed one or more hidden tests.
EX13:
Implement method remove to remove the element with given value from a maxHeap, clear to
remove all elements and bring the heap back to the initial state. You also have to implement
method getItem to help you. Some given methods that you don't need to implement again
are push, printHeap, ensureCapacity, reheapUp, reheapDown.
class Heap {
protected:
T* elements;
int capacity;
int count;
public:
Heap()
{
this->capacity = 10;
this->count = 0;
this->elements = new T[capacity];
}
~Heap()
{
delete[]elements;
}
void push(T item);
int getItem(T item);
void remove(T item);
void clear();
void printHeap()
{
cout << "Max Heap [ ";
for (int i = 0; i < count; i++)
cout << elements[i] << " ";
cout << "]\n";
}
private:
void ensureCapacity(int minCapacity);
void reheapUp(int position);
void reheapDown(int position);
};
For example:
Test Result
maxHeap.remove(42);
maxHeap.remove(35);
maxHeap.remove(30);
maxHeap.printHeap();
template<class T>
int Heap<T>::getItem(T item) {
// TODO: return the index of item in heap
int pos = -1;
for (int i=0; i<this->count-1; i++){
if (*(elements+i) == item)
pos = i;
}
return pos;
}
template<class T>
void swap (T* a, T* b){
T temp = *a;
*a = *b;
*b = temp;
}
template<class T>
void Heap<T>::remove(T item) {
// TODO: remove the element with value equal to item
int lastPos = this->count - 1;
int pos = getItem (item);
if (pos == -1) return;
*(elements+pos) = *elements + 1;
reheapUp (pos);
*elements = *(elements + lastPos);
this->count--;
reheapDown (0);
}
template<class T>
void Heap<T>::clear() {
// TODO: delete all elements in heap
delete[] this->elements;
this->elements = nullptr;
this->count = 0;
}
Test Expected Got