0% found this document useful (0 votes)
10 views5 pages

AVL

Uploaded by

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

AVL

Uploaded by

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

1.

Node* rotateRight(Node* root) {


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

leftNode->pRight = root;
root->pLeft = rightleftNode;
return leftNode;
}

Node* rotateLeft(Node* root) {


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

rightNode->pLeft = root;
root->pRight = leftrightNode;
return rightNode;
}

2.
//Helping functions
Node* rightRotate(Node* node) {
Node* leftNode = node->pLeft;
Node* rightleftNode = leftNode->pRight;

leftNode->pRight = node;
node->pLeft = rightleftNode;
return leftNode;
}

Node* leftRotate(Node* node) {


Node* rightNode = node->pRight;
Node* leftrightNode = rightNode->pLeft;

rightNode->pLeft = node;
node->pRight = leftrightNode;
return rightNode;
}

int balance(Node* node) {


if (node == NULL) {
return 0;
}
return getHeightRec(node->pLeft) - getHeightRec(node->pRight);
}

Node* insert(Node* node, const T &value) {


if (node == NULL) {
Node* newNode = new Node(value);
return newNode;
}
if (value < node->data) {
node->pLeft = insert(node->pLeft, value);
}
else if (value >= node->data) {
node->pRight = insert(node->pRight, value);
}
int bal = balance(node);
if (bal > 1 && value < node->pLeft->data) {
return rightRotate(node);
}
if (bal < -1 && value > node->pRight->data) {
return leftRotate(node);
}
if (bal > 1 && value > node->pLeft->data) {
node->pLeft = leftRotate(node->pLeft);
return rightRotate(node);
}
if (bal < -1 && value < node->pRight->data) {
node->pRight = rightRotate(node->pRight);
return leftRotate(node);
}
return node;
}

void insert(const T &value){


//TODO
root = insert(root, value);
}

3.
//Helping functions
Node* rightRotate(Node* node) {
Node* leftNode = node->pLeft;
Node* rightleftNode = leftNode->pRight;

leftNode->pRight = node;
node->pLeft = rightleftNode;
return leftNode;
}

Node* leftRotate(Node* node) {


Node* rightNode = node->pRight;
Node* leftrightNode = rightNode->pLeft;

rightNode->pLeft = node;
node->pRight = leftrightNode;
return rightNode;
}

int balance(Node* node) {


if (node == NULL) {
return 0;
}
return getHeightRec(node->pLeft) - getHeightRec(node->pRight);
}

Node* insert(Node* node, const T &value) {


if (node == NULL) {
Node* newNode = new Node(value);
return newNode;
}
if (value < node->data) {
node->pLeft = insert(node->pLeft, value);
}
else if (value >= node->data) {
node->pRight = insert(node->pRight, value);
}
int bal = balance(node);
if (bal > 1 && value < node->pLeft->data) {
return rightRotate(node);
}
if (bal < -1 && value > node->pRight->data) {
return leftRotate(node);
}
if (bal > 1 && value > node->pLeft->data) {
node->pLeft = leftRotate(node->pLeft);
return rightRotate(node);
}
if (bal < -1 && value < node->pRight->data) {
node->pRight = rightRotate(node->pRight);
return leftRotate(node);
}
return node;
}

void insert(const T &value){


//TODO
root = insert(root, value);
}

5.
//Helping functions
Node* minValue(Node* node) {
Node* itr = node;
while (itr->pRight != NULL) {
itr = itr->pRight;
}
return itr;
}

Node* rightRotate(Node* node) {


Node* leftNode = node->pLeft;
Node* rightleftNode = leftNode->pRight;

leftNode->pRight = node;
node->pLeft = rightleftNode;
return leftNode;
}

Node* leftRotate(Node* node) {


Node* rightNode = node->pRight;
Node* leftrightNode = rightNode->pLeft;

rightNode->pLeft = node;
node->pRight = leftrightNode;
return rightNode;
}

int balance(Node* node) {


if (node == NULL) {
return 0;
}
return getHeightRec(node->pLeft) - getHeightRec(node->pRight);
}
Node* remove(Node* node, const T &value) {
if (node == NULL) {
return NULL;
}
if (value < node->data) {
node->pLeft = remove(node->pLeft, value);
}
else if (value > node->data) {
node->pRight = remove(node->pRight, value);
}
else {
if (node->pLeft == NULL && node->pRight == NULL) {
delete node;
node = NULL;
}
else if (node->pLeft == NULL) {
Node* temp = node;
node = node->pRight;
delete temp;
}
else if (node->pRight == NULL) {
Node* temp = node;
node = node->pLeft;
delete temp;
}
else {
Node* temp = minValue(node->pLeft);
node->data = temp->data;
node->pLeft = remove(node->pLeft, temp->data);
}
}
if (node == NULL) {
return NULL;
}
int bal = balance(node);
if (bal > 1 && balance(node->pLeft) >= 0) {
return rightRotate(node);
}
if (bal > 1 && balance(node->pLeft) < 0) {
node->pLeft = leftRotate(node->pLeft);
return rightRotate(node);
}
if (bal < -1 && balance(node->pRight) <= 0) {
return leftRotate(node);
}
if (bal < -1 && balance(node->pRight) > 0) {
node->pRight = rightRotate(node->pRight);
return leftRotate(node);
}
return node;
}

void remove(const T &value){


//TODO
root = remove(root, value);
}

7.
void printInorder(Node* node) {
if (node == NULL) {
return;
}
else {
printInorder(node->pLeft);
cout << node->data << " ";
printInorder(node->pRight);
}
}

void printInorder(){
//TODO
printInorder(root);
}

bool search(Node* node, const T &value) {


if (node == NULL) {
return 0;
}
if (node->data == value) {
return 1;
}
return search(node->pLeft, value) || search(node->pRight, value);
}

bool search(const T &value){


//TODO
return search(root, value);
}

You might also like