Exemple #1
0
/*
 * @brief	Checks if the tree is balanced or not
 */
bool is_balanced(struct t_node *root)
{
	if (root == NULL)
		return true;
	int l_height = find_height(root->left);
	int r_height = find_height(root->right);
	return (abs(l_height - r_height) <= 1 &&
		is_balanced(root->left) &&
		is_balanced(root->right));
}
 int FindMinDepth (Node* node) {
   int height = 0;
   if (node != 0) {
     //calculate each AVL subtree
     int left_height = find_height (node->left);
     int right_height = find_height (node->right);
     //NOTE here the parenthese on the right is necessary
     height = 1 + ( (left_height > right_height) ? right_height : left_height);
   }
   return height;
 }
void set_diameter(node* root)
{
    if (!root)
        return ;
    set_diameter(root->left);
    int l = find_height(root->left);
    int r = find_height(root->right);
    if (l+r+1 > max_diameter)
        max_diameter = l+r+1+1;// 1 is added to count the parent node in that path, and another 1 is added because when we are calculating height of LST and RST then both of them are starting from 0, but while counting the diameter we need to count the number of nodes, so we add one.
    set_diameter(root->right);
}
Exemple #4
0
int find_height(avl_node *node) {
    int height = 0;
    
    if (node != NULL) {
        
        int left_height = find_height(node->left);
        int right_height = find_height(node->right);
        
        int max = (left_height > right_height) ? left_height : right_height;
        
        height = 1 + max;
    }
    
    return height;
}
Exemple #5
0
/*
 * Driver function
 */
int main(int argc, char *argv[])
{
	struct node *root = add_node(1);
	root->left = add_node(2);
	root->right = add_node(3);
	root->left->left = add_node(4);
	root->left->right = add_node(5);
	root->right->left = add_node(6);
	root->right->right = add_node(7);

	printf("Size: %d\n", find_height(root));

	return 0;
}
int find_height(node_t root)
{
	if(root == NULL) return 0;

	return 1 + max(find_height(root->llink),find_height(root->rlink));
}
 int getHeight() {
   return find_height (root);
 }
 //get the balanced factor
 int getFactor (Node* node) {
   int left_height = find_height (node -> left);
   int right_height = find_height (node -> right);
   int b_factor = left_height - right_height;
   return b_factor;
 }