Exemple #1
0
// number of black nodes on paths from x to leaves
static bool is_balanced(rba_buffer_t *b, uint32_t x, uint32_t *h) {
  uint32_t i, j;
  uint32_t hi, hj;

  if (x == 0) {
    assert(is_black(b, x));
    *h = 1;
    return true;
  }

  i = b->child[x][0]; // left child
  j = b->child[x][1]; // right child
  if (is_balanced(b, i, &hi) && is_balanced(b, j, &hj)) {
    if (hi == hj) {
      if (is_black(b, x)) {
	hi ++;
      }
      *h = hi;

      return true;
    } else {
      printf("unbalanced tree at node %"PRIu32"\n", x);
      printf("   left child = %"PRIu32",  black height = %"PRIu32"\n", i, hi);
      printf("  right child = %"PRIu32",  black height = %"PRIu32"\n", j, hj);
      fflush(stdout);
    }
  }
  return false;
}
Exemple #2
0
void test()
{
    node_t* root = build_n_tree(30, 0);
    bfs(root);
    node_t* root2 = build_n_tree(50, 0);
    bfs(root2);
    node_t* root3 = build_n_tree(90, 0);
    bfs(root3);
    node_t* root4 = build_n_tree(1000, 0);
    bfs(root4);
    node_t* root5 = build123();
    bfs(root5);
    
    printf("balanced? %d\n", is_balanced(root));
    printf("balanced? %d\n", get_height_balanced(root));
    
    printf("balanced? %d\n", is_balanced(root2));
    printf("balanced? %d\n", get_height_balanced(root2));
    
    printf("balanced? %d\n", is_balanced(root3));
    printf("balanced? %d\n", get_height_balanced(root3));
    
    printf("balanced? %d\n", is_balanced(root4));
    printf("balanced? %d\n", get_height_balanced(root4));
    
    printf("balanced? %d\n", is_balanced(root5));
    printf("balanced? %d\n", get_height_balanced(root5));
}
Exemple #3
0
int is_balanced(node_t* root)
{
    if (root == NULL) return 1;

    if (abs(get_height(root->left) - get_height(root->right)) > 1)
        return 0;
    else
        return is_balanced(root->left) && is_balanced(root->right);
}
Exemple #4
0
int is_balanced(bst *root) {

    if(root == 0)	return 0;
    int lt = is_balanced(root->left);
    int rt = is_balanced(root->right);
    if (abs(lt - rt) > 1 || lt < 0 || rt < 0)
        return 0;
    return max(lt,rt) + 1; //add root weight = 1;

}
Exemple #5
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));
}
bool is_balanced(char *p, int c) {
	if (*p == '\0') {
		return c == 0;
	} else if (c < 0) {
		return false;
	}
	while (p[0] != ':' && p[0] != '(' && p[0] != ')')
		return is_balanced(p + 1, c);
	trim_right(p);
	switch (p[0]) {
	case '(':
		return is_balanced(p + 1, c + 1);
	case ')':
		return is_balanced(p + 1, c - 1);
	case ':':
		switch (p[1]) {
		case '(':
			return is_balanced(p + 2, c) ||
			       is_balanced(p + 2, c + 1);
		case ')':
			return is_balanced(p + 2, c) ||
			       is_balanced(p + 2, c - 1);
		default:
			return is_balanced(p + 1, c);
		}
	default:
		return false;
	}
}
int main(int argc, char *argv[])
{
	FILE *fp;
	int s = 0, sbs = 32;
	char c;
	char *sb = malloc(sbs);

	fp = fopen(*++argv, "r");
	while ((c = getc(fp)) != EOF || s > 0) {
		if (c == '\n' || c == EOF) {
			sb[s] = '\0';
			if (is_balanced(sb, 0))
				puts("YES");
			else
				puts("NO");
			s = 0;
		} else {
			if (s == sbs - 1) {
				sbs += sbs / 2;
				sb = realloc(sb, sbs);
			}
			sb[s++] = c;
		}
	}
	free(sb);
	return 0;
}
Exemple #8
0
int main()
{
	struct Tnode *root = NULL;
	root = create_node(1);
	root->left = create_node(2);
	root->right = create_node(3);
	root->left->left = create_node(4);
	printf("Is Balanced Tree : %d\n", is_balanced(root));
}
  bool is_balanced(TreeNode *root, int &height) {
    if (NULL == root) {
      height = 0;

      return true;
    }

    int left_height;
    int right_height;
    bool is_left_balanced = is_balanced(root->left, left_height);
    bool is_right_balanced = is_balanced(root->right, right_height);

    height = 1 + max(left_height, right_height);

    if (is_left_balanced && is_right_balanced) {
      return (abs(left_height - right_height) <= 1) ? true : false;
    }
    else {
      return false;
    }
  }
Exemple #10
0
/*
 * Driver function
 */
int main(int argc, char *argv[])
{
	struct t_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);

	if (true == is_balanced(root))
		printf("Tree is balanced\n");
	else
		printf("Tree is NOT balanced\n");

	return 0;
}
Exemple #11
0
int main( int argc, char ** argv )
{
  int i;
  tree * t;

  srand( time(NULL) );

  t = NULL;
  for( i = 0; i < 4; ++i )
    {
      add( &t, rand() % 500 );
    }

  inorder( t );
  printf("\n");
  is_balanced( t );
  return 0;
}
Exemple #12
0
int main(void) {
    printf("Testing if balanced binary tree 1 reports as balanced\n");
    assert(is_balanced(balanced_binary_tree_1()));
    printf("Testing if balanced binary tree 2 reports as balanced\n");
    assert(is_balanced(balanced_binary_tree_2()));
    printf("Testing if balanced binary tree 3 reports as balanced\n");
    assert(is_balanced(balanced_binary_tree_3()));
    printf("Testing if balanced binary tree 4 reports as balanced\n");
    assert(is_balanced(balanced_binary_tree_4()));
    printf("Testing if balanced binary tree 5 reports as balanced\n");
    assert(is_balanced(balanced_binary_tree_5()));
    printf("Testing if unbalanced binary tree 1 reports as unbalanced\n");
    assert(is_balanced(unbalanced_binary_tree_1()) == 0);
    printf("Testing if unbalanced binary tree 2 reports as unbalanced\n");
    assert(is_balanced(unbalanced_binary_tree_2()) == 0);
    return 0;
}
Exemple #13
0
int main(int argc, char* argv[])
{
    if (argc < 2){
        printf("\nUsage: %s <brainfuck-code.txt>\n", argv[0]);
        free_memory();
        exit(EXIT_FAILURE);
    }
    clean_memory();
    reset_pointer();
    
    load_program(argv[1]);
    if (!is_balanced(program, program_size)){
        printf("ERROR: Unbalanced brackets found in the supplied Brainfuck code.\n");
        free_memory();
        exit(EXIT_FAILURE);
    }
    execute_program();
    free_memory();
    return 0;
}
Exemple #14
0
/*
 * Tests: add and remove
 */
static void run_tests(rba_buffer_t *b) {
  uint32_t i, h, n;

  check_tree(b);
  init_tests();

  // add all power products
  n = NUM_TESTS;
  for (i=0; i<n; i++) {
    test_add(b, test[i]);
  }
  printf("\nAfter %"PRIu32" additions\n", n);
  printf("   num_nodes = %"PRIu32"\n", b->num_nodes);
  printf("   num_terms = %"PRIu32"\n", b->nterms);
  printf("   root node = %"PRIu32"\n", b->root);
  if (is_balanced(b, b->root, &h)) {
    printf("   b-height    = %"PRIu32"\n", h);
    printf("   full height = %"PRIu32"\n", tree_height(b));
  } else {
    printf("   not balanced\n");
  }
  printf("\n");

  // remove half
  n = NUM_TESTS/2;
  for (i=0; i<n; i++) {
    test_remove(b, test[i]);
  }
  printf("\nAfter %"PRIu32" removals\n", n);
  printf("   num_nodes = %"PRIu32"\n", b->num_nodes);
  printf("   num_terms = %"PRIu32"\n", b->nterms);
  printf("   root node = %"PRIu32"\n", b->root);
  if (is_balanced(b, b->root, &h)) {
    printf("   b-height    = %"PRIu32"\n", h);
    printf("   full height = %"PRIu32"\n", tree_height(b));
  } else {
    printf("   not balanced\n");
  }

  // add them back
  for (i=0; i<n; i++) {
    test_add(b, test[i]);
  }
  printf("\nAfter %"PRIu32" additions\n", n);
  printf("   num_nodes = %"PRIu32"\n", b->num_nodes);
  printf("   num_terms = %"PRIu32"\n", b->nterms);
  printf("   root node = %"PRIu32"\n", b->root);
  if (is_balanced(b, b->root, &h)) {
    printf("   b-height    = %"PRIu32"\n", h);
    printf("   full height = %"PRIu32"\n", tree_height(b));
  } else {
    printf("   not balanced\n");
  }
  printf("\n");


  // Try again after reset
  reset_rba_buffer(b);
  n = NUM_TESTS/2;
  i = n;
  while (i > 0) {
    i --;
    test_add(b, test[i]);
  }

  printf("\nAfter %"PRIu32" additions\n", n);
  printf("   num_nodes = %"PRIu32"\n", b->num_nodes);
  printf("   num_terms = %"PRIu32"\n", b->nterms);
  printf("   root node = %"PRIu32"\n", b->root);
  if (is_balanced(b, b->root, &h)) {
    printf("   b-height    = %"PRIu32"\n", h);
    printf("   full height = %"PRIu32"\n", tree_height(b));
  } else {
    printf("   not balanced\n");
  }
  printf("\n");

  i = n;
  while (i > 0) {
    i --;
    test_remove(b, test[i]);
  }

  printf("\nAfter %"PRIu32" removals\n", n);
  printf("   num_nodes = %"PRIu32"\n", b->num_nodes);
  printf("   num_terms = %"PRIu32"\n", b->nterms);
  printf("   root node = %"PRIu32"\n", b->root);
  if (is_balanced(b, b->root, &h)) {
    printf("   b-height    = %"PRIu32"\n", h);
    printf("   full height = %"PRIu32"\n", tree_height(b));
  } else {
    printf("   not balanced\n");
  }



  fflush(stdout);
}
Exemple #15
0
static bool tree_is_balanced(rba_buffer_t *b) {
  uint32_t h;
  return is_balanced(b, b->root, &h);
}
Exemple #16
0
bool is_balanced(tree* T) {
  if (T == NULL) return true;
  return abs(height(T->left) - height(T->right)) <= 1
    && is_balanced(T->left) && is_balanced(T->right);
}
Exemple #17
0
bool is_tree(tree* T, bst B) {
  return is_ordered(T, NULL, NULL, B)
    && is_specified_height(T)
    && is_balanced(T);
}
Exemple #18
0
int main(void) {
    std::cout << is_balanced("[])") << std::endl;
}
Exemple #19
0
void checkForBalancedParensBracketsBraces( char * filename )
{
	FILE * fp = fopen(filename,"rb");
	if( !fp )
	{
		fprintf(stderr,"Error opening file %s, %s\n",filename,strerror(errno));
		return;
	}

	int MAX_CHARS = 1024;
	int nCharsRead;
	char buf[MAX_CHARS];
	char * charsRead;
	char lastChar;
	char curChar;

	struct char_stack * cs = char_stack_init();

	int lineno = 1;

   while( (charsRead = fgets(buf,MAX_CHARS,fp)) != NULL )
   {
      nCharsRead = strlen(charsRead);
      for(int i = 0; i < nCharsRead; i++)
      {
         curChar = charsRead[i];
			if(curChar == '\n')
				lineno++;

			switch(curChar)
			{
				case '(':
				case '[':
				case '{':
					cs = char_stack_push( cs,curChar);
					break;

				case ')':
					if( ! is_balanced( cs, '(') )
						printf("Out of balance at line %d, found %c, expected to match %c\n",
							lineno, cs->c, '(');
					cs = char_stack_pop(cs);
					break;

				case ']':
					if( ! is_balanced( cs, '[') )
						printf("Out of balance at line %d, found %c, expected to match %c\n",
							lineno, cs->c, '[');
					cs = char_stack_pop( cs );
					break;
				case '}':
					if( ! is_balanced( cs, '{') )
						printf("Out of balance at line %d, found %c, expected to match %c\n",
							lineno, cs->c, '{');
					cs = char_stack_pop( cs );
					break;

			}
		}
	}

	if( ! char_stack_is_empty(cs) )
	{
		printf("Out of balance at end of source, dumping what's left\n");
		char_stack_dump(cs,stdout);
	}
	//char_stack_free(cs);
}
  bool isBalanced(TreeNode *root) {
    int height;

    return is_balanced(root, height);
  }