943,945 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5213
  • C++ RSS
May 17th, 2006
0

Reading a file into a binary search tree

Expand Post »
i know how to get a file and all but how do u go about putting that file into the tree?

my get file
C++ Syntax (Toggle Plain Text)
  1. void BST::loadFile()
  2. {
  3. cout << "Enter the the file location" << endl;
  4. cin >> inFileName;
  5. inFile.open(inFileName.c_str());
  6. if (!inFile.is_open()) //test for file
  7. {
  8. cerr << "Cannot open file: " << inFileName << endl;
  9. getche();
  10. }
  11. while(!inFile.eof()) //loop through untill end of file
  12. {
  13. inFile >> lastName >> firstName;
  14.  
  15. cout << lastName << ", " << firstName << endl;
  16.  
  17. }// end obtain info
  18.  
  19. inFile.close();//close infile
  20. }

do i just go key = inFile
here is my key struct
C++ Syntax (Toggle Plain Text)
  1. struct Key {
  2. char* data; // string
  3.  
  4. Key();
  5. Key(char* data) { this->data = new char[strlen(data)];
  6. strcpy(this->data,data);}
  7. ~Key() {delete data;};
  8. Key(Key& key);
  9. void print();
  10.  
  11. Key& operator= (Key& key);
  12. bool operator== (Key& key) { return 0 == strcmp(this->data,key.data);} // is this == to that key
  13. bool operator< (Key& key) { return -1 == strcmp(this->data,key.data);}// is this < that key
  14. bool operator> (Key& key) { return 1 == strcmp(this->data,key.data);} // is this > that key
  15. };
  16.  
  17.  
  18. Key::Key()
  19. {
  20. data = NULL;
  21. }
  22.  
  23. Key::Key(Key& key)
  24. {
  25. data = key.data;
  26. }
  27.  
  28. Key& Key::operator= (Key& key)
  29. {
  30. data = key.data;
  31. return *this;
  32. }
  33.  
  34. void Key::print()
  35. {
  36. cout << data << endl;
  37. }

my BST Class
C++ Syntax (Toggle Plain Text)
  1. class BST_Node {
  2. private:
  3. Key key; // key holds the data
  4. BST_Node* left; // ptr to left subtree
  5. BST_Node* right; // ptr to right subtree
  6.  
  7. public:
  8. // Managers
  9. BST_Node();
  10. BST_Node(Key key); // Construct given key-data
  11. BST_Node(BST_Node& node); // Copy Constructor
  12. ~BST_Node(); // Destruct node
  13.  
  14. // Operators
  15. BST_Node& operator= (BST_Node& node); // Assignment
  16.  
  17. // Accessors
  18. Key getKey() {return key;}; // get Key Data
  19. BST_Node* getLeft() {return left;}; // get root of left subtree
  20. BST_Node* getRight() {return right;}; // get root of right subtree
  21. void setLeft(BST_Node* node);
  22. void setRight(BST_Node* node);
  23. };
  24.  
  25. BST_Node::BST_Node()
  26. {
  27. key.data = NULL;
  28. left = NULL;
  29. right = NULL;
  30. }
  31.  
  32. BST_Node::BST_Node(Key key)
  33. {
  34.  
  35. cout << "enter key" << endl;
  36. cin >> key.data;
  37.  
  38. }
  39.  
  40. BST_Node::BST_Node(BST_Node& node)
  41. {
  42. right = node.right;
  43. left = node.left;
  44. //key = node.key;
  45. }
  46.  
  47. BST_Node::~BST_Node()
  48. {
  49. delete left;
  50. delete right;
  51. }
  52.  
  53. BST_Node& BST_Node::operator= (BST_Node& node)
  54. {
  55. right = node.right;
  56. left = node.left;
  57. //key = node.key;
  58. return *this;
  59. }
  60.  
  61. void BST_Node::setLeft(BST_Node* node)
  62. {
  63. this->left = node;
  64. }
  65.  
  66. void BST_Node::setRight(BST_Node* node)
  67. {
  68. this->right = node;
  69. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
tyczj is offline Offline
91 posts
since Mar 2005
May 17th, 2006
0

Re: Reading a file into a binary search tree

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 18th, 2006
0

Re: Reading a file into a binary search tree

yea i have an insert function already

C++ Syntax (Toggle Plain Text)
  1. bool BST::insert(BST_Node* &subRoot, Key key)
  2. {
  3. BST_Node* node = new BST_Node(key);
  4. if(!subRoot)
  5. {
  6. subRoot = node;
  7. return true;
  8. }
  9. if(key == root->getKey())
  10. {
  11. return false;
  12. delete node;
  13. }
  14. if(key < subRoot->getKey())
  15. {
  16. if(subRoot->getLeft() == NULL)
  17. {
  18. root->setLeft(new BST_Node(key));
  19. }
  20. else
  21. {
  22. insert(subRoot->getRight(), key.data);
  23. }
  24. }
  25. }
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
tyczj is offline Offline
91 posts
since Mar 2005
May 18th, 2006
0

Re: Reading a file into a binary search tree

So just use the insert function in main.

The only thing I would do different is to use ofstream and ifstream for file i/o. And avoid anything with (EOF) like the plague.

Depending how your file is structured, will determine how you read it in. I'm assuming it looks something like this:

file.txt
C++ Syntax (Toggle Plain Text)
  1. Smith Chris
  2. Jones Antony
  3. Anderson Clive

Therefore you could read in each line into the binary search tree, using the getline command?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 16th, 2010
0
Re: Reading a file into a binary search tree
I need some help to read data from file to BST
C++ Syntax (Toggle Plain Text)
  1. /*
  2. Name: Nguyen Ngoc Hoang
  3. Student COde: 08020167
  4. K53D- UET- VNU
  5. Assigment 01
  6. DSA
  7. */
  8. #include<iostream>
  9. #include<string>
  10. #include<fstream>
  11. #include<algorithm>
  12. #include<iomanip>
  13.  
  14. using namespace std;
  15.  
  16. struct Data // Data include keyword and number of occurrences
  17. {
  18. string key;
  19. int count;
  20. };
  21.  
  22. struct Node //A Node inclues Data, left and right
  23. {
  24. Data data;
  25. Node* left;
  26. Node* right;
  27. Node(Data d,Node* l,Node* r)
  28. {
  29. data=d;
  30. left=l;
  31. right=r;
  32. }
  33. };
  34.  
  35. class BSTree
  36. {
  37. private:
  38. static const int N = 100;
  39. Node *root;
  40. public:
  41. //Construct an empty BSTree
  42. BSTree()
  43. {
  44. root=NULL;
  45. }
  46. //Create a BSTree from a file
  47. BSTree(BSTree &T , char *input)
  48. {
  49. ifstream fin;
  50. fin.open(input);
  51. if(!fin.good())
  52. {
  53. cout<<"Error!File wordcount.in is not exits!"<<endl;
  54. cout<<"Please check again !"<<endl;
  55. }
  56. Data getdata;
  57. getdata.count=1;
  58. string keyword;
  59. while(fin.good())
  60. {
  61. fin>>keyword;
  62. strcpy(keyword,getdata.key);
  63. T.InsertData(getdata);
  64. }
  65. fin.close();
  66. }
  67. //Destruct the BSTree
  68. // ~BSTree();
  69. int IsEmpty();
  70. //Print all values of nodes inoder traversal
  71. void PrintTree(char* output);
  72. void Print_Tree(Node*,char* output);
  73. //Insert a value to BSTree
  74. void InsertData(Data DataInsert);
  75. void AddNode(Node *v,Data DataInsert);
  76. };
  77. int BSTree::IsEmpty()
  78. {
  79. return root==NULL;
  80. }
  81. void BSTree::PrintTree(char* output)
  82. {
  83. Print_Tree(root,output);
  84. }
  85. void BSTree::Print_Tree(Node* p,char* output)
  86. {
  87. ofstream fout;
  88. fout.open(output);
  89. if(p!=NULL)
  90. {
  91. Print_Tree(p->left,output);
  92. fout<<p->data.key<<"\t";
  93. fout<<p->data.count<<endl;
  94. //fout<<"hoang huong";
  95. Print_Tree(p->right,output);
  96. }
  97. fout.close();
  98. }
  99.  
  100. void BSTree::InsertData(Data DataInsert)
  101. {
  102. if(root==NULL) root=new Node(DataInsert,NULL,NULL );
  103. else AddNode(root,DataInsert);
  104. }
  105. void BSTree::AddNode(Node *v,Data ValueInsert)
  106. {
  107.  
  108. if(v->data.key==ValueInsert.key)
  109. {
  110. ValueInsert.count++;
  111. return;
  112. }
  113. else
  114. {
  115. if(v->data.key > ValueInsert.key)
  116. {
  117. if(v->left!=NULL) AddNode(v->left,ValueInsert);
  118. else v->left=new Node(ValueInsert,NULL,NULL);
  119. }
  120. else
  121. {
  122. if(v->right!=NULL) AddNode(v->right,ValueInsert);
  123. else v->right=new Node(ValueInsert,NULL,NULL);
  124. }
  125. }
  126. }
  127. //main function
  128. int main()
  129. {
  130. BSTree T;
  131. BSTree Tree = BSTree(T ,"wordcount.in");
  132. T.PrintTree("wordcount.out");
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. //count the number of Node
  141. int CountNode(Node* root)
  142. {
  143. if(root=NULL) return 0;
  144. else
  145. {
  146. int count=1;
  147. return CountNode(root->left)+CountNode(root->right);
  148. }
  149. }
  150. //Compare key of Node with a string
  151. bool Compare(Node* n, string s)
  152. {
  153. while(true)
  154. {
  155. if (n==NULL) return false;
  156. else if(s==(n->data).key) return true;
  157. else if(s<n->data.key) n=n->left;
  158. else n=n->right;
  159.  
  160. }
  161. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hoangnn90 is offline Offline
1 posts
since Nov 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Classes
Next Thread in C++ Forum Timeline: getline not reading in the last line in a file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC