Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "bstree.h"
- FILE *fp;
- void initfile()
- {
- fp=fopen("files/output.txt","w");
- //printf("Init file %p",fp);
- }
- void closefile()
- {
- fclose(fp);
- }
- BNODEPTR* getNode()
- {
- BNODEPTR *p = (BNODEPTR*)malloc(sizeof(BNODEPTR));
- return p;
- }
- BNODEPTR* maketree(int x)
- {
- BNODEPTR* p;
- p = getNode();
- p->info = x;
- p->count=0;
- p->left = NULL;
- p->right = NULL;
- return p;
- }
- void setLeft(BNODEPTR** p, int x)
- {
- BNODEPTR* q;
- q = *p;
- if(q == NULL)
- {
- printf("void insertion");
- }
- else if (q->left != NULL)
- {
- printf("cannot insert");
- }
- else
- {
- q->left = maketree (x);
- //printf("\nset left");
- }
- }
- void setRight(BNODEPTR** p, int x)
- {
- BNODEPTR* q;
- q = *p;
- if(q == NULL)
- {
- printf("void insertion");
- }
- else if (q->right != NULL)
- {
- printf("cannot insert");
- }
- else
- {
- q->right = maketree (x);
- //printf("\nset right");
- }
- }
- void insert(BNODEPTR**root,int x)
- {
- BNODEPTR *p,*q;
- p = *root;
- if(p==NULL)
- {
- p = maketree(x);
- *root = p;
- //printf("\nset root");
- }
- else
- {
- p=q=*root;
- while(x != p->info && q !=NULL)
- {
- p=q;
- if(x<p->info)
- q=p->left;
- else
- q=p->right;
- }
- if(x==p->info)
- {
- p->count++;
- //printf("\nDuplicate data");
- }
- else if(x<p->info)
- setLeft(&p,x);
- else
- setRight (&p,x);
- }
- }
- void inorder(BNODEPTR *root)
- {
- //printf("file address %p",fp);
- if(root!=NULL)
- {
- inorder(root->left);
- do
- {
- fprintf(fp," %d",root->info);
- }while((root->count--)!=0);
- inorder(root->right);
- }
- //fclose(fp);
- }
- void freetree(BNODEPTR *root)
- {
- //printf("file address %p",fp);
- if(root!=0)
- {
- freetree(root->left);
- freetree(root->right);
- free(root);
- }
- } //fclose(fp);
- void print(char *pr)
- {
- printf("Printing %s",pr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement