Help with binary search tree insertion

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2006
Posts: 8
Reputation: noxee is an unknown quantity at this point 
Solved Threads: 0
noxee noxee is offline Offline
Newbie Poster

Help with binary search tree insertion

 
0
  #1
Nov 1st, 2006
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:
  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:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
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: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Help with binary search tree insertion

 
0
  #2
Nov 1st, 2006
What's wordstream.h linkedlist.h?

Please label which one is which for clarity
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Help with binary search tree insertion

 
0
  #3
Nov 1st, 2006
Originally Posted by iamthwee View Post
What's wordstream.h linkedlist.h?

Please label which one is which for clarity
I think he hasn't posted those files.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 8
Reputation: noxee is an unknown quantity at this point 
Solved Threads: 0
noxee noxee is offline Offline
Newbie Poster

Re: Help with binary search tree insertion

 
0
  #4
Nov 1st, 2006
Originally Posted by iamthwee View Post
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 8
Reputation: noxee is an unknown quantity at this point 
Solved Threads: 0
noxee noxee is offline Offline
Newbie Poster

Re: Help with binary search tree insertion

 
0
  #5
Nov 2nd, 2006
Disregard all the above.... it was working fine i was just outputting the wrong thing when i was testing...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC