Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Emmanuel Sammy
- 815007829
- COMP 2000
- */
- /*I attempted to solve this assignment by using an array of binary trees,
- however for some reason the program kept crashing beyond my control even after allocating space for the tree.
- it works perfectly with one tree and everything gets sorted correctly etc.
- each tree in the array would have been for a team and the array would have been sorted to determine the team positions,
- 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*/
- #include <stdio.h>
- #include <iostream>
- #include <stdlib.h>
- #include <fstream>
- #include <sstream>
- #include <string.h>
- using namespace std;
- struct nodeData{
- char word[20];
- };
- typedef struct Node{ // Node structure of tree.
- nodeData data;
- struct Node *left;
- struct Node *right;
- }*treePtr;
- struct BTree{
- treePtr root[10];
- };
- treePtr createNode(nodeData d){// create space for node and put data in node.
- treePtr p =(treePtr)malloc(sizeof(Node));
- p->data=d;
- p->left=p->right=NULL;
- return p;
- }
- nodeData newNodeData(char s[]){
- nodeData temp;
- strcpy(temp.word,s);
- return temp;
- }
- treePtr search(string word, treePtr root){
- treePtr c = root; // let traverse node be root to start.
- if(root!=NULL){ //make sure tree not empty
- while(word.compare(c->data.word)!=0){ // if root is not the key
- if (word.compare(c->data.word)<0){ //if key is less go left
- c=c->left;
- }
- else{
- c=c->right;// if key is greater go right
- }
- if (c==NULL)
- return NULL;// return null if tree reaches end.
- }
- return c;// return node if word is found.
- }
- }
- void insert(nodeData d,treePtr root){ // insert data into the tree.
- treePtr newNode=createNode(d);// create new node
- string str(d.word);
- treePtr a= search(str,root);// search for string
- if(a!=NULL){ // if node is not empty node exists
- return;
- }
- else{// add node to tree
- if(root==NULL){
- root=newNode;
- }
- else {
- treePtr p=NULL;
- treePtr c=root;
- while (1){
- p = c;
- if (strcmp(d.word,c->data.word)<0){ // if word is less go left
- c=c->left;
- if (c==NULL){
- p->left=newNode;
- break;
- }
- }//endif go left
- else { // else go right
- c=c->right;
- if (c==NULL){
- p->right=newNode;
- break;
- }
- }//end else go right
- }// end while
- }// end else if root empty
- }
- }// end insert
- void inOrder(treePtr root, ofstream &out){ // traverse tree inorder
- if (root != NULL){
- inOrder(root->left,out);
- printf("%s ",root->data.word);
- out<<root->data.word<<endl;
- inOrder(root->right,out);
- }
- }
- treePtr getsmallest(treePtr root){
- while(root!=NULL){
- root=root->left;
- }
- return root;
- }
- int main(){
- //treePtr root=(treePtr)malloc(sizeof(Node));
- treePtr root[10];
- for(int k=0;k<10;k++){
- root[k]=(treePtr)malloc(sizeof(Node));
- }
- string word;
- string name;
- char ch[20];
- int i=0;
- ifstream in;
- ofstream out;
- in.open("input.txt");
- out.open("output.txt");
- if (!in.is_open()){
- cerr<<"Error Opening File"<<endl;
- return 0;
- }
- if (!out.is_open()){
- cerr<<"Error Opening File"<<endl;
- return 0;
- }
- while(getline(in,word)){
- int n=word.length();
- for(int j=0;j<n;j++){
- word[j]=tolower(word[j]);
- }
- istringstream iss(word,istringstream::in);
- while(iss>>name){
- name[0]=toupper(name[0]);
- strcpy(ch,name.c_str());
- insert(newNodeData(ch),root[i]);
- //insert(newNodeData(ch),root);
- }
- i++;
- }
- //inOrder(root,out);
- inOrder(root[6],out);
- in.close();
- out.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement