944,010 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 6433
  • C++ RSS
Nov 1st, 2006
0

Help with binary search tree insertion

Expand Post »
I'm having a problem inserting data into my binary search tree, it will insert some of it but not all.

this is the header file for my BST class:
c++ Syntax (Toggle Plain Text)
  1.  
  2. #ifndef BST_H
  3. #define BST_H
  4.  
  5. //-----------------------------------------------------------------------------------------
  6.  
  7. #include <string>
  8. #include "LinkedList.h"
  9.  
  10. //-----------------------------------------------------------------------------------------
  11.  
  12. using namespace std;
  13.  
  14. //-----------------------------------------------------------------------------------------
  15.  
  16. class BST
  17. {
  18. public:
  19. //Default constructor, sets the address of root to NULL
  20. BST ();
  21.  
  22. //Deconstructor
  23. ~BST () {};
  24.  
  25. //Insert new data into binary search tree
  26. bool Insert (string &word, int docID);
  27.  
  28. //Find the word passed in as a parameter and copy document list
  29. bool Find (string &word, LinkedList &list);
  30.  
  31. //friend ostream &operator << (ostream &ostr, BST &BSTree);
  32.  
  33. private:
  34. struct BSTNode {
  35. string Word; //contains a single word
  36. LinkedList DocList; //a linked list of document IDs
  37. BSTNode *left; //points to the left child
  38. BSTNode *right; //points to the right child
  39. };
  40.  
  41. BSTNode * root;
  42.  
  43. //Insert into subtree
  44. bool InsertIntoSubTree(BSTNode &parent, BSTNode &newNode);
  45. };
  46. #endif

Here is the .cpp implementation:
c++ Syntax (Toggle Plain Text)
  1. #include "BST.h"
  2.  
  3. BST::BST()
  4. {
  5. root = NULL;
  6. }
  7.  
  8. //-----------------------------------------------------------------------------------------
  9. // Insert new data into binary search tree
  10. //-----------------------------------------------------------------------------------------
  11. bool BST::Insert(string &word, int docID)
  12. {
  13. BSTNode *newNode = new BSTNode;
  14. bool success = false;
  15.  
  16. newNode->Word = word;
  17. newNode->DocList.Insert(docID);
  18. newNode->left = NULL;
  19. newNode->right = NULL;
  20.  
  21. if (root == NULL)
  22. {
  23. root = newNode;
  24. success = true;
  25. cout << "root: " << root->Word << endl;
  26. }
  27. else if (root->Word == word)
  28. {
  29. return false;
  30. }
  31. else
  32. {
  33. success = InsertIntoSubTree(*root, *newNode);
  34. }
  35.  
  36. return success;
  37. }
  38.  
  39. //-----------------------------------------------------------------------------------------
  40. // Insert into the BST sub branch
  41. //-----------------------------------------------------------------------------------------
  42. bool BST::InsertIntoSubTree(BSTNode &parent, BSTNode &newNode)
  43. {
  44. bool success = false;
  45.  
  46. if (newNode.Word < parent.Word)
  47. {
  48. if (parent.left == NULL)
  49. {
  50. parent.left = &newNode;
  51. success = true;
  52. cout << "left: " << parent.Word << endl;
  53. }
  54. else
  55. {
  56. success = InsertIntoSubTree(*parent.left, newNode);
  57. }
  58. }
  59. else
  60. {
  61. if (parent.right == NULL)
  62. {
  63. parent.right = &newNode;
  64. success = true;
  65. cout << "right: " << parent.Word << endl;
  66. }
  67. else
  68. {
  69. success = InsertIntoSubTree(*parent.right, newNode);
  70. }
  71. }
  72.  
  73. return success;
  74. }
  75.  
  76. //-----------------------------------------------------------------------------------------
  77. /*ostream &operator << (ostream &ostr, BST &BSTree)
  78. {
  79.   //ostr << "The document numbers contained in the list:";
  80.  
  81.   for (BSTNode *ptr = &BSTree.root; ptr-> != NULL; ptr = ptr->left)
  82.   {
  83.   ostr << ptr.Word;
  84.   }
  85.  
  86.   return ostr;
  87. }*/

And finally this is what I am trying to do:
c++ Syntax (Toggle Plain Text)
  1. // BSTTest.cpp
  2. //--------------------------------------------------------------------
  3.  
  4. #include "WordStream.h"
  5. #include "BST.h"
  6.  
  7. //--------------------------------------------------------------------
  8.  
  9. using namespace std;
  10.  
  11. //--------------------------------------------------------------------
  12.  
  13. int main()
  14. {
  15. BST * newBST = new BST;
  16. string filename = "test.txt";
  17. WordStream file(filename);
  18. string word;
  19.  
  20. while (file.Next(word)) //continuially reads a word from a file until end of file is reached
  21. {
  22. cout << "Input word: " << word << endl; //display what is being inputed
  23. newBST->Insert(word,1);
  24. }
  25.  
  26. system("pause");
  27.  
  28. return 0;
  29. }
Last edited by noxee; Nov 1st, 2006 at 12:07 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
noxee is offline Offline
8 posts
since Aug 2006
Nov 1st, 2006
0

Re: Help with binary search tree insertion

What's wordstream.h linkedlist.h?

Please label which one is which for clarity
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Nov 1st, 2006
0

Re: Help with binary search tree insertion

Click to Expand / Collapse  Quote originally posted by iamthwee ...
What's wordstream.h linkedlist.h?

Please label which one is which for clarity
I think he hasn't posted those files.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Nov 1st, 2006
0

Re: Help with binary search tree insertion

Click to Expand / Collapse  Quote originally posted by iamthwee ...
What's wordstream.h linkedlist.h?

Please label which one is which for clarity
I haven't included them in the post, they aren't really relevant to the problem. All WordStream.h does is read in a word from a specified file source and it works fine, I've all ready tested it. LinkedList.h just generates and linked list from a given integer, again this works fine and doesn't really have and relevance to the insertion problem
Reputation Points: 10
Solved Threads: 0
Newbie Poster
noxee is offline Offline
8 posts
since Aug 2006
Nov 2nd, 2006
0

Re: Help with binary search tree insertion

Disregard all the above.... it was working fine i was just outputting the wrong thing when i was testing...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
noxee is offline Offline
8 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Help with Assignment
Next Thread in C++ Forum Timeline: Initializing Multidimensional Arrays plz help!





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


Follow us on Twitter


© 2011 DaniWeb® LLC