Example #1
0
static int bt_check_node(bt* tree, bt_node* node) {
    int hl, hr;
    bt_node* leftmost;
    if (isleaf(node)) {
        CHECK(node->leaf.N <= tree->blocksize);
        return 0;
    }

    CHECK(sum_childN(&node->branch) == node->branch.N);

    leftmost = node;
    while (!isleaf(leftmost))
        leftmost = leftmost->branch.children[0];
    CHECK(&leftmost->leaf == node->branch.firstleaf);

    hl = height_slow(node->branch.children[0]);
    hr = height_slow(node->branch.children[1]);
    CHECK(node->branch.balance == (hr - hl));
    CHECK((node->branch.balance == 0) ||
          (node->branch.balance == 1) ||
          (node->branch.balance == -1));

    if (bt_check_node(tree, node->branch.children[0]) ||
        bt_check_node(tree, node->branch.children[1]))
        return -1;
    return 0;
}
Example #2
0
void
genlabel (tree * n)
{
   if (n == NULL)
      return;
   if (!isleaf (n))
   {
      if (isleaf (n->left))
	 n->left->label = 1;
      else
	 genlabel (n->left);

      if (isleaf (n->right))
	 n->right->label = 0;
      else
	 genlabel (n->right);

      if(n->right == NULL)
	 n->label = n->left->label;
      else
	 if (n->right->label != n->left->label)
	    n->label = max (n->right->label, n->left->label);
	 else
	    n->label = n->right->label + 1;
   }
}
Example #3
0
static void transform(Agraph_t * g)
{
    Agnode_t *n;
    Agedge_t *e;
    char *str;
    Agsym_t *m_ix, *s_ix;
    int cnt, d;

    m_ix = bindedgeattr(g, "minlen");
    s_ix = bindedgeattr(g, "style");

    for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
	d = myindegree(n) + myoutdegree(n);
	if (d == 0) {
	    if (ChainLimit < 1)
		continue;
	    if (ChainNode) {
		e = agedge(g, ChainNode, n, "", TRUE);
		agxset(e, s_ix, "invis");
		ChainSize++;
		if (ChainSize < ChainLimit)
		    ChainNode = n;
		else {
		    ChainNode = NULL;
		    ChainSize = 0;
		}
	    } else
		ChainNode = n;
	} else if (d > 1) {
	    if (MaxMinlen < 1)
		continue;
	    cnt = 0;
	    for (e = agfstin(g, n); e; e = agnxtin(g, e)) {
		if (isleaf(agtail(e))) {
		    str = agxget(e, m_ix);
		    if (str[0] == 0) {
			adjustlen(e, m_ix, (cnt % MaxMinlen) + 1);
			cnt++;
		    }
		}
	    }

	    cnt = 0;
	    for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
		if (isleaf(e->node) || (Do_fans && ischainnode(e->node))) {
		    str = agxget(e, m_ix);
		    if (str[0] == 0)
			adjustlen(e, m_ix, (cnt % MaxMinlen) + 1);
		    cnt++;
		}
	    }
	}
    }
}
Example #4
0
int bt_height(bt* tree) {
    bt_node* n;
    int h;
    n = tree->root;
    if (!n) return 0;
    if (isleaf(n)) return 1;
    for (h=1; !isleaf(n); h++) {
        if (n->branch.balance > 0)
            n = getrightchild(n);
        else
            n = getleftchild(n);
    }
    return h;
}
Example #5
0
static int rem (BTree* a, int x)
{
    if (isleaf(a))
    {
        int pos;
        if (findpos(a,x,&pos))
            delleft(a,pos); /* or delright */
        else
            return 0;
    }
    else {
        int pos;
        if (findpos(a,x,&pos))
        {
            BTree* l = findmax(a->p[pos]);
            a->k[pos] = l->k[l->n-1];
            l->k[l->n-1] = x;
        }
        if (!rem(a->p[pos],x))
            return 0;
        if (underflow(a->p[pos]))
            rearrange(a,pos);
    }
    return 1;
}
Example #6
0
void insert(tttree *cur,int num)
	{
	if(cur==NULL)
		{
		cur=new tttree(num);
		root=cur;
		return;
		}
	if(num==cur->v1||(cur->type==3&&cur->v2==num))return;
	if(isleaf(cur))
		{
		if(cur->type==2)
			{
			cur->v2=num;
			cur->type=3;
			cur->needswap();
			}
		else if(cur->type==3)
			split(cur,num);
		return;
		}
	if(num<cur->v1)insert(cur->left,num);
	else if(cur->type==2&&num>cur->v1)insert(cur->right,num);
	else if(cur->type==3&&num>cur->v2)insert(cur->right,num);
	else	if(cur->type==3)insert(cur->mid,num);
	}
Example #7
0
static void bt_free_node(bt_node* node) {
    if (!isleaf(node)) {
        bt_free_node(getleftchild(node));
        bt_free_node(getrightchild(node));
    }
    free(node);
}
Example #8
0
static bt_node* next_node(bt_node** ancestors, int nancestors,
                          bt_node* child,
                          bt_node** nextancestors, int* nnextancestors) {
    // -first, find the first ancestor of whom we are a left
    //  (grand^n)-child.
    bt_node* parent = NULL;
    int i, j;
    for (i=nancestors-1; i>=0; i--) {
        parent = ancestors[i];
        if (parent->branch.children[0] == child)
            break;
        child = parent;
    }
    if (i < 0) {
        // no next node.
        return NULL;
    }

    // we share ancestors from the root to "parent".
    for (j=i; j>=0; j--)
        nextancestors[j] = ancestors[j];
    *nnextancestors = i+1;

    // -next, find the leftmost leaf of the parent's right branch.
    child = parent->branch.children[1];
    while (!isleaf(child)) {
        nextancestors[(*nnextancestors)++] = child;
        child = child->branch.children[0];
    }
    return child;
}
Example #9
0
/**
 * Returns the first child of the (internal) node v.
 */
ulong SSTree::firstChild(ulong v)
{
    if (isleaf(v))
        return 0;

    return v+1;
}
Example #10
0
int 
no_reply_child (
    DN object,
    DN dn, 	/* tail - not matched */
    struct dn_seq *dn_stack,
    int master,
    Entry entryptr,
    struct DSError *err,
    struct di_block **di_p
)
{
	DN dn_tmp;

	DLOG (log_dsap,LLOG_TRACE,("no reply child"));

	if (isleaf(entryptr)) {
		DLOG (log_dsap,LLOG_DEBUG,("definate NO"));
		if (dn != NULLDN) {
			dn_tmp = dn->dn_parent;
			dn->dn_parent = NULLDN;
		} else
			object = NULLDN;
		err->dse_type = DSE_NAMEERROR;
		err->ERR_NAME.DSE_na_problem = DSE_NA_NOSUCHOBJECT;
		err->ERR_NAME.DSE_na_matched = dn_cpy (object);
		if (dn != NULLDN)
			dn->dn_parent = dn_tmp;

		return(DS_X500_ERROR);
	}

	return(constructor_dsa_info_aux (object, dn_stack, master, entryptr, err, di_p));
}
Example #11
0
static int height_slow(bt_node* node) {
    int hl, hr;
    if (isleaf(node)) return 1;
    hl = height_slow(node->branch.children[0]);
    hr = height_slow(node->branch.children[1]);
    return 1 + (hl > hr ? hl : hr);
}
Example #12
0
/**
 * Returns the d-th character of the label of the node v's incoming edge.
 */
uchar SSTree::edge(ulong v, ulong d)
{
    uchar *ss;
    if (isleaf(v))
    {
        ulong i = leftrank(v);
        ulong j = depth(parent(v));
        ulong d1 = sa->lookup(i) + j;
        if (d > n - d1)
            return 0u;
        ss = sa->substring(d1 + d - 1,1);
        uchar result = ss[0];
        delete [] ss;
        return result;
    }

    ulong d1 = hgt->GetPos(inorder(parent(v)));
    ulong d2 = hgt->GetPos(inorder(v));
    if (d > d2 - d1)
        return 0u;
    ss = sa->substring(sa->lookup(inorder(v)) + d1 + d - 1,1);
    uchar result = ss[0];
    delete [] ss;
    return result;
}
Example #13
0
static void bt_print_node(bt* tree, bt_node* node, char* indent,
                          void (*print_element)(void* val)) {
    printf("%s", indent);
    printf("(%p) ", node);
    printf("N=%i", node_N(node));

    if (!isleaf(node)) {
        char* subind;
        char* addind = "  ";
        printf(", leftmost (%p)", node->branch.firstleaf);
        printf(", Nleft=%i, Nright=%i, balance %i.\n",
               node_N(node->branch.children[0]),
               node_N(node->branch.children[1]),
               node->branch.balance);
        subind = malloc(strlen(indent) + strlen(addind) + 1);
        sprintf(subind, "%s%s", indent, addind);
        printf("%sLeft child:\n", indent);
        bt_print_node(tree, node->branch.children[0], subind, print_element);
        printf("%sRight child:\n", indent);
        bt_print_node(tree, node->branch.children[1], subind, print_element);
        free(subind);
    } else {
        int i;
        printf(".  Leaf.");
        if (print_element) {
            printf("  [ ");
            for (i=0; i<node_N(node); i++)
                print_element(get_element(tree, &node->leaf, i));
        }
        printf("]\n");
    }
}
Example #14
0
void bt_destroy (BTree* a) {
  int i;
  if (!isleaf(a)) {
    for (i = 0; i <= a->n; ++i)
    bt_destroy(a->p[i]);
  }
  free(a);
}
Example #15
0
void split(tttree *cur,int num)
	{
	tttree *n1=new tttree(min(cur->v1,cur->v2,num));
	tttree *n2=new tttree(max(cur->v1,cur->v2,num));
	int x=cur->v1+cur->v2+num-n1->v1-n2->v1;
	tttree *p;
	if(cur->par==NULL)
		{
		p=new tttree(x);
		root=p;
		}
	else p=cur->par;
	n1->par=p;
	n2->par=p;
	if(x<p->v1)
		{
		p->left=n1;
		p->extra=n2;
		}
	else if((p->type==2&&x>p->v1)||(p->type==3&&x>p->v2))
		{
		p->right=n2;
		p->extra=n1;
		}
	else if(p->v1!=x)
		{
		p->mid=n1;
		p->extra=n2;
		}
	if(!isleaf(cur))
		{
		sort(cur);
		n1->left=cur->left;
		n1->right=cur->mid;
		n2->left=cur->right;
		n2->right=cur->extra;
		cur->left->par=n1;
		cur->mid->par=n1;
		cur->right->par=n2;
		cur->extra->par=n2;
		}
	delete cur;
	if(p->v1==x)
		{
		p->left=n1;
		p->right=n2;
		return;
		}
	if(p->type==2)
		{
		p->v2=x;
		p->type=3;
		p->mid=p->extra;
		p->needswap();
		}
	else split(p,x);
	return;
	}
Example #16
0
void split(tttree *cur,int num)
	{
	tttree *n1=new tttree(min(cur->v1,num,cur->v2));
	tttree *n2=new tttree(max(cur->v1,num,cur->v2));
	int x=cur->v1+num+cur->v2-n1->v1-n2->v1;
	
	tttree *p=NULL;
	if(cur->par==NULL)
		{
		p=new tttree(x);
		root=p;
		}
	else
		p=cur->par;
	n1->par=p;
	n2->par=p;
	if(x<p->v1)
		{
		p->left=n1;
		p->extra=n2;
		}
	else if((p->type==2&&x>p->v1)||(p->type==3&&x>p->v2))
		{
		p->right=n2;
		p->extra=n1;
		}
	else if(x!=p->v1)
		{
		p->mid=n1;
		p->extra=n2;
		}
	
	if(!isleaf(cur))
		{
		sort(cur);
		cur->left->par=n1;n1->left=cur->left;
		cur->mid->par=n1;n1->right=cur->mid;
		cur->right->par=n2;n2->left=cur->right;
		cur->extra->par=n2;n2->right=cur->extra;
		}
	delete cur;
	if(p->v1==x)
		{
		p->left=n1;  //!!!!FORGET
		p->right=n2; //!!!!FORGET
		return;
		}
	if(p->type==2)
		{
		p->mid=p->extra;  //!!!FORGET
		p->type=3;
		p->v2=x;
		needswap(p);
		}
	else
		split(p,x);
	}
Example #17
0
BTree* bt_search(BTree* a, int x, int* pos) {
  int found = findpos(a,x,pos);
  if (found)
    return a;
  else if (isleaf(a))
    return NULL;
  else
    return bt_search(a->p[*pos], x, pos);
}
Example #18
0
//Subtree with maximum sum
Node *maxSumSubtree(Node *root)
{
    if(!root)   return NULL;
    if(isleaf(root))    return root;
    int maxsum=0;
    Node *res=NULL;
    helper(root,&res,&maxsum);
return *res;
}
Example #19
0
int numleaves(void* tree) {
	if(isempty(tree)) {
		return 0;
	} else if(isleaf(tree)) {
		return 1;
	} else {
		return numleaves(left(tree)) + numleaves(right(tree));
	}
}
Example #20
0
int 
no_reply_edb (
    DN object,
    DN dn, 	/* tail - not matched */
    struct dn_seq *dn_stack,
    int master,
    Entry entryptr,
    struct DSError *err,
    struct di_block **di_p
)
{
	DN dn_tmp;
	Entry akid;

	DLOG (log_dsap,LLOG_TRACE,("no reply edb"));

	if (isleaf(entryptr)) {
		DLOG (log_dsap,LLOG_DEBUG,("definate NO"));
		if (dn != NULLDN) {
			dn_tmp = dn->dn_parent;
			dn->dn_parent = NULLDN;
		} else
			object = NULLDN;
		err->dse_type = DSE_NAMEERROR;
		err->ERR_NAME.DSE_na_problem = DSE_NA_NOSUCHOBJECT;
		err->ERR_NAME.DSE_na_matched = dn_cpy (object);
		if (dn != NULLDN)
			dn->dn_parent = dn_tmp;

		return(DS_X500_ERROR);
	}

	if (entryptr->e_children == NULLAVL) {
		return(constructor_dsa_info(object,dn_stack,master,entryptr,err,di_p));
	}

	akid = (Entry) avl_getone(entryptr->e_children);
	if ((akid->e_data == E_DATA_MASTER)
			|| ((! master) && (akid->e_data == E_TYPE_SLAVE)) ) {
		DLOG (log_dsap,LLOG_DEBUG,("definate NO"));
		if (dn != NULLDN) {
			dn_tmp = dn->dn_parent;
			dn->dn_parent = NULLDN;
		} else
			object = NULLDN;
		err->dse_type = DSE_NAMEERROR;
		err->ERR_NAME.DSE_na_problem = DSE_NA_NOSUCHOBJECT;
		err->ERR_NAME.DSE_na_matched = dn_cpy (object);
		if (dn != NULLDN)
			dn->dn_parent = dn_tmp;
		return(DS_X500_ERROR);
	}

	/* build a referral */
	return(constructor_dsa_info_aux(object,dn_stack,master,entryptr,err,di_p));
}
Example #21
0
/**
 * Returns the path label from root to the node v.
 */
uchar *SSTree::pathlabel(ulong v) {
    if (isleaf(v)) {
        ulong i = leftrank(v);
        ulong k = depth(v);
        ulong d1 = _sa->lookup(i);
        return _sa->substring(d1,k);
    }
    ulong d2 = _hgt->GetPos(inorder(v));
    return _sa->substring(_sa->lookup(inorder(v)), d2);
}
Example #22
0
File: spr.c Project: pcordes/allspr
/* sort out a tree where nodes' parent pointers are pointing to nodes that are actually their children.
 * borrowed from procov */
static void spr_organize_tree(struct spr_node *from, struct spr_node *nd)
{
	struct spr_node *p;

	if(isleaf(nd)) return;
	if(from==nd->left){  p=nd->parent; nd->parent=nd->left;  nd->left =p; }
	if(from==nd->right){ p=nd->parent; nd->parent=nd->right; nd->right=p; }
	spr_organize_tree(nd, nd->left);
	spr_organize_tree(nd, nd->right);
}
Example #23
0
/**
 * Suffix link for internal nodes
 */
ulong SSTree::sl(ulong v)
{
    if (v == 0 || v == root() || isleaf(v))
        return 0;

    ulong x = brLeaf->rank(v - 1) + 1;
    ulong y = brLeaf->rank(Pr->findclose(v));
    x = sa->Psi(x - 1);
    y = sa->Psi(y - 1);
    return lca(brLeaf->select(x + 1), brLeaf->select(y + 1));
}
Example #24
0
//check the leaves if they are in sorted order from left to right
int isSorted(struct node* node){
static int max=INT32_MIN;
static int flag=1;

if(node!=NULL){
	isSorted(node->left);

	if(node->data>max && isleaf(node)){
        max=node->data;
        flag=1*flag;
	}
    if(node->data<max && isleaf(node)){
    flag=0;
    }
    isSorted(node->right);

    }//end of if

return flag;
}
Example #25
0
void checktype(tree *n)
{
   if(!isleaf(n))
      return;

   if(n->type == t_id && !lookup(name(n)))
   {
      fprintf(stderr, "Unknown variable(%s)\n", name(n));
      exit(UNKNOWN_VAR_ERR);
   }
}
Example #26
0
/**
 * Suffix link for internal nodes
 */
ulong SSTree::sl(ulong v) {

    if (v == 0 || v == root() || isleaf(v)) {
        return 0;
    }

    ulong x = _brLeaf->rank(v - 1) + 1;
    ulong y = _brLeaf->rank(_Pr->findclose(v));
    x = _sa->Psi(x - 1);
    y = _sa->Psi(y - 1);
    return lca(_brLeaf->select(x + 1), _brLeaf->select(y + 1));
}
Example #27
0
/**
 * Returns the string depth of the node v.
 */
ulong SSTree::depth(ulong v) {
    if (v == 0) {
        return 0;
    }

    if (isleaf(v)) {
        ulong i = leftrank(v);
        return _n - _sa->lookup(i);
    }

    v = inorder(v);
    return _hgt->GetPos(v);
}
Example #28
0
// Pure?
void* bt_access(bt* tree, int index) {
    bt_node* n = tree->root;
    int offset = index;
    while (!isleaf(n)) {
        int Nleft = node_N(n->branch.children[0]);
        if (offset >= Nleft) {
            n = n->branch.children[1];
            offset -= Nleft;
        } else
            n = n->branch.children[0];
    }
    return get_element(tree, &n->leaf, offset);
}
Example #29
0
/**
 * Returns the edge label of the incoming edge of the node v.
 */
uchar *SSTree::edge(ulong v) {
    if (isleaf(v)) {
        ulong i = leftrank(v);
        ulong j = depth(parent(v));
        ulong k = depth(v);
        ulong d1 = _sa->lookup(i) + j;
        return _sa->substring(d1, k - j);
    }

    ulong d1 = _hgt->GetPos(inorder(parent(v)));
    ulong d2 = _hgt->GetPos(inorder(v));

    return _sa->substring(_sa->lookup(inorder(v)) + d1, d2 - d1);
}
Example #30
0
/**
 * Returns the child node of v so that edge leading to the child node starts with the symbol c.
  */
ulong SSTree::child(ulong v, uchar c)
{
    if (isleaf(v))
        return 0;

    v++;   // First child of v
    while (v != 0)
    {
        if (c == edge(v,1))
            return v;
        v = sibling(v);
    }
    return 0;
}