Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- FILE *pfile;
- FILE *ofile;
- struct tnode
- {
- char str[100];
- int info;
- struct tnode *left,*right;
- };
- struct tnode *root=NULL;
- struct node *head=NULL;
- struct tnode *insert(struct tnode *,int,char[]);
- struct tnode *insertpos(struct tnode *,struct tnode *);
- void inorder(struct tnode *);
- void bst(){
- char mnemonic[10];
- int opcode,t;
- pfile = fopen("opcodetable.txt","r");
- ofile = fopen("opcode.txt","w");
- while(feof(pfile)==0)
- {
- fscanf(pfile,"%s %x", mnemonic, &opcode);
- root=insert(root,opcode,mnemonic);
- }
- inorder(root);
- }
- struct tnode * insert(struct tnode *root,int val,char *val2)
- {
- struct tnode *tmp;
- tmp=(struct tnode *)malloc(sizeof(struct tnode));
- tmp->info=val;
- strcpy(tmp->str,val2);
- tmp->right=tmp->left=NULL;
- if(root==NULL)
- {
- root=tmp;
- return(root);
- }
- else
- root= insertpos(root,tmp);
- return(root);
- }
- struct tnode * insertpos(struct tnode *root,struct tnode*tmp)
- {
- if(strcmp(root->str,tmp->str)>0)
- {
- if(root->left==NULL)
- {
- root->left=tmp;
- }
- else
- insertpos(root->left,tmp);
- }
- else if(strcmp(root->str,tmp->str)<0)
- {
- if(root->right==NULL)
- {
- root->right=tmp;
- }
- else
- insertpos(root->right,tmp);
- }
- return(root);
- }
- void inorder(struct tnode *root)
- {
- if(root!=NULL)
- {
- inorder(root->left);
- fprintf(ofile,"%s \t %.2x\n",root->str,root->info);
- inorder(root->right);
- }
- }
Add Comment
Please, Sign In to add comment