Binary search tree help please

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

Join Date: Sep 2008
Posts: 31
Reputation: JustLearning is an unknown quantity at this point 
Solved Threads: 0
JustLearning JustLearning is offline Offline
Light Poster

Binary search tree help please

 
0
  #1
Oct 30th, 2008
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.
  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

  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. }

  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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 334
Reputation: Prabakar is on a distinguished road 
Solved Threads: 29
Prabakar's Avatar
Prabakar Prabakar is offline Offline
Posting Whiz

Re: Binary search tree help please

 
0
  #2
Oct 31st, 2008
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

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: Binary search tree help please

 
0
  #3
Oct 31st, 2008
Also, when working with classes that allocate memory dynamically, you HAVE to write destructor that will free all allocated memory!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
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