Exemplo n.º 1
0
int tree_balanced (struct Node * tree)
{
	if (tree == NULL)
		return 1;
	else
		if (tree_balanced(tree->left) && tree_balanced(tree->right) && ((tree_height(tree->left) - tree_height(tree->right)) <= 1) && ((tree_height(tree->left) - tree_height(tree->right)) >= -1))
			return 1;
		else
			return 0;		
}
Exemplo n.º 2
0
Arquivo: 4.1.c Projeto: kkpattern/CTCI
int main() {
  MemoryPool *pool = memory_pool_init(sizeof(int) * 100);

  BinarySearchTree *tree = binary_search_tree_init(int_compare_by_pointer,
                                                   int_compare_by_pointer);
  binary_search_tree_insert(tree, memory_pool_alloc_int(pool, 2), NULL);
  binary_search_tree_insert(tree, memory_pool_alloc_int(pool, 1), NULL);
  binary_search_tree_insert(tree, memory_pool_alloc_int(pool, 3), NULL);
  int height = 0;
  assert(true == tree_balanced(tree));
  tree = binary_search_tree_free(tree);

  // Construct an almost balanced tree.
  tree = binary_search_tree_init(int_compare_by_pointer,int_compare_by_pointer);
  int keys[] = {5, 3, 6, 2, 4, 1};
  for (size_t i = 0; i < sizeof(keys)/sizeof(int); ++i) {
    binary_search_tree_insert(tree, memory_pool_alloc_int(pool, keys[i]), NULL);
  }
  assert(false == tree_balanced(tree));
  tree = binary_search_tree_free(tree);

  pool = memory_pool_free(pool);
}
Exemplo n.º 3
0
int main()
{
	struct Node * tree = NULL;
	
	int nn;
	scanf("%d", &nn);
	while (nn > 0)
	{
		tree = tree_add(tree, nn);
		scanf("%d", &nn);
	};

	printf("%s\n", tree_balanced(tree)?"YES":"NO");

	tree_destroy(tree);

	return 0;
}