Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. #include <cmath>
  5. #include <iomanip>
  6. #include <fstream>
  7. #include <cstdlib>
  8. #include <sstream>
  9. #include <string>
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <stdlib.h>
  13. #include <time.h>
  14. #include <ctime>
  15.  
  16.  
  17. ofstream outf;
  18. ifstream inf;
  19.  
  20. struct Tnode
  21. {
  22. Tnode *leftchild;
  23. string letter;
  24. string code;
  25. Tnode *rightchild;
  26. };
  27.  
  28. Tnode *root;
  29.  
  30. Tnode* MyNewNode();
  31. void insert(Tnode *, string, string);
  32. void printTree(Tnode *);
  33. void printTree2(Tnode *);
  34. void printTree3(Tnode *);
  35.  
  36. int main()
  37. {
  38. outf.open("results.txt");
  39. inf.open("morsecode.txt");
  40.  
  41. if (!inf)
  42. {
  43. cout << "Error opening file" << endl;
  44.  
  45. return 0;
  46. }
  47.  
  48. string a, b, ary[100], ary2[100];
  49. char k;
  50. int x = 0;
  51.  
  52.  
  53. while(!inf.eof()){
  54.  
  55. inf >> a >> b;
  56. ary[x] = a;
  57. ary2[x] = b;
  58.  
  59. x++;
  60.  
  61. insert(root, a, b);
  62. }
  63.  
  64.  
  65. printTree(root);
  66.  
  67. outf << endl;
  68.  
  69. printTree2(root);
  70.  
  71. outf << endl;
  72.  
  73. printTree3(root);
  74.  
  75.  
  76. }
  77.  
  78. Tnode* MyNewNode(){
  79.  
  80. Tnode *temp;
  81. temp = new Tnode;
  82. temp->letter.clear();
  83. temp->code.clear();
  84. temp->rightchild = NULL;
  85. temp->leftchild = NULL;
  86. return temp;
  87.  
  88. }
  89.  
  90. void insert(Tnode *c, string targ, string targ2){
  91.  
  92.  
  93. Tnode *p;
  94. p = NULL;
  95.  
  96. while(c != NULL){
  97. p = c;
  98.  
  99. if(targ2 < c->code){
  100.  
  101. c = c->leftchild;
  102.  
  103. }
  104. else{
  105.  
  106. c = c->rightchild;
  107.  
  108. }
  109.  
  110. }
  111.  
  112. c = MyNewNode();
  113. c->letter = targ;
  114. c->code = targ2;
  115.  
  116. if(p == NULL){
  117.  
  118. root = c;
  119. }
  120.  
  121.  
  122. if(targ2 < p->code){
  123.  
  124. p->leftchild = c;
  125. }
  126. if(targ2 > p->code){
  127.  
  128. p->rightchild = c;
  129. }
  130.  
  131.  
  132.  
  133. }
  134.  
  135. void printTree(Tnode *c){
  136.  
  137. if(c == NULL){
  138.  
  139. return;
  140. }
  141.  
  142. printTree(c->leftchild);
  143. outf << c->letter << " ";
  144. printTree(c->rightchild);
  145.  
  146. }
  147. void printTree2(Tnode *c){
  148.  
  149. if(c == NULL){
  150.  
  151. return;
  152. }
  153.  
  154. outf << c->code << " ";
  155. printTree(c->leftchild);
  156. printTree(c->rightchild);
  157.  
  158. }
  159.  
  160. void printTree3(Tnode *c){
  161.  
  162. if(c == NULL){
  163.  
  164. return;
  165. }
  166.  
  167. printTree(c->leftchild);
  168. printTree(c->rightchild);
  169. outf << c->letter << " ";
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement