// Make a tree empty void IntervallTree_bed::makeempty(Leaf * &p) { Leaf * d; if (p != NULL) { makeempty(p->left); makeempty(p->right); d = p; free(d); p = NULL; } }
// Make a tree empty void bstree::makeempty(nodeptr &p) { nodeptr d; if (p != NULL) { makeempty(p->left); makeempty(p->right); d=p; free(d); p=NULL; } }
int read(void){ gettoken(); switch(stok.type){ case NUMBER: return(makenum(atoi(stok.buf))); case SYMBOL: return(makesym(stok.buf)); case QUOTE: return(cons(makesym("quote"), cons(read(),makeempty()))); case LPAREN: return(readlist()); } }
int f_oblist(int arglist){ int addr,addr1,res; res = makeempty(); addr = E; while(addr != 0){ addr1 = makesym(symname(addr)); res = cons(addr1,res); addr = cdr(addr); } return(res); }
int readlist(void){ int car,cdr; gettoken(); if(stok.type == RPAREN) return(makeempty()); else{ stok.flag = BACK; car = read(); cdr = readlist(); return(cons(car,cdr)); } }
int search(void) { int x0,y0,cout,action,i; type p,t; memset(array,0,sizeof(array)); p=malloc(sizeof(struct node)); p->x=x1; p->y=y1; p->cout=0; p->action=0; array[p->x][p->y][p->action]=1; createqueue(1000); makeempty(Q); enqueue(p,Q); while( !isempty(Q) ) { p=dequeue(Q); if( p->x==x2&&p->y==y2) return p->cout; for(i=0;i<5;++i) { x0=p->x+direction[i][0]; y0=p->y+direction[i][1]; cout=p->cout+1; action=p->action+1; if(legal(x0,y0,action)&&map[x0][y0]!='*'&&x0>=0&&x0<m&&y0>=0&&y0<n) { if(!array[x0][y0][action]&&!(x0==dogpoint[action].x&&y0==dogpoint[action].y)) { array[x0][y0][action]=1; t=malloc(sizeof(struct node)); t->x=x0; t->y=y0; if(action==D) action=0; t->action=action; t->cout=cout; enqueue(t,Q); } } } } return -1; }
// Copy a tree void bstree::copy(nodeptr &p,nodeptr &p1) { makeempty(p1); p1 = nodecopy(p); }
// Copy a tree void IntervallTree_bed::copy(Leaf * &p, Leaf * &p1) { makeempty(p1); p1 = nodecopy(p); }
int main() { node * curr; char input[15]; printf("Creating binary tree..."); makeempty(); printf("\tDone\n\n"); char delims[] = " "; char *result = NULL; char *item = NULL; while (1){ printf("What would you like to do?\n[help for list of commands]\n"); gets(input); search_count = 0; result = (char *) strtok( input, delims ); if(!strcmp("insert", result)) { item = (char *) strtok( NULL, delims ); if( !find(root, atoi(item)) ) { curr = (node *)malloc(sizeof(node)); curr->left = curr->right = NULL; curr->val = atoi(item); insert(&root, curr); printout(root); } else printf("Value %d exists\n\n", atoi(item)); continue; } if(!strcmp("delete", result)) { item = (char *) strtok( NULL, delims ); if( find(root, atoi(item)) ) { printf("Found value %d \n", atoi(item)); deleteval( root , atoi(item) ); printf("Value %d deleted\n\n", atoi(item)); } else printf("Value %d does not exist\n\n", atoi(item)); continue; } if(!strcmp("search", result)) { item = (char *) strtok( NULL, delims ); if( find(root, atoi(item)) ) { printf("Found value %d - %d nodes walked\n\n", atoi(item), search_count); } else printf("Value %d does not exist\n\n", atoi(item)); continue; } if(!strcmp("max", result)) { if(root != NULL) printf("Max value: %d\n\n", max(root)); else printf("Search tree is empty\n\n"); continue; } if(!strcmp("succ", result)) { item = (char *) strtok( NULL, delims ); int x, tree_max; if( root == NULL ) { printf("Search tree is empty\n\n"); continue; } x = succ( root , atoi(item) , NULL); if(x==NULL) { printf("Value %d is larger than any other value in tree\n\n", atoi(item)); continue; //printf("Value's %d next larger value is %d\n\n", atoi(item) , x ); } else if ( x == atoi(item) ) { printf("Value %d is the largest value in tree\n\n", atoi(item)); continue; //printf("Value's %d next larger value is %d\n\n", atoi(item) , x ); } else { printf("Value %d is the next larger value of %d\n\n", x , atoi(item)); continue; } continue; } if(!strcmp("subtree", result)){ result = strtok( NULL, delims ); if(!strcmp("leaves", result)){ item = (char *) strtok( NULL, delims ); if( find(root, atoi(item)) ) { find_print(root, atoi(item)); } else printf("Value %d does not exist\n\n", atoi(item)); continue; } else { printf("Please give a valid command\n\n"); continue; } } if(!strcmp("exit", result)){ printf("Destroing tree...\t"); //printf("\n"); tree_destroy(root); printf("Done\n\n"); printf("Bye bye!\n\n\n"); break; } if(!strcmp("help", result)){ printf("\nList of commands: \ninsert x \ndelete x \nsearch x \nmax \nsucc x \nsubtree leaves x \nexit \n\n"); continue; } printf("Please give a valid command\n\n"); } //sleep(1000); return 0; }