943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 849
  • C++ RSS
Oct 30th, 2008
0

Binary search tree help please

Expand Post »
I need some help with this binary search tree. I am having a problem printing out the tree. I don't quite understand how to do it. I know that I need to use recursive call to traverse to the end of the tree but the way the header file is written I can not find any examples any where on the net like the header file. (Header file provided by instructor can not be changed) The instructions say when a 'p' is read in from the file the printforward function is to print out to the terminal the continents of the tree in order.
Can someone please help me?
Also did I set up the insert function correctly?
Here is what I have written so far plus the header and test driver.
c++ Syntax (Toggle Plain Text)
  1. #ifndef BSTREE_H
  2. #define BSTREE_H
  3.  
  4. #include <cstddef>
  5. #include <new>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. class FullBSTree // Exception class
  11. {
  12. };
  13.  
  14. class EmptyBSTree // Exception class
  15. {
  16. };
  17.  
  18. class NotFoundBSTree // Exception class
  19. {
  20. };
  21.  
  22. typedef char SomeType; // Set SomeType as a char
  23.  
  24. struct BSTreeNode // Node of BSTree
  25. {
  26. SomeType data; // Data stored in node
  27. BSTreeNode* leftPtr; // Pointer to left subtree
  28. BSTreeNode* rightPtr; // Pointer to right subtree
  29. };
  30.  
  31. class BSTree // BSTree Abstract Data Type
  32. {
  33. private:
  34. BSTreeNode* rootPtr; // Pointer to root of BSTree
  35.  
  36. public:
  37. BSTree(); // Default constructor
  38.  
  39. ~BSTree(); // Destructor
  40.  
  41. void InsertItem(SomeType item); // Inserts item into BSTree
  42.  
  43. void DeleteItem(SomeType item); // Deletes item from BSTree if present
  44.  
  45. void MakeEmpty(); // Deallocates all BSTree nodes and sets root to NULL
  46.  
  47. int Size() const; // Returns total number of data values stored
  48.  
  49. bool IsFull() const; // Returns true if BSTree full, false otherwise
  50.  
  51. bool IsEmpty() const; // Returns true if BSTree empty, false otherwise
  52.  
  53. string ForwardPrint() const; // Returns tree items concatenated in order for printing
  54.  
  55. string ReversePrint() const; // Returns tree items concatenated in reverse order for printing
  56. };
  57.  
  58.  
  59. #endif

c++ Syntax (Toggle Plain Text)
  1. #include "bstree.h"
  2. #include <new>
  3. #include <cstddef>
  4. #include <iostream>
  5. #include <string>
  6.  
  7. BSTree::BSTree() // Default constructor
  8. {
  9. rootPtr = NULL;
  10. }
  11.  
  12. BSTree::~BSTree() // Destructor
  13. {
  14. }
  15.  
  16. void BSTree::InsertItem(SomeType item) // Inserts item into BSTree
  17. {
  18. BSTreeNode* tempPtr, * leftPtr, * rightPtr;
  19. if (rootPtr == NULL)
  20. {
  21. tempPtr = new BSTreeNode;
  22. tempPtr->data = item;
  23. tempPtr->leftPtr = NULL;
  24. tempPtr->rightPtr = NULL;
  25. rootPtr = tempPtr;
  26. }
  27. else if (item < rootPtr->data)
  28. {
  29. tempPtr = new BSTreeNode;
  30. tempPtr->data = item;
  31. leftPtr = tempPtr;
  32. }
  33. else
  34. {
  35. tempPtr = new BSTreeNode;
  36. tempPtr->data = item;
  37. rightPtr = tempPtr;
  38. }
  39. cout << item;
  40.  
  41. }
  42.  
  43. void BSTree::DeleteItem(SomeType item) // Deletes item from BSTree if present
  44. {
  45. if (rootPtr == NULL)
  46. throw FullBSTree();
  47. if (item == rootPtr->data)
  48. {
  49. cout << rootPtr->data;
  50. delete rootPtr;
  51. }
  52. }
  53.  
  54. void BSTree::MakeEmpty() // Deallocates all BSTree nodes and sets root to NULL
  55. {
  56. }
  57.  
  58. int BSTree::Size() const // Returns total number of data values stored
  59. {
  60. BSTreeNode* tempPtr;
  61. int numberOfNodes = 0;
  62. if(rootPtr == NULL)
  63. return 0;
  64.  
  65. }
  66.  
  67. bool BSTree::IsFull() const // Returns true if BSTree full, false otherwise
  68. {
  69. BSTreeNode* fullPtr;
  70.  
  71. try
  72. {
  73. fullPtr = new BSTreeNode;
  74. delete fullPtr;
  75. return false;
  76. }
  77. catch ( bad_alloc )
  78. {
  79. return true;
  80. }
  81. }
  82.  
  83. bool BSTree::IsEmpty() const // Returns true if BSTree empty, false otherwise
  84. {
  85. return (rootPtr == NULL);
  86. }
  87.  
  88. /***************************************************
  89. here is the problem function
  90. ***************************************************/
  91. string BSTree::ForwardPrint() const // Returns tree items concatenated in order for printing
  92. {
  93. string inputs;
  94. if ( rootPtr != NULL )
  95. { // (Otherwise, there's nothing to print.)
  96. // Print the root item.
  97. while (rootPtr->leftPtr != NULL)
  98. {
  99. inputs = rootPtr->leftPtr->data;
  100. ForwardPrint();
  101. return inputs;
  102.  
  103. }
  104. // Print items in left subtree.
  105. ForwardPrint(); // Print items in right subtree.
  106. }
  107.  
  108.  
  109. }
  110.  
  111. string BSTree::ReversePrint() const // Returns tree items concatenated in reverse order for printing
  112. {
  113. }

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include "bstree.h"
  5.  
  6. using namespace std;
  7.  
  8. int main(int argc, char * const argv[])
  9. {
  10. BSTree tree;
  11. char command, letter;
  12. ifstream inputs;
  13. if (argc != 2)
  14. {
  15. cout << "Usage: \n program07 <inputfile>\n";
  16. return 1;
  17. }
  18. inputs.open(argv[1]);
  19. if (!inputs)
  20. {
  21. cout << "Error - unable to open input file";
  22. return 1;
  23. }
  24. inputs >> command;
  25.  
  26. while(!inputs.eof())
  27. {
  28. switch (command)
  29. {
  30. case 'c':
  31. {
  32. cout << "Constructor()" << endl;
  33. break;
  34. }
  35. case '+':
  36. {
  37. inputs >> letter;
  38. cout <<"+";
  39. tree.InsertItem(letter);
  40. cout << "\n";
  41. break;
  42. }
  43. case '-':
  44. {
  45. try
  46. {
  47. inputs >> letter;
  48. cout << "DeleteItem('" << letter << "')";
  49. tree.DeleteItem(letter);
  50. cout <<"- " << letter << "\n";;
  51. break;
  52. }
  53. catch(FullBSTree)
  54. {
  55. cout << " -- '" << letter << "' not found" << endl;
  56. }
  57. }
  58. case 'p':
  59. {
  60. cout << tree.ForwardPrint();
  61. cout <<"p\n";
  62. break;
  63. }
  64. case 'r':
  65. {
  66. tree.ReversePrint();
  67. cout <<"r\n";
  68. break;
  69. }
  70. case 's':
  71. {
  72. cout << tree.Size() << " ";
  73. cout <<"s\n";
  74. break;
  75. }
  76. case 'm':
  77. {
  78. tree.MakeEmpty();
  79. cout <<"m\n";
  80. break;
  81. }
  82. case 'd':
  83. {
  84. tree.~BSTree();
  85. cout <<"d\n";
  86. break;
  87. }
  88. default:
  89. {
  90. cout << "Command not recognized" << endl;
  91. }
  92.  
  93. }
  94. inputs >> command;
  95. }
  96.  
  97. //cout << command;
  98. system("pause");
  99. return 1;
  100. };
Last edited by JustLearning; Oct 30th, 2008 at 11:38 pm. Reason: Add information
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
JustLearning is offline Offline
31 posts
since Sep 2008
Oct 31st, 2008
0

Re: Binary search tree help please

Assuming that print forward is to print the data in ascending order all you have to do is inorder traversal
which should be something like this

cpp Syntax (Toggle Plain Text)
  1. void inorder(tree_ptr)
  2. {
  3. if ( tree_ptr -> left )
  4. inorder( tree_ptr->left);
  5. // print the value stored in this node
  6. if ( tree_ptr -> right )
  7. inorder( tree_ptr->right);
  8. }

And reverse print is the opposite of the above algorithm. go right then left. Google inorder traversal for explanation.

And inserting a node in the tree is not that simple as you have wrote. google it
Last edited by Prabakar; Oct 31st, 2008 at 8:34 am.
Reputation Points: 94
Solved Threads: 33
Posting Whiz
Prabakar is offline Offline
342 posts
since May 2008
Oct 31st, 2008
0

Re: Binary search tree help please

Also, when working with classes that allocate memory dynamically, you HAVE to write destructor that will free all allocated memory!
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008

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: prompting for input for inherited classes
Next Thread in C++ Forum Timeline: Word Location in Text File





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


Follow us on Twitter


© 2011 DaniWeb® LLC