Reading a file into a binary search tree

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Reading a file into a binary search tree

 
0
  #1
May 17th, 2006
i know how to get a file and all but how do u go about putting that file into the tree?

my get file
  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
  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
  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. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Reading a file into a binary search tree

 
0
  #2
May 17th, 2006
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 91
Reputation: tyczj is an unknown quantity at this point 
Solved Threads: 1
tyczj tyczj is offline Offline
Junior Poster in Training

Re: Reading a file into a binary search tree

 
0
  #3
May 18th, 2006
yea i have an insert function already

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Reading a file into a binary search tree

 
0
  #4
May 18th, 2006
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
  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?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3570 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC