Example #1
0
void freetree(struct tnode* root)
{
	if(root==NULL)
		return;
	freetree(root->right);
	freetree(root->left);
	free(root);
}
Example #2
0
void freetree(struct tree *root)
{
	if(root->left != NULL)
		freetree(root->left);
	if(root->right != NULL)
		freetree(root->right);
	FREE(root);
}
Example #3
0
void freetree(Node *tree){
	if(!tree)return;
	freetree(tree->c0);
	freetree(tree->c1);
	freetree(tree->c2);
	freetree(tree->c3);
	free(tree);
}
Example #4
0
void freetree(node *now)
{
    if(now == NULL)
        return;
    freetree(now->left);
    freetree(now->right);
    printf("%c", now->a);
    free(now);
}
Example #5
0
/*
 * Clean query tree from values which is always in
 * text (stopword)
 */
static NODE *
clean_fakeval_intree(NODE *node, char *result)
{
	char		lresult = V_UNKNOWN,
				rresult = V_UNKNOWN;

	/* since this function recurses, it could be driven to stack overflow. */
	check_stack_depth();

	if (node->valnode->type == QI_VAL)
		return node;
	else if (node->valnode->type == QI_VALSTOP)
	{
		pfree(node);
		*result = V_STOP;
		return NULL;
	}

	Assert(node->valnode->type == QI_OPR);

	if (node->valnode->qoperator.oper == OP_NOT)
	{
		node->right = clean_fakeval_intree(node->right, &rresult);
		if (!node->right)
		{
			*result = V_STOP;
			freetree(node);
			return NULL;
		}
	}
	else
	{
		NODE	   *res = node;

		node->left = clean_fakeval_intree(node->left, &lresult);
		node->right = clean_fakeval_intree(node->right, &rresult);

		if (lresult == V_STOP && rresult == V_STOP)
		{
			freetree(node);
			*result = V_STOP;
			return NULL;
		}
		else if (lresult == V_STOP)
		{
			res = node->right;
			pfree(node);
		}
		else if (rresult == V_STOP)
		{
			res = node->left;
			pfree(node);
		}
		return res;
	}
	return node;
}
void freetree (bhtree *node) {
    if (node!=NULL) {
        freetree(node->nw);
        freetree(node->ne);
        freetree(node->sw);
        freetree(node->se);
        node->body = NULL;
        free(node);
        node = NULL;
    }
}
Example #7
0
void freetree(struct node *node) {
	if (node->left != NULL)
		freetree(node->left);
	if (node->right != NULL)
		freetree(node->right);
	if (node->parentleft != NULL)
		node->parentleft->right = NULL;
	if (node->parentright != NULL)
		node->parentright->left = NULL;
	free(node);
}
Example #8
0
static void
freetree(NODE * node)
{
	if (!node)
		return;
	if (node->left)
		freetree(node->left);
	if (node->right)
		freetree(node->right);
	pfree(node);
}
Example #9
0
int run_prompt() {

    Node *ptree;
    AstNode *stree;
    EmCodeObject *co;
    Environment *env;
    EmObject *retval;

    env = newenv(vm->topenv);

    while (1) {
        ptree = parse();
        if (ptree) {

            if (ptree->type != MAGIC_COMMAND) {
                // printtree(ptree);

                stree = ast_from_ptree(ptree);

                // printstree(stree);

                co = compile_ast_tree(stree);

                INCREF(&nulobj);
                retval = run_codeobject(co, env, &nulobj);

                if (retval)
                    DECREF(retval);

                vm_reset_for_prompt();

                freetree(ptree);
                freestree(stree);

            } else { // MAGIC_COMMAND
                printf("got magic command %d\n", CHILD(ptree,0)->type);
                if (NCH(ptree) == 2) {
                    printf("magic command arg = %s\n",
                    CHILD(ptree,1)->lexeme);
                }
                if (CHILD(ptree,0)->type == MCA_EXIT) {
                    freetree(ptree); // release memory before exit
                    break;
                }
                // Always release memory of parse tree
                freetree(ptree);
            }
        }
    }
    env_free(env);

    return 1;
}
Example #10
0
void
freetree(tree *p)
{
	if(p==0)
		return;
	freetree(p->child[0]);
	freetree(p->child[1]);
	freetree(p->child[2]);
	if(p->str)
		efree(p->str);
	efree((char *)p);
}
Example #11
0
/*
 * clean tree for ! operator.
 * It's useful for debug, but in
 * other case, such view is used with search in index.
 * Operator ! always return TRUE
 */
static NODE *
clean_NOT_intree(NODE *node)
{
	/* since this function recurses, it could be driven to stack overflow. */
	check_stack_depth();

	if (node->valnode->type == QI_VAL)
		return node;

	if (node->valnode->qoperator.oper == OP_NOT)
	{
		freetree(node);
		return NULL;
	}

	/* operator & or | */
	if (node->valnode->qoperator.oper == OP_OR)
	{
		if ((node->left = clean_NOT_intree(node->left)) == NULL ||
			(node->right = clean_NOT_intree(node->right)) == NULL)
		{
			freetree(node);
			return NULL;
		}
	}
	else
	{
		NODE	   *res = node;

		Assert(node->valnode->qoperator.oper == OP_AND ||
			   node->valnode->qoperator.oper == OP_PHRASE);

		node->left = clean_NOT_intree(node->left);
		node->right = clean_NOT_intree(node->right);
		if (node->left == NULL && node->right == NULL)
		{
			pfree(node);
			res = NULL;
		}
		else if (node->left == NULL)
		{
			res = node->right;
			pfree(node);
		}
		else if (node->right == NULL)
		{
			res = node->left;
			pfree(node);
		}
		return res;
	}
	return node;
}
Example #12
0
tree *freetree(struct tree *t)
{
     if( t!=NULL )
     {
         t->left=(freetree(t->left) );
         
         t->right=(freetree(t->right) );
         
         free(t);
 
     }

     return NULL;
}
Example #13
0
static void
freetree(NODE *node)
{
	/* since this function recurses, it could be driven to stack overflow. */
	check_stack_depth();

	if (!node)
		return;
	if (node->left)
		freetree(node->left);
	if (node->right)
		freetree(node->right);
	pfree(node);
}
Example #14
0
void freetree(NODE *n)
{
	if (n != NULL) {

		NODE *ln = n->ln;
		NODE *rn = n->rn;

		freetree(ln);
		freetree(rn);

		free(n->idx->k);
		free(n->idx);
		free(n);
	}
}
Example #15
0
Node* combine(Node **n1, Node **n2){
	Node *t;
	if(*n1==NULL && *n2==NULL)return NULL;
	else if(*n1 == NULL && *n2 != NULL) return *n2;
	else if(*n1 != NULL && *n2 == NULL) return *n1;

	if((*n1)->type == 'p' && (*n2)->type == 'p'){
		(*n1)->c0 = combine(&(*n1)->c0, &(*n2)->c0);
		(*n1)->c1 = combine(&(*n1)->c1, &(*n2)->c1);
		(*n1)->c2 = combine(&(*n1)->c2, &(*n2)->c2);
		(*n1)->c3 = combine(&(*n1)->c3, &(*n2)->c3);
		(*n2)->c0 = NULL;
		(*n2)->c1 = NULL;
		(*n2)->c2 = NULL;
		(*n2)->c3 = NULL;
		return *n1;
	}
	else if((*n1)->type == 'p' && (*n2)->type == 'e'){
		freetree(*n2);
		*n2 = NULL;
		return *n1;
	}
	else if((*n1)->type == 'p' && (*n2)->type == 'f'){
		freetree(*n1);
		*n1 = NULL;
		return *n2;
	}
	else if((*n1)->type == 'e' && (*n2)->type == 'p'){
		freetree(*n1);
		*n1 = NULL;
		return *n2;
	}
	else if((*n1)->type == 'e' && (*n2)->type == 'e'){
		freetree(*n2);
		*n2 = NULL;
		return *n1;
	}
	else if((*n1)->type == 'e' && (*n2)->type == 'f'){
		freetree(*n1);
		*n1 = NULL;
		return *n2;
	}
	else if((*n1)->type == 'f' && (*n2)->type == 'p'){
		freetree(*n2);
		*n2 = NULL;
		return *n1;
	}
	else if((*n1)->type == 'f' && (*n2)->type == 'e'){
		freetree(*n2);
		*n2 = NULL;
		return *n1;
	}
	else { /*if(n1->type == 'f' && n2->type == 'f') */
		freetree(*n2);
		*n2 = NULL;
		return *n1;
	}
}
Example #16
0
int run_file() {

    Node *ptree;
    AstNode *stree;
    EmCodeObject *co;
    EmObject *retval;

    ptree = parse();
    if (ptree) {
        printtree(ptree);

        stree = ast_from_ptree(ptree);
        printstree(stree);

        co = compile_ast_tree(stree);
        printobj((EmObject *)co, stdout);


        INCREF(&nulobj);
        retval = run_codeobject(co, NULL, &nulobj);

        if (retval)
            DECREF(retval);

        freetree(ptree);
        freestree(stree);

    }
    fclose(source.fp);

    return 1;
}
Example #17
0
int main()
{
          FILE *f;
          treetype root=NULL;
          treetype stopw=NULL;
          stopw=banword(stopw);
          char name[20];
          printf("Enter name file: ");
          scanf("%s",name);
          root=count(f,name,root,stopw);
          printftree(root);
          freetree(root);
          freetree(stopw);
          int choose;
          return 0;
}
Example #18
0
int main()
{
	const char* IN_FILE="encoded.txt";
	const char* CODE_FILE="code.txt";
	const char* OUT_FILE="decoded.txt";
	FILE* fout;
	FILE* fin;
	/*allocate root*/
	root=talloc();
	fin=fopen(CODE_FILE,"r");
	/*build tree*/
	build_tree(fin);
	fclose(fin);

	/*decode*/
	fin=fopen(IN_FILE,"r");
	fout=fopen(OUT_FILE,"w");
	decode(fin,fout);
	fclose(fin);
	fclose(fout);
	/*cleanup*/
	freetree(root);
	getchar();
	return 0;
}
void freetree ( sommet *noeud )

/******************************************************************************/
/*
  Purpose:
 
    FREETREE frees storage associated with a tree.

  Reference:
 
    Eric Thiemard,
    An Algorithm to Compute Bounds for the Star Discrepancy,
    Journal of Complexity,
    Volume 17, pages 850-880, 2001.
*/
{
  int i;
  int max = noeud[0].id;
  for (i=1;i<=max;i++)
    if (noeud[i].pt!=NULL)
      freetree(noeud[i].pt);
  free (noeud);

  return;
}
Example #20
0
int mfree(PCB *currentPCB, void *ptr){
    int address;
    int size;
    uint32 virtual_address,physical_address;
    
    if(ptr==NULL){
    return -1;
    }
    if(((int)ptr<16384)||((int)ptr>=20480)){ //out of range
    return -1;
    }
    address=((int)ptr)-16384;
    virtual_address=ptr;
    physical_address=MemoryTranslateUserToSystem(currentPCB,virtual_address);
   // printf("free address is %d\n",address);
    if(((size=freenode(address,currentPCB->heapnode)))!=-1){
    printf("Freeing heap block of size <%d> bytes: virtual address <0x%x> , physical address <0x%x>\n",size,virtual_address,physical_address);
    freetree(currentPCB->heapnode);
    return(size);
    }
    else{
    printf("Cannot free the heap block of size<%d> bytes:virtual address <0x%x>, physicsal address <0x%x>\n",size, virtual_address,physical_address);
    return NULL;
    }
}
Example #21
0
File: io.c Project: yumm007/C
TREE *load_tree(void) {
	FILE *fp;
	TREE *tree;
	int n, failed = 0, errflag = 0;

	if ((fp = fopen("tree.db", "r")) == NULL)
		return NULL;

	if ((tree = create_boxtree()) == NULL)
		failed = 1;

	if (!failed && fread(&n, sizeof(n), 1, fp) < 1)
		failed = 1;

	if (!failed && (tree->head = __read_box(fp, &errflag)) == NULL) 
		failed = 1;

	if (!failed && !errflag && __load_tree(tree->head, fp) == -1)
		failed = 1;

	if (failed || errflag) {
		freetree(tree);
		free(tree);
		fclose(fp);
		return NULL;
	}

	tree->num = n;

	fclose(fp);
	return tree;
}
Example #22
0
/*
 * clean tree for ! operator.
 * It's useful for debug, but in
 * other case, such view is used with search in index.
 * Operator ! always return TRUE
 */
static NODE *
clean_NOT_intree(NODE * node)
{
	if (node->valnode->type == VAL)
		return node;

	if (node->valnode->val == (int4) '!')
	{
		freetree(node);
		return NULL;
	}

	/* operator & or | */
	if (node->valnode->val == (int4) '|')
	{
		if ((node->left = clean_NOT_intree(node->left)) == NULL ||
			(node->right = clean_NOT_intree(node->right)) == NULL)
		{
			freetree(node);
			return NULL;
		}
	}
	else
	{
		NODE	   *res = node;

		node->left = clean_NOT_intree(node->left);
		node->right = clean_NOT_intree(node->right);
		if (node->left == NULL && node->right == NULL)
		{
			pfree(node);
			res = NULL;
		}
		else if (node->left == NULL)
		{
			res = node->right;
			pfree(node);
		}
		else if (node->right == NULL)
		{
			res = node->left;
			pfree(node);
		}
		return res;
	}
	return node;
}
Example #23
0
int main(int argc, char **argv) {
	struct node *top = parse("euler67-data");
	if (top != NULL) {
		printf("%d\n", recursivesum(top));
		freetree(top);
	}
	else
		return 1;
	return 0;
}
Example #24
0
int freetree(node *node){
    if((node->left==NULL)&&(node->right==NULL)){//leaf
      if(node->free_status==1){
         return(1);
      }
      else{
         return(-1);
      }
    }
    if((node->left->left==NULL)&&(node->left->right==NULL)&&(node->right->left==NULL)&&(node->right->right==NULL)){ //childern are leaves
       if((node->left->free_status==1)&&(node->right->free_status==1)){//both children are free
           printf("Coalesced buddy nodes(order = %d, addr= %d, size= %d) & (order = %d, addr = %d, size = %d) ",node->left->order,node->left->address,node->left->size,node->right->order,node->right->address,node->right->size);
           node->left->inuse=1;
           node->right->inuse=1;
           node->left=NULL;
           node->right=NULL;
           node->free_status=1;
           printf("into the parent node (order= %d, addr= %d, size= %d)\n",node->order,node->address,node->size);
           return(1);
       }
       else{
          printf("the other child node is not free now,parent->order = %d, parent->addr =%d,parent-> size=%d\n",node->order,node->address,node->size); 
          return(-1);
       }
    }
    else{
       if((freetree(node->left)==1)&&(freetree(node->right)==1)){
           printf("Coalesced buddy nodes(order = %d, addr= %d, size= %d) & (order = %d, addr = %d, size = %d) ",node->left->order,node->left->address,node->left->size,node->right->order,node->right->address,node->right->size);
           node->left->inuse=1;
           node->right->inuse=1;
           node->left=NULL;
           node->right=NULL;
           node->free_status=1;
           printf("into the parent node (order= %d, addr= %d, size= %d)\n",node->order,node->address,node->size);
           return(1);
       }
       else{
    //      printf("the other child node is not free now,parent->order = %d, parent->addr =%d,parent-> size=%d\n",node->order,node->address,node->size);
          return(-1);
       }
    }
 }
Example #25
0
int main()
{
    while(scanf(" %s %s", pre, ino) != EOF){
        root = (node*)malloc(sizeof(node));
        subtree(root, 0, 0, strlen(ino) - 1);
        freetree(root);
        root = NULL;
        printf("\n");
    }
    return 0;
}
Example #26
0
static void
freetree(				/* free a trace tree */
	struct node	*tp
)
{
	register struct node	*kid, *k2;

	for (kid = tp->daughter; kid != NULL; kid = k2) {
		k2 = kid->sister;
		freetree(kid);
	}
	free((void *)tp);
}
Example #27
0
static void
freereg(struct regnod *regp)
{
	struct regnod 	*sav;

	while (regp)
	{
		free_arg(regp->regptr);
		freetree(regp->regcom);
		sav = regp->regnxt;
		free(regp);
		regp = sav;
	}
}
Example #28
0
int main()
{
    
    struct tree *t=NULL;
    
    insert(&t,50);
    insert(&t,40);
    insert(&t,100);
    insert(&t,120);
    insert(&t,10);
    insert(&t,30);
    
    inorder(t);
    
    t=freetree(t);   
    inorder(t);
    
    
 
    getch();
}
Example #29
0
void
mainloop(void)				/* get and process input */
{
	static struct node	*sis[MAXDEPTH];
	register struct node	*newp;
	char	buf[128];
	int	level;
	register int	i;

	level = 0;
	while (fgets(buf, sizeof(buf), pin) != NULL) {
		if ((newp = newnode()) == NULL) {
			fprintf(stderr, "%s: memory error\n", progname);
			return;
		}
		for (i = 0; buf[i] == '\t'; i++)
			;
		if (strtoipt(newp->ipt, buf+i) < 0) {
			fprintf(stderr, "%s: bad read\n", progname);
			return;
		}
		newp->sister = sis[i];
		sis[i] = newp;
		if (i < level) {
			newp->daughter = sis[level];
			sis[level] = NULL;
		}
		level = i;
		if (i == 0) {
			setvec(sis[0]->ipt);
			tracerays(sis[0]);
			freetree(sis[0]);
			sis[0] = NULL;
			if (!slow)
				XFlush(theDisplay);
		}
	}
}
Example #30
0
File: io.c Project: yumm007/C
TREE *load_tree(cmp_box *cmp) {

	FILE *fp;
	TREE *tree;
	int n;

	if ((fp = fopen("tree.db", "r")) == NULL)
		return NULL;

	if ((tree = create_boxtree()) == NULL)
		return NULL;
	
	fread(&n, sizeof(tree->num), 1, fp);

	if (__load_tree(tree, fp, cmp, n) == -1){
		freetree(tree);
		fclose(fp);
		return NULL;
	}

	fclose(fp);
	return tree;
}