Relaxed AVL trees (10 marks) In class, we saw that AVL trees satisfy the height-
ID: 3587217 • Letter: R
Question
Relaxed AVL trees (10 marks) In class, we saw that AVL trees satisfy the height-balance property, which means that for every internal node, the left child subtree and the right child subtree have height differing by at most 1. Consider a slight relaxation of the height-balance requirement, where, for every internal node, the left child subtree and the right child subtree have height which differs by at most 2. Let's call these trees "Relaxed AVL trees". Define N(h) to be the minimum number of elements that can be stored in a Relaxed AVL tree. Prove that N(h) = (kh) for some constant k 1, Note that you do not have to find the largest k for which this holds. OneExplanation / Answer
1) Left subtree of T is balanced
2) Right subtree of T is balanced
3) The difference between heights of left subtree and right subtree is not more than 1.
balance factor = height(left sub tree) - height(right sub tree)
Alogorithm:
#include<stdio.h>
#include<stdlib.h>
#define bool int
struct node
{
int data;
struct node* left;
struct node* right;
};
int height(struct node* node);
bool isBalanced(struct node *root)
{
int lh;
int rh;
if(root == NULL)
return 1;
lh = height(root->left);
rh = height(root->right);
if( abs(lh-rh) <= 1 &&
isBalanced(root->left) &&
isBalanced(root->right))
return 1;
return 0;
}
int max(int a, int b)
{
return (a >= b)? a: b;
}
int height(struct node* node)
{
if(node == NULL)
return 0;
return 1 + max(height(node->left), height(node->right));
}
struct node* newNode(int data)
{
struct node* node = (struct node*)
malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->left->left->left = newNode(8);
if(isBalanced(root))
printf("Tree is balanced");
else
printf("Tree is not balanced");
getchar();
return 0;
}