Advertisement
Guest User

COMP 2K A2

a guest
Jun 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. /*
  2. Emmanuel Sammy
  3. 815007829
  4. COMP 2000
  5. */
  6. /*I attempted to solve this assignment by using an array of binary trees,
  7. however for some reason the program kept crashing beyond my control even after allocating space for the tree.
  8. it works perfectly with one tree and everything gets sorted correctly etc.
  9. each tree in the array would have been for a team and the array would have been sorted to determine the team positions,
  10. this unfortunately did not work in my favour and would hope to acquire assistance in the future as to how i would have possibly done this*/
  11.  
  12. #include <stdio.h>
  13. #include <iostream>
  14. #include <stdlib.h>
  15. #include <fstream>
  16. #include <sstream>
  17. #include <string.h>
  18.  
  19. using namespace std;
  20.  
  21. struct nodeData{
  22. char word[20];
  23. };
  24.  
  25. typedef struct Node{ // Node structure of tree.
  26. nodeData data;
  27. struct Node *left;
  28. struct Node *right;
  29. }*treePtr;
  30.  
  31. struct BTree{
  32. treePtr root[10];
  33. };
  34.  
  35. treePtr createNode(nodeData d){// create space for node and put data in node.
  36. treePtr p =(treePtr)malloc(sizeof(Node));
  37. p->data=d;
  38. p->left=p->right=NULL;
  39. return p;
  40. }
  41.  
  42. nodeData newNodeData(char s[]){
  43. nodeData temp;
  44. strcpy(temp.word,s);
  45. return temp;
  46. }
  47.  
  48. treePtr search(string word, treePtr root){
  49. treePtr c = root; // let traverse node be root to start.
  50. if(root!=NULL){ //make sure tree not empty
  51. while(word.compare(c->data.word)!=0){ // if root is not the key
  52. if (word.compare(c->data.word)<0){ //if key is less go left
  53. c=c->left;
  54. }
  55. else{
  56. c=c->right;// if key is greater go right
  57. }
  58. if (c==NULL)
  59. return NULL;// return null if tree reaches end.
  60. }
  61. return c;// return node if word is found.
  62. }
  63. }
  64.  
  65. void insert(nodeData d,treePtr root){ // insert data into the tree.
  66. treePtr newNode=createNode(d);// create new node
  67. string str(d.word);
  68. treePtr a= search(str,root);// search for string
  69. if(a!=NULL){ // if node is not empty node exists
  70. return;
  71. }
  72. else{// add node to tree
  73. if(root==NULL){
  74. root=newNode;
  75. }
  76. else {
  77. treePtr p=NULL;
  78. treePtr c=root;
  79. while (1){
  80. p = c;
  81. if (strcmp(d.word,c->data.word)<0){ // if word is less go left
  82. c=c->left;
  83. if (c==NULL){
  84. p->left=newNode;
  85. break;
  86. }
  87. }//endif go left
  88. else { // else go right
  89. c=c->right;
  90. if (c==NULL){
  91. p->right=newNode;
  92. break;
  93. }
  94. }//end else go right
  95. }// end while
  96. }// end else if root empty
  97. }
  98. }// end insert
  99.  
  100. void inOrder(treePtr root, ofstream &out){ // traverse tree inorder
  101. if (root != NULL){
  102. inOrder(root->left,out);
  103. printf("%s ",root->data.word);
  104. out<<root->data.word<<endl;
  105. inOrder(root->right,out);
  106. }
  107. }
  108.  
  109. treePtr getsmallest(treePtr root){
  110. while(root!=NULL){
  111. root=root->left;
  112. }
  113. return root;
  114. }
  115.  
  116. int main(){
  117. //treePtr root=(treePtr)malloc(sizeof(Node));
  118. treePtr root[10];
  119. for(int k=0;k<10;k++){
  120. root[k]=(treePtr)malloc(sizeof(Node));
  121. }
  122. string word;
  123. string name;
  124. char ch[20];
  125. int i=0;
  126. ifstream in;
  127. ofstream out;
  128. in.open("input.txt");
  129. out.open("output.txt");
  130. if (!in.is_open()){
  131. cerr<<"Error Opening File"<<endl;
  132. return 0;
  133. }
  134. if (!out.is_open()){
  135. cerr<<"Error Opening File"<<endl;
  136. return 0;
  137. }
  138. while(getline(in,word)){
  139. int n=word.length();
  140. for(int j=0;j<n;j++){
  141. word[j]=tolower(word[j]);
  142. }
  143. istringstream iss(word,istringstream::in);
  144. while(iss>>name){
  145. name[0]=toupper(name[0]);
  146. strcpy(ch,name.c_str());
  147. insert(newNodeData(ch),root[i]);
  148. //insert(newNodeData(ch),root);
  149. }
  150. i++;
  151. }
  152. //inOrder(root,out);
  153. inOrder(root[6],out);
  154. in.close();
  155. out.close();
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement