コード例 #1
0
ファイル: heightbalance.c プロジェクト: rurtle/PracticeProbs
/*
 * @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));
}
コード例 #2
0
ファイル: BinaryTree(AVL NR).cpp プロジェクト: cdht/Algorithm
 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;
 }
コード例 #3
0
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);
}
コード例 #4
0
ファイル: avltree.c プロジェクト: joaopcanario/avl_tree
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;
}
コード例 #5
0
ファイル: height.c プロジェクト: rurtle/PracticeProbs
/*
 * 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;
}
コード例 #6
0
int find_height(node_t root)
{
	if(root == NULL) return 0;

	return 1 + max(find_height(root->llink),find_height(root->rlink));
}
コード例 #7
0
ファイル: BinaryTree(AVL NR).cpp プロジェクト: cdht/Algorithm
 int getHeight() {
   return find_height (root);
 }
コード例 #8
0
ファイル: BinaryTree(AVL NR).cpp プロジェクト: cdht/Algorithm
 //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;
 }