示例#1
0
文件: tree.c 项目: brsaran/FuzzyApp
/**************************************************************************
 * Compute and store the number of descendants of each node in a tree.
 **************************************************************************/
static void compute_descendants
  (TREE_T * a_tree, BOOLEAN_T force) 
{
  int i_child;
  int num_descendants;

  /* Don't do anything if we've already computed the descendants. */
  if (!force && a_tree->has_descendants) {
    return;
  }

  /* Base case: A leaf has only itself as a descendant. */
  if (is_leaf(a_tree)) {
    num_descendants = 1;
    a_tree->has_descendants = FALSE;
  }
  else {
    /* Recursive case: Descendants of each child. */
    num_descendants = 0;
    a_tree->has_descendants = TRUE;
    for (i_child = 0; i_child < a_tree->num_children; i_child++) {
      compute_descendants(get_nth_child(i_child, a_tree), force);
      num_descendants += get_num_descendants(get_nth_child(i_child, a_tree), force);
    }
  }
  a_tree->num_descendants = num_descendants;
}
示例#2
0
文件: tree.c 项目: brsaran/FuzzyApp
/*************************************************************************
 * Compute the maximum depth of a tree.
 *************************************************************************/
int compute_depth
  (TREE_T * const a_tree)
{
  /* Base case: leaf. */
  if (is_leaf(a_tree)) {
    return(1);
  }

  /* Recursive case: internal node. */
  else {
    int max_depth = 0;
    int num_children = get_num_children(a_tree);
    int i_child;
    for (i_child = 0; i_child < num_children; i_child++) {
      int this_depth = compute_depth(get_nth_child(i_child, a_tree));
      if (this_depth > max_depth) {
	max_depth = this_depth;
      }
    }
    return(max_depth + 1);
  }
  /* Unreachable. */
  abort();
  return(0);
}
/*************************************************************************
  This function populates a trans_matix_array with pointers to matrices 
  and the corresponding time values indexed by the edge number of the 
  phylogenetic tree.  The edges are numbered in depth-first order.

  The three parameters are an evolutinary model, a phylogentic
  tree, and a pointer to substmatrix_array structure.

  The function returns an integer containing the number of matrices
  added to the substmatrix_array structure.
 *************************************************************************/
static int populate_substmatrix_array(
  EVOMODEL_T* model, // IN
  TREE_T* tree, // IN
  int current_position, // IN
  SUBSTMATRIX_ARRAY_T* array // OUT
) {
  // Recursively descend the tree, depth first
  int num_children = get_num_children(tree);
  if (is_leaf(tree) != TRUE) {
    int c = 0;
    for (c = 0; c < num_children; c++) {
      TREE_T* child = get_nth_child(c, tree);
      double t = get_length(child);
      set_substmatrix_time(array, current_position, t);
      MATRIX_T* prob_matrix = get_model_prob_matrix(t, model);
      set_substmatrix_matrix(array, current_position, prob_matrix);
      free_matrix(prob_matrix);
      current_position = populate_substmatrix_array(
        model, 
        child, 
        current_position + 1,
        array
      );
    }
  }
  return current_position;
}
示例#4
0
文件: tree.c 项目: brsaran/FuzzyApp
/*************************************************************************
 * Get the total number of edges under in a tree
 * by recursively summing all of the children.
 *************************************************************************/
int get_num_edges
  (TREE_T * const a_tree)
{
  assert(a_tree != NULL);
  TREE_T* child = NULL;
  int num_children = get_num_children(a_tree);
  int num_edges = num_children;
  int c = 0;
  for (c = 0; c < num_children; c++) {
    child = get_nth_child(c, a_tree);
    num_edges += get_num_edges(child);
  }
  return num_edges;
}
示例#5
0
文件: tree.c 项目: brsaran/FuzzyApp
/*************************************************************************
 * What is the total branch length of the given tree?
 *************************************************************************/
float get_total_length
  (TREE_T * const a_tree)
{
  check_null_tree(a_tree);
  int num_children = get_num_children(a_tree);
  double length = get_length(a_tree);
  int c = 0;

  if (!is_leaf(a_tree)) {
    for (c = 0; c < num_children; c++) {
      TREE_T* child = get_nth_child(c, a_tree);
      length += get_total_length(child);
    }
  }

  return length;
} // get_total_length
示例#6
0
		// If n has a single child, cuts out n and splices its child c to its parent (or if n was the root, sets c to be the new root), then returns (c, true)
		// Otherwise, returns (null_vertex(), false)
		t_vert_bool
		cut_and_splice(node_descriptor n)
		{
			// Can only cut and splice a node with exactly 1 child
			if(child_count(n) != 1)
			{
				return t_vert_bool(null_vertex(), false);
			}

			// Get a descriptor for the only child
			node_descriptor c = get_nth_child(n, 0).first;
			// And having gotten this, we can now disconnect it and its subbranch from n
			n->children.clear();

			// Get n's parent, p, if it exists
			bool has_parent;
			node_descriptor p;
			boost::tie(p, has_parent) = get_parent(n);

			if(has_parent)
			{
				// Replace the vertex descriptor for n in p's children list with c
				child_list_t::iterator c_it = child_it_from_node_descriptor(n);
				*c_it = c;
				// Set c's parent to be p
				c->parent = p;
			}
			else
			{
				// Must be root node, just set c to be the new root
				m_root = static_cast< node_t* >(c);
				c->parent = null_vertex();
			}

			// Now n is disconnected we can safely delete it
			delete static_cast< node_t* >(n);
			return t_vert_bool(c, true);
		}
main(int argc ,char *argv[])
{
	int i;

	/*checking for creation and printing the tree ---Working*/
	node *tree ;
	tree=create_tree();
	read_ssf_from_file(tree, argv[1]);
	print_tree(tree);
	printf("Checked read and print\n\n");



	/*checking for get_nth_child ----Working*/
	node *child = get_nth_child(tree, 2);
	print_node_without_index(child);
	printf("Checked get_nth_child\n\n");

	/*checking for getchildren-----Working*/
	list_of_nodes *l1;
	l1=getchildren(tree);
	printf("\n\nsize=%d\n", l1->size);
	for (i=0; i< l1->size; i++)
	{
		print_attr_of_node(l1->l[i] );
		printf("\n");
	}
	printf("Checked getchildren\n\n");


	/*checking for getleaves----Working*/
	list_of_nodes *l2;
	l2=getleaves(tree);
	printf("\n\nsize=%d\n", l2->size);
	for (i=0; i< l2->size; i++)
	{
		print_attr_of_node(l2->l[i] );
		printf("\n");
	}
	printf("Checked getleaves\n\n");
	
	/*checking for getleaves_child---Working*/
	list_of_nodes *l3;
	l3=getleaves_child(l1->l[1]);
	printf("\n\nsize=%d\n", l3->size);
	for (i=0; i< l3->size; i++)
	{
		print_attr_of_node(l3->l[i] );
		printf("\n");
	}
	printf("Checked getleaves_child\n\n");

	/*checking for get_nodes---Working*/
	list_of_nodes *l4;
	l4=get_nodes(2,"NNS",tree);
	printf("\n\nsize=%d\n",l4->size);
	for (i=0; i< l4->size; i++)
	{
		print_attr_of_node(l4->l[i]);
		printf("\n");
	}
	printf("Checked get_nodes\n\n");


	/*checking for get_pattern---Working*/
	list_of_nodes *l5;
	l5=get_pattern(2,".*N.*",tree);
	printf("\n\nsize=%d\n",l5->size);
	for (i=0; i< l5->size; i++)
	{
		print_attr_of_node(l5->l[i]);
		printf("\n");
	}
	printf("Checked get_pattern\n\n");

	//checking for delete_node------Working
	//printf("%d\n",delete_node(l5->l[1]));
	//print_tree(tree);

	//checking for count_leaf_nodes ----Working
	printf("count of leaf nodes----%d\n\n", count_leaf_nodes(tree));

	//checking for get_field(s)  ----Working 
	char *str;
       	str=get_field(l5->l[1],1);
	printf("%s\n",str);
	str=get_fields(l5->l[1]);
	printf("%s\n", str);
	printf("Checked get_field and get_fields\n\n");


	//checking for get_next_node  and get_previos_node----Working
	node *N;
	N=get_next_node(l1->l[1]);
	str=get_fields(N);
	printf("%s\n", str);
	N=get_previous_node(l1->l[1]);
	str=get_fields(N);
	printf("%s\n", str);
	printf("Checked get_next_node & get_previous_node\n\n");


	//checking for insert_node_position------Working
	node *M;
	M=create_node_with_attr("iiit","NNP","<loc=hyd>",NULL);
	insert_node_into_position(tree,M,1);
	print_tree(tree);
	printf("Checked insert_node_position\n\n");
	
	/*Checkin print_attr_of_or_node--------Working*/	
	print_attr_of_or_node(M->OR);
	printf("\nChecked print_attr_of_or_node\n\n");
		
}