Exemplo n.º 1
0
int AvlNode::tree_height(AvlIndex tree)
{
    if (tree)
        return 0;

    int lh = tree_height(AVLNODE(tree)->left);
    int rh = tree_height(AVLNODE(tree)->right);

    return 1 + MAX(lh, rh);
}
Exemplo n.º 2
0
Arquivo: tree.c Projeto: Serdeko/calgo
int tree_height(tree *btree)
{
    int left, right;
    if(!btree)
        return 0;
    left = tree_height(btree->left);
    right = tree_height(btree->right);
    if(left > right)
        return left+1;
    else
        return right+1;
}
Exemplo n.º 3
0
int tree_height (struct Node * tree)
{
	if (tree ==  NULL)
		return 0;
	else
	{
		int ret1 = 1, ret2 = 1;
		if (tree->left != NULL)
			ret1 += tree_height(tree->left);
		if (tree->right != NULL)
			ret2 += tree_height(tree->right);
		return (ret1 > ret2)?ret1:ret2;
	}
};
Exemplo n.º 4
0
size_t tree_height(struct tree_root *root)
{
	size_t hl, hr;

	if (root == NIL)
		return 0;

	hl = tree_height(root->left);
	hr = tree_height(root->right);

	if (hl > hr)
		return hl + 1;

	return hr + 1;
}
Exemplo n.º 5
0
void main()
{
    int a[30];
    int count = 0;
    struct tree_node *root = NULL;
    root = tree_add_node(1);
    root->left = tree_add_node(2);
    root->right = tree_add_node(3);
    root->left->left = tree_add_node(4);
    root->left->left->right = tree_add_node(10);
    root->left->right = tree_add_node(5);
    root->left->right->right = tree_add_node(50);
    root->right->left = tree_add_node(6);
    root->right->right = tree_add_node(7);

    struct tree_node *root2 = NULL;
    root2 = tree_add_node(1);
    root2->left = tree_add_node(2);
    root2->right = tree_add_node(3);
    root2->left->left = tree_add_node(4);
    root2->left->left->right = tree_add_node(10);
    root2->left->right = tree_add_node(5);
    root2->right->left = tree_add_node(6);
    root2->right->right = tree_add_node(7);
    //tree_preorder_print(root);
    tree_postorder_iterative_print(root);
    printf("Tree size(9) = %d\n", tree_size(root));
    printf("Height is %d\n", tree_height(root));
    printf("Check identical for same tree = %d\n", tree_check_identical(root, root2));
    tree_mirror(root);
    tree_postorder_iterative_print(root);
    tree_r2l_path(root, a, count);
}
Exemplo n.º 6
0
void map_dump(map m) 
{
  int height = tree_height(m->root);
  int width = (2 << (height-1)) - 1; /* width = 2^height - 1*/
  // printf("tree height: %d\n", height);
  // printf("tree width: %d\n", width);
  
  int array[height][width];
  int i, j;
  
  // reset array
  for(j = 0 ; j < height ; j++)
    for(i = 0 ; i < width ; i++) 
      array[j][i] = -1;
  
  // traverse tree and mark nodes
  if(m->root != NULL) 
    tree_traverse_and_mark(m,m->root,height,(int*)array,0,0);
  
  // dump array
  for(j = 0 ; j < height ; j++) {    
    printf("|");
    for(i = 0 ; i < width ; i++) {
      if(array[j][i] < 0) printf("  ");
      else printf("%.2d",array[j][i]);
    }
    printf("|\n");
  }
  
}
Exemplo n.º 7
0
int tree_height(nwk_node* n_node) {
   int i, max = 0;
   if (n_node) {
      if (tg_size(n_node->children)) {
         for (i = 0; i < (tg_size(n_node->children)); i++) {
		    if (tree_height(n_node->children[i]) > max) {
			   max = tree_height(n_node->children[i]);
			}
		 }
	  } else {
	     return n_node->height;
	  }
	  return max;
   }
   return 0;
}	
Exemplo n.º 8
0
Arquivo: 4.3.c Projeto: kkpattern/CTCI
int main() {
  MemoryPool *pool = memory_pool_init(sizeof(int)*100);
  int values[] = {1, 2, 3, 4, 5, 6, 7};
  BinarySearchTree *tree = construct_minimal_height_tree(
      values, sizeof(values)/sizeof(int), pool);
  assert(3 == tree_height(tree));
  memory_pool_free(pool);
  return 0;
}
Exemplo n.º 9
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.º 10
0
void print_binary_tree(const Node *const pt_to_node) {
    if (!pt_to_node) {
        putchar('\n');
        return;
    }
    const int height = tree_height(pt_to_node);
    if (height == 0)
        print_value(pt_to_node->value), putchar('\n');
    else
        print_subtree(pt_to_node, height, 0);
}
Exemplo n.º 11
0
int tree_height(BINNODE root)
{
	int left_height=0,right_height=0;
	if(root!=NULL)
	{
		if(root->left==NULL&&root->right==NULL)
		return 0;
		else
		{
		left_height=tree_height(root->left);
		right_height=tree_height(root->right);
		
	//Finding the maximum height of subtrees
	return	1 + (left_height>right_height?left_height:right_height);
		}
	}
	else
	return 0;

}
Exemplo n.º 12
0
//트리 높이 구하는 함수
//트리노드를 인자로 받음 
int tree_height(TreeNode *root)
{   
    //트리 전체의 높이 
    int height=0;
    //루트노드의 좌, 우 서브트리의 높이 
    int l, r;
    
    //루트노드가 없으면 0 반환 
    if(root==NULL)
         return 0;
    
    //루트노드의 좌측 서브트리 높이 저장      
    l=tree_height(root->left);
    //루트노드의 우측 서브트리 높이 저장  
    r=tree_height(root->right);
    //루트노드의 좌/우 서브트리 중에서 높이가 높은 값 추출 후 루트 자신의 높이인 1을 더함 
    height=(l>r?l:r)+1;
    
    //총 높이를 반환 
    return height;
}
Exemplo n.º 13
0
int main (int argc, char **argv)
{
    flux_t h;
    uint32_t rank, n;
    int k;

    if (!(h = flux_open (NULL, 0)))
        err_exit ("flux_open");
    if (flux_get_rank (h, &rank) < 0)
        err_exit ("flux_get_rank");
    if (flux_get_size (h, &n) < 0)
        err_exit ("flux_get_size");
    if (flux_get_arity (h, &k) < 0)
        err_exit ("flux_get_arity");
    printf ("height of %d-ary tree of size %" PRIu32 ": %d\n",
            k, n, tree_height (n, k));
    printf ("height of %d-ary at rank %" PRIu32 ": %d\n",
            k, rank, tree_height (rank + 1, k));
    flux_close (h);
    return (0);
}
Exemplo n.º 14
0
Arquivo: main.c Projeto: bcho/homework
int main()
{
    char msg[] = " deadbeef";
    binary_tree_t t = create_tree(msg, 8);

    printf("%d", tree_height(t));
    sep;

    preorder_traversal(t, print_node);
    sep;
    postorder_traversal(t, print_node);
    sep;
    inorder_traversal(t, print_node);

    return 0;
}
Exemplo n.º 15
0
std::size_t BST<Item,Key>::height() const{
	return tree_height(root);
}
Exemplo n.º 16
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);
}
Exemplo n.º 17
0
	
	node* root=(node*)malloc(sizeof(node));
	*root=new;
	insert(root, new1);
	insert(root, new2);
	insert(root, new3);
	insert(root, new4);
	
	printf("\nKsiazka telefoniczna:\n");
	in_order(root);
	
	printf("\n\nMinimum:");
	print_node(find_min(root));
	
	printf("\n\nMaksimum:");
	print_node(find_max(root));
	
	printf("\n\nLiczba wezlow: %d", nodes(root));
	
	printf("\n\nSzukam osoby o nazwisku Nowak:");
	print_node(find_key(root, "Nowak"));
	
	printf("\n\nSzukam osoby o nazwisku Jan:");
	print_node(find_key(root, "Jan"));
	
	printf("\n\nWysokosc drzewa: %d\n", tree_height(root));
	return 0;
	
	delete_tree(root);
}
Exemplo n.º 18
0
int tree_height(tree node)
{
  if(node == NULL) return 0;   /* the height of a void tree is 0. */
  return 1 + max(tree_height(node->left),tree_height(node->right));
}
Exemplo n.º 19
0
int main(void)
{
    //트리를 구성할 루트노드 
    TreeNode *root=NULL;
    //트리에 넣을 값들을 받을 정수형 배열 
    int val[100];
    //반복 구문을 수행할 정수형 변수 
    int i, j;
    
    //트리에 구성할 값들을 입력 받기 
    //설명문 출력 
    printf("트리를 구성할 값을 입력하세요.\n(정수 범위: 1~100 / 최대 100개 입력 가능 / 종료는 101)\n");
    //트리에 넣을 값을 배열에 저장(최대 100번 반복)
    for(i=0;i<100;i++)
    {
         //트리에 넣을 값 입력 
         scanf("%d", &val[i]);
         //101를 입력받으면 반복 종료 
         if(val[i]==101)
              break;
         //입력 범위를 초과하면 
         else if(val[i]>101)
              //오류 메세지 출력 
              error("값을 초과하였습니다.");
    }
    
    //입력 값 출력 및 입력 값 트리에 삽입 
    printf("\n입력 값 \n");
    //입력 한 값들의 갯수만큼 반복 
    for(j=0;j<i;j++)
    {
         //입력 값 확인을 위한 출력 
         printf("%d ", val[j]);
         //입력 값 트리에 삽입 
         tree_insert_node(&root, val[j]);
    }
    //출력 구분을 위한 개행 
    printf("\n-------------------------------------------------------\n");

    //전위 순회 함수를 통해 전위 순회 출력 
    printf("전위 순회 출력 결과\n"); 
    tree_preorder(root);
    
    //중위 순회 함수를 통해 전위 순회 출력 
    printf("\n중위 순회 출력 결과\n"); 
    tree_inorder(root);
    
    //후위 순회 함수를 통해 전위 순회 출력 
    printf("\n후위 순회 출력 결과\n"); 
    tree_postorder(root);
    printf("\n");
    
    //입력 받은 값 트리에서 삭제하기 
    //설명문 출력 
    printf("\n트리에서 삭제할 값을 순서대로 입력하세요!(종료는 101) \n");
    //트리에서 삭제할 값 배열에 저장(최대 100번 반복)
    for(i=0;i<100;i++)
    {
         //삭제할 값 입력 
         scanf("%d", &val[i]);
         //101을 입력받으면 반복 종료 
         if(val[i]==101)
              break;
         //입력 범위를 초과하면 
         else if(val[i]>101)
              //오류 메세지 출력 
              error("값을 초과하였습니다.");
    }
    
    //입력 값 출력 및 입력 값 트리에서 삭제 
    printf("\n입력 값 \n");
    //입력 한 값들의 갯수만큼 반복 
    for(j=0;j<i;j++)
    {
         //삭제 값 확인을 위한 출력 
         printf("%d ", val[j]);
         //트리에서 해당되는 노드 삭제  
         tree_delete_node(&root, val[j]);
    }
    
    //출력 구분을 위한 개행 
    printf("\n-------------------------------------------------------\n");
    //트리 노드 개수 구하는 함수를 통한 개수 출력 
    printf("삭제 후 트리의 노드 개수 : %d\n", tree_get_node_count(root));
    //트리 높이 구하는 함수를 통한 높이 출력 
    printf("삭제 후 트리의 높이 : %d\n", tree_height(root));
    //레벨 순회 함수를 통한 레벨 순회 출력
    printf("삭제 후 트리의 레벨 순회 \n");
    tree_level_order(root); 
    printf("\n");
    
    system("pause");
    return 0;
}
Exemplo n.º 20
0
int BinarySearchTree::tree_height() {
	return tree_height(root);
}
Exemplo n.º 21
0
void menu()
{
	int c=0,data;
	BINNODE temp;
	while(c!=9)
	{
	printf("\t\t\t\tMenu for AVL tree build and Traversal\n");
	printf("\t\t\t1.Insert into AVL tree\n");
	printf("\t\t\t2.Search in AVL tree\n");
	printf("\t\t\t3.Height of the tree\n");
	printf("\t\t\t4.Number of nodes in the tree\n");
	printf("\t\t\t5.Inorder Traversal\n");
	printf("\t\t\t9.Exit\n");
		printf("\t\t\t\tEnter your choice:");
	scanf("%d",&c);
		switch(c)
		{
			case 1:
			printf("Enter the data to be inserted:");
			scanf("%d",&data);
			if(root==NULL)
			{
			if((temp=(BINNODE)malloc(sizeof(struct binnode)))\
			==NULL)
				printf("Error 1:\
				Memory Allocation Problem.\n");
			else
				{
				temp->data=data;
				temp->left=NULL;
				temp->right=NULL;
				temp->balance_factor=0;
				root=temp;
				}
			}
			else
				insert_node(data);
			break;
			case 2:
			printf("Enter the data to serach:");
			scanf("%d",&data);
			if((temp=search_node(root,data))==NULL)
				printf("Node does not exist.\n");
			else
				printf("Node found,with balance factor:%d\n"
				,temp->balance_factor);
			break;
			case 3:
				printf("Height of the tree:%d\n"\
				,tree_height(root));
			break;
			case 4:
				printf("Number of nodes in the tree:%d\n"\
				,count_node(root));
				break;
			case 5:
				print_inorder(root);
				printf("\n");
				break;
			case 9:
				deallocate(root);
				printf("Exiting...\n");
				break;


		}
	}
Exemplo n.º 22
0
void printtolog(FILE *fp, RBtree *tree)
{
    fprintf(fp, "%d\n", tree_height(tree->root));
}
Exemplo n.º 23
0
int tree_height(Node node){
	if(!node)
		return 0;
	return 1 + max(tree_height(node->left), tree_height(node->right));
}
Exemplo n.º 24
0
std::size_t BST<Item, Key>::tree_height(const Node* root) const{
    if (root == NULL) return 0;

    return 1 + std::max(tree_height(root->left), tree_height(root->right));
}