AVL Tree (Bonus)
Time limit: 500ms
Memory limit: 256mb
Description:
Please complete the missing code of the AVL Tree ADT, such that the program could output a correct answer.
1. llRotation
2. rrRotation
3. lrRotation
4. rlRotation
5. insertNode
6. deleteNode
-------------------------Copy the following code, complete it and submit-------------------------
/*
I, , am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct AVLTreeNode{
int data;
int height;
struct AVLTreeNode *left;
struct AVLTreeNode *right;
};
struct AVLTreeNode *createNode(int x) {
struct AVLTreeNode *node = (struct AVLTreeNode *)malloc(sizeof(struct AVLTreeNode));
node->data = x;
node->height = 0;
node->left = NULL;
node->right = NULL;
return node;
}
struct AVLTreeNode * Lchild(struct AVLTreeNode * root) {
return root->left;
}
struct AVLTreeNode * Rchild(struct AVLTreeNode * root) {
return root->right;
}
int Data(struct AVLTreeNode * root) {
return root->data;
}
bool isEmpty(struct AVLTreeNode * root) {
return root == NULL;
}
int max(int a, int b) {
if (a > b) {
return a;
}
else {
return b;
}
}
int Height(struct AVLTreeNode * root) {
if (isEmpty(root)) {
return 0;
}
return 1+max(Height(root->left), Height(root->right));
}
// left-left imbalance
struct AVLTreeNode *llRotation(struct AVLTreeNode *y) {
// write your code here
}
// right-right imbalance
struct AVLTreeNode *rrRotation(struct AVLTreeNode *x) {
// write your code here
}
// left-right imbalance
struct AVLTreeNode *lrRotation(struct AVLTreeNode *y) {
// write your code here
}
// right-left imbalance
struct AVLTreeNode *rlRotation(struct AVLTreeNode *x) {
// write your code here
}
int balanceFactor(struct AVLTreeNode *root)
{
if (root == NULL) {
return 0;
}
return Height(root->left) - Height(root->right);
}
struct AVLTreeNode * insertNode(struct AVLTreeNode * root, int x) {
// write your code here
}
struct AVLTreeNode* deleteNode(struct AVLTreeNode* root, int x) {
// write your code here
}
void Preorder(struct AVLTreeNode *root) {
if (!isEmpty(root)) {
printf("%d ", Data(root));
Preorder(Lchild(root));
Preorder(Rchild(root));
}
}
void Inorder(struct AVLTreeNode *root) {
if (!isEmpty(root)) {
Inorder(Lchild(root));
printf("%d ", Data(root));
Inorder(Rchild(root));
}
}
void Postorder(struct AVLTreeNode *root) {
if (!isEmpty(root)) {
Postorder(Lchild(root));
Postorder(Rchild(root));
printf("%d ", Data(root));
}
}
int main() {
int n, m, x;
struct AVLTreeNode * root = NULL;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
root = insertNode(root, x);
}
printf("Preorder: ");
Preorder(root);
printf("\nInorder: ");
Inorder(root);
printf("\nPostorder: ");
Postorder(root);
printf("\n");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
root = deleteNode(root, x);
}
printf("Preorder: ");
Preorder(root);
printf("\nInorder: ");
Inorder(root);
printf("\nPostorder: ");
Postorder(root);
printf("\n");
return 0;
}
-------------------------------------------End of Code-------------------------------------------
Input:
The first line contains a positive integer N;
The second line contains an array of N integers, a_0, a_1, ..., a_(N-1);
The third line contains a positive integer M;
The second line contains an array of M integers, b_0, b_1, ..., b_(M-1);
Output:
The Preorder, Inorder, Postorder after N insertion operations.
The Preorder, Inorder, Postorder after M deletion operations.
Sample Input 1:
10
7 9 11 5 4 6 8 12 15 10
5
11 12 8 6 9
Sample Output 1:
Preorder: 7 5 4 6 11 9 8 10 12 15
Inorder: 4 5 6 7 8 9 10 11 12 15
Postorder: 4 6 5 8 10 9 15 12 11 7
Preorder: 7 5 4 10 15
Inorder: 4 5 7 10 15
Postorder: 4 5 15 10 7
Submit