void connect_siblings(TreeNode *n)
{
	if(n == NULL)
		return;

	if(n->left != NULL)
		(n->left)->next = n->right;

	if(n->right != NULL)
	{
		if(n->next)
			(n->right)->next = (n->next)->left?(n->next)->left:(n->next)->right;
		else
			(n->right)->next = NULL;
	}

	connect_siblings(n->left);
	connect_siblings(n->right);
}
int main()
{
	int arr[] = {4,2,6,7,5,3,1}, i;
	TreeNode *root = NULL;

	for(i=0;i<7;i++)
		insert_into_bst(&root, arr[i]);

	inorder(root);
	printf("\n");

	printf("CONNECTIONS\n");
	connect_siblings(root);
	call_display(root);
	delete_tree(&root);

	return 0;
}
예제 #3
0
NODE* apply_extension_rule_2(
                      /* Node 1 (see drawings) */
                      NODE*           node,            
                      /* Start index of node 2's incoming edge */
                      DBL_WORD        edge_label_begin,   
                      /* End index of node 2's incoming edge */
                      DBL_WORD        edge_label_end,      
                      /* Path start index of node 2 */
                      DBL_WORD        path_pos,         
                      /* Position in node 1's incoming edge where split is to be
		         performed */
                      DBL_WORD        edge_pos,         
                      /* Can be 'new_son' or 'split' */
                      RULE_2_TYPE     type)            
{
   NODE *new_leaf,
        *new_internal,
        *son;
   /*-------new_son-------*/
   if(type == new_son)                                       
   {
#ifdef DEBUG   
      printf("rule 2: new leaf (%lu,%lu)\n",edge_label_begin,edge_label_end);
#endif
      /* Create a new leaf (4) with the characters of the extension */
      new_leaf = create_node(node, edge_label_begin , edge_label_end, path_pos);
      /* Connect new_leaf (4) as the new son of node (1) */
      son = node->sons;
      while(son->right_sibling != 0)
         son = son->right_sibling;
      connect_siblings(son, new_leaf);
      /* return (4) */
      return new_leaf;
   }
   /*-------split-------*/
#ifdef DEBUG   
   printf("rule 2: split (%lu,%lu)\n",edge_label_begin,edge_label_end);
#endif
   /* Create a new internal node (3) at the split point */
   new_internal = create_node(
                      node->father,
                      node->edge_label_start,
                      node->edge_label_start+edge_pos,
                      node->path_position);
   /* Update the node (1) incoming edge starting index (it now starts where node
   (3) incoming edge ends) */
   node->edge_label_start += edge_pos+1;

   /* Create a new leaf (2) with the characters of the extension */
   new_leaf = create_node(
                      new_internal,
                      edge_label_begin,
                      edge_label_end,
                      path_pos);
   
   /* Connect new_internal (3) where node (1) was */
   /* Connect (3) with (1)'s left sibling */
   connect_siblings(node->left_sibling, new_internal);   
   /* connect (3) with (1)'s right sibling */
   connect_siblings(new_internal, node->right_sibling);
   node->left_sibling = 0;

   /* Connect (3) with (1)'s father */
   if(new_internal->father->sons == node)            
      new_internal->father->sons = new_internal;
   
   /* Connect new_leaf (2) and node (1) as sons of new_internal (3) */
   new_internal->sons = node;
   node->father = new_internal;
   connect_siblings(node, new_leaf);
   /* return (3) */
   return new_internal;
}
예제 #4
0
Node_T apply_extension_rule_2(Node_T node, SuffixTreeIndex_T edge_label_begin,
                              SuffixTreeIndex_T edge_label_end,
                              SuffixTreeIndex_T path_pos, SuffixTreeIndex_T edge_pos,
                              Rule2_T type)
{
   Node_T new_leaf = NULL, new_internal = NULL, son;
   /*-------new_son-------*/
   if(type == new_son)                                       
   {
      /* Create a new leaf (4) with the characters of the extension */
      new_leaf = create_node(node, edge_label_begin, edge_label_end, path_pos);
      check(new_leaf, "Could not create node.");
      /* Connect new_leaf (4) as the new son of node (1) */
      son = node->left_son;
      while(son->right_sibling != NULL)
         son = son->right_sibling;
      connect_siblings(son, new_leaf);
      /* return (4) */
      return new_leaf;
   }
   /*-------split-------*/
   /* Create a new internal node (3) at the split point */
   new_internal = create_node(
                      node->father,
                      node->edge_label_start,
                      node->edge_label_start+edge_pos,
                      node->path_position);
    check(new_internal, "Could not create node.");
   /* Update the node (1) incoming edge starting index (it now starts where node
   (3) incoming edge ends) */
   node->edge_label_start += edge_pos+1;

   /* Create a new leaf (2) with the characters of the extension */
   new_leaf = create_node(
                      new_internal,
                      edge_label_begin,
                      edge_label_end,
                      path_pos);
   check(new_leaf, "Could not create node.");
   
   /* Connect new_internal (3) where node (1) was */
   /* Connect (3) with (1)'s left sibling */
   connect_siblings(node->left_sibling, new_internal);   
   /* connect (3) with (1)'s right sibling */
   connect_siblings(new_internal, node->right_sibling);
   node->left_sibling = NULL;

   /* Connect (3) with (1)'s father */
   if(new_internal->father->left_son == node)
      new_internal->father->left_son = new_internal;
   
   /* Connect new_leaf (2) and node (1) as sons of new_internal (3) */
   new_internal->left_son = node;
   node->father = new_internal;
   connect_siblings(node, new_leaf);
   /* return (3) */
   return new_internal;

error:
   if(new_leaf) free(new_leaf);
   if(new_internal) free(new_internal);
   return NULL;
}