shridharshetti

bstoptab

Nov 29th, 2018
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. FILE *pfile;
  6. FILE *ofile;
  7. struct tnode
  8. {
  9. char str[100];
  10. int info;
  11. struct tnode *left,*right;
  12. };
  13. struct tnode *root=NULL;
  14.  
  15. struct node *head=NULL;
  16. struct tnode *insert(struct tnode *,int,char[]);
  17. struct tnode *insertpos(struct tnode *,struct tnode *);
  18. void inorder(struct tnode *);
  19. void bst(){
  20. char mnemonic[10];
  21. int opcode,t;
  22. pfile = fopen("opcodetable.txt","r");
  23. ofile = fopen("opcode.txt","w");
  24. while(feof(pfile)==0)
  25. {
  26. fscanf(pfile,"%s %x", mnemonic, &opcode);
  27. root=insert(root,opcode,mnemonic);
  28. }
  29. inorder(root);
  30. }
  31.  
  32. struct tnode * insert(struct tnode *root,int val,char *val2)
  33. {
  34. struct tnode *tmp;
  35.  
  36. tmp=(struct tnode *)malloc(sizeof(struct tnode));
  37.  
  38. tmp->info=val;
  39. strcpy(tmp->str,val2);
  40.  
  41. tmp->right=tmp->left=NULL;
  42.  
  43. if(root==NULL)
  44. {
  45. root=tmp;
  46. return(root);
  47. }
  48. else
  49. root= insertpos(root,tmp);
  50.  
  51. return(root);
  52. }
  53.  
  54. struct tnode * insertpos(struct tnode *root,struct tnode*tmp)
  55. {
  56. if(strcmp(root->str,tmp->str)>0)
  57. {
  58. if(root->left==NULL)
  59. {
  60. root->left=tmp;
  61. }
  62. else
  63. insertpos(root->left,tmp);
  64.  
  65. }
  66.  
  67. else if(strcmp(root->str,tmp->str)<0)
  68. {
  69. if(root->right==NULL)
  70. {
  71. root->right=tmp;
  72. }
  73. else
  74. insertpos(root->right,tmp);
  75. }
  76. return(root);
  77.  
  78. }
  79.  
  80.  
  81. void inorder(struct tnode *root)
  82. {
  83. if(root!=NULL)
  84. {
  85. inorder(root->left);
  86. fprintf(ofile,"%s \t %.2x\n",root->str,root->info);
  87. inorder(root->right);
  88. }
  89.  
  90. }
Add Comment
Please, Sign In to add comment