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, <Your Full Name>, 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 summation_of_subtree;
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 = 1;
node->left = NULL;
node->right = NULL;
node->summation_of_subtree = x;
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 DynamicHeight(struct AVLTreeNode * root) {
if (isEmpty(root)) {
return 0;
}
return 1+max(DynamicHeight(root->left), DynamicHeight(root->right));
}
int StaticHeight(struct AVLTreeNode * root) {
if (isEmpty(root)) {
return 0;
}
return 1+max( root->left?root->left->height:0, root->right?root->right->height:0 );
}
int Height(struct AVLTreeNode * root) {
if (isEmpty(root)) {
return 0;
}
return root->height;
}
struct AVLTreeNode *llRotation(struct AVLTreeNode *y) {
// write down your code here
}
// right-right imbalance
struct AVLTreeNode *rrRotation(struct AVLTreeNode *x) {
// write down your code here
}
// left-right imbalance
struct AVLTreeNode *lrRotation(struct AVLTreeNode *y) {
// write down your code here
}
// right-left imbalance
struct AVLTreeNode *rlRotation(struct AVLTreeNode *x) {
// write down 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 down your code here
}
struct AVLTreeNode* deleteNode(struct AVLTreeNode* root, int x) {
// write down your code here
}
void Preorder(struct AVLTreeNode *root) {
if (!isEmpty(root)) {
printf("%d ", root->summation_of_subtree);
Preorder(Lchild(root));
Preorder(Rchild(root));
}
}
void Inorder(struct AVLTreeNode *root) {
if (!isEmpty(root)) {
Inorder(Lchild(root));
printf("%d ", root->summation_of_subtree);
Inorder(Rchild(root));
}
}
void Postorder(struct AVLTreeNode *root) {
if (!isEmpty(root)) {
Postorder(Lchild(root));
Postorder(Rchild(root));
printf("%d ", root->summation_of_subtree);
}
}
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), which are the N integers to be inserted into the tree;
The third line contains a positive integer M;
The fourth line contains an array of M integers, b_0, b_1, ..., b_(M-1), which are the M integers to be deleted from the tree;
Output:
After N insertion operations, run the Preorder traversal, Inorder traversal, Postorder traversal, respectively. When visiting a node, please report the summation of the subtree rooted at this node (The summation includes the value of this node).
After M deletion operations, run the Preorder traversal, Inorder traversal, Postorder traversal again, and report the summation of the subtrees.
Sample Input 1:
10
7 9 11 5 4 6 8 12 15 10
4
4 6 15 12
Sample Output 1:
Preorder: 87 15 4 6 65 27 8 10 27 15
Inorder: 4 15 6 87 8 27 10 65 27 15
Postorder: 4 6 15 8 10 27 15 27 65 87
Preorder: 50 20 5 8 21 10
Inorder: 5 20 8 50 10 21
Postorder: 5 8 20 10 21 50
Submit