Beispiel #1
0
int
btree_depth(const btree tree)
{
        int left_depth, right_depth;
        int max_depth;
        if (!tree) {
                return 0;
        }

        /* traverse left subtree */
        if (tree->left) {
                left_depth = btree_depth(tree->left);
        } else {
                left_depth = 0;
        }
        /* traverse right subtree */
        if (tree->right) {
                right_depth = btree_depth(tree->right);
        } else {
                right_depth = 0;
        }

        /* colon three-meta expression shoulde be careful */
        max_depth = left_depth > right_depth ? left_depth + 1: right_depth + 1;
        return max_depth;
}
//二元树的深度。
//调用之前,设置parent_depth=-1
int btree_depth(btree_node<int> *p_node, int parent_depth)
{
	if (p_node == NULL)
		return parent_depth;

	if (p_node->mp_left == NULL && p_node->mp_right == NULL)
		return parent_depth + 1;

	int left_child_depth = p_node->mp_left? btree_depth(p_node->mp_left, parent_depth+1): -1;
	int right_child_depth = p_node->mp_right? btree_depth(p_node->mp_right, parent_depth+1): -1;
	return std::max<int>(left_child_depth, right_child_depth);
}
int test_insert() {
  BTree *tree;
  BTree *find;
  int depth; 
  tree = NULL;
  
  printf("The numbers should be in ascending order.\n");
  
  btree_insert(&tree, "5");
  btree_insert(&tree, "1");
  btree_insert(&tree, "6");
  btree_insert(&tree, "9");
  btree_insert(&tree, "0");
  btree_insert(&tree, "4");
  btree_insert(&tree, "3");
  btree_insert(&tree, "8");
  btree_insert(&tree, "2");
  btree_insert(&tree, "7");
  btree_insert(&tree, "4");

  /*print_preorder(tree);*/
  printLevelOrder(tree,5);
  depth  = btree_depth(tree);
  printf("d_single:%d\n", depth);
  find = btree_find(tree, "Z");
  /*print_preorder(find);*/
  if(find == NULL){
    printf("Not found.\n");
  }else{
    printLevelOrder(find,5);
  }
  btree_free(tree);
  return 0;
}
int main() {
  BTree *tree;
  BTree *find;
  int depth;
  char *array[11];
  
  array[0] = "5";
  array[1] = "1";
  array[2] = "6";
  array[3] = "9";
  array[4] = "0";
  array[5] = "4";
  array[6] = "3";
  array[7] = "8";
  array[8] = "2";
  array[9] = "7";
  array[10] = "4";
  
  tree = array_to_btree(array);
  /*print_preorder(tree);*/
  printLevelOrder(tree,5);
  depth  = btree_depth(tree);
  printf("d_array:%d\n", depth);
  find = btree_find(tree, "A");
  /*print_preorder(find);*/
  if(find == NULL){
    printf("Not found.\n");
  }else{
    printLevelOrder(find,5);
  }
  btree_free(tree);
  test_insert();
  return 0;
}
Beispiel #5
0
static int btree_depth(BTREE_NODE *x)
{
	int l, r;

	if (x == NULL) {
		return (0);
	}

	l = btree_depth(x->left);
	r = btree_depth(x->right);

	if (l > r) {
		return (l + 1);
	} else {
		return (r + 1);
	}
}
Beispiel #6
0
static int
btree_depth(btree_node_t *x)
{
    int l, r;

    if (x == NULL) {
        return 0;
    }

    l = btree_depth(x->left);
    r = btree_depth(x->right);

    if (l > r) {
        return l + 1;
    } else {
        return r + 1;
    }
}
/**
 * btree_depth - Calculates the depth
 * of a given binary tree
 * @*tree: contains the structure address
 */
int btree_depth(BTree *tree)
{
  int right_node;
  int left_node;

  /*Checking if the tree is empty*/
  if (tree == NULL)
  {
    return (0);
  }
  /*using recursion to count the depth on each brach*/
  right_node = btree_depth(tree->right);
  left_node = btree_depth(tree->left);
  /*returning only the highest number of nodes*/
  if (right_node > left_node)
  {
    return (right_node + 1);
  }
  return (left_node + 1);
}
/**
 * btree_depth - Calculates the depth of a binary tree
 *
 * @tree: Pointer to root leaf of a binary tree
 *
 * Description:  Receives pointer to root node of a binary tree.  If pointer
 * points to NULL, the tree is empty.  Otherwise call the function recursively
 * and add 1 for the current leag
 */
int btree_depth(BTree *tree)
{
	int ldepth;
	int rdepth;

	if (tree == NULL)
	{
		return 0;
	}
	else
	{
		ldepth = btree_depth(tree->left);
		rdepth = btree_depth(tree->right);
	}
	if (ldepth > rdepth)
	{
		return (ldepth + 1);
	}
	else
	{
		return (rdepth + 1);
	}
	return 0;
}
Beispiel #9
0
int
main()
{
    btree_t *b;
    uint32_t i, *x;
    uint32_t v[] = {15, 5, 16, 3, 12, 20, 10, 13, 18, 23, 6, 7};
    uint32_t nv = sizeof(v) / sizeof(v[0]);

    btree_create(&b);

    for(i = 0; i < nv; i++) {
        x = (uint32_t*)xmalloc(sizeof(uint32_t));
        *x = (uint32_t)random();
        if (btree_add(b, v[i], (void*)x) != TRUE) {
            printf("Fail Add %lu %lu\n", v[i], *x);
        }
    }

    printf("depth %d\n", btree_depth(b->root));
    btree_dump(b);

    sleep(3);
    btree_remove(b, 5, (void*)&x);
    btree_dump(b);
    sleep(3);
    btree_remove(b, 16, (void*)&x);
    btree_dump(b);
    sleep(3);
    btree_remove(b, 13, (void*)&x);
    btree_dump(b);

    while (btree_get_root_key(b, &i)) {
        if (btree_remove(b, i, (void*)&x) == FALSE) {
            fprintf(stderr, "Failed to remove %lu\n", i);
        }
        btree_dump(b);
        sleep(1);
    }

    if (btree_destroy(&b) == FALSE) {
        printf("Failed to destroy \n");
    }

    return 0;
}
Beispiel #10
0
int main(int argc, char **argv)
{
        printf("-------------------------------------------------\n");
        printf("starts to have a test of binary tree.\n");
        printf("-------------------------------------------------\n");
        btree tree = btree_create();
        btree_node *dataone = btree_node_make("hello");
        btree_insert_child_tree(tree, PRE_ORDER, dataone);
        btree_node *datatwo = btree_node_make("World");
        btree_insert_child_tree(dataone, PRE_ORDER, datatwo);
        btree_traverse(tree, IN_ORDER, btree_node_print);
        printf("now length of tree is %d\n", btree_depth(tree));
        printf("-------------------------------------------------\n");
        printf("test is over, everything is gone be alright.\n");
        printf("-------------------------------------------------\n");
        printf("\n");

        return EXIT_SUCCESS;
}
btree_dtable::page_stack::page_stack(int fd, size_t key_count)
	: fd(fd), next_file_page(1), filled(false), flushed(false)
{
	depth = btree_depth(key_count);
	assert(depth > 0);
	pages = new page[depth];
	next_depth = depth - 1;
	
	header.magic = BTREE_DTABLE_MAGIC;
	header.version = BTREE_DTABLE_VERSION;
	header.page_size = BTREE_PAGE_SIZE;
	header.pageno_size = BTREE_PAGENO_SIZE;
	header.key_size = BTREE_KEY_SIZE;
	header.index_size = BTREE_INDEX_SIZE;
	header.key_type = 1; /* 1 -> uint32 */
	header.key_count = key_count;
	header.depth = depth;
	/* to be filled in later */
	header.root_page = 0;
	header.last_full = 0;
}
Beispiel #12
0
int acl_btree_depth(ACL_BTREE *tree)
{
	return (btree_depth(tree->root));
}