944,198 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 805
  • C++ RSS
Nov 9th, 2009
0

creating a binary search tree but running into complications in object creati

Expand Post »
1>c:\users\aniket karmarkar\documents\visual studio 2008\projects\generic bst container\generic bst container\my_bst.h(80) : warning C4717: 'my_bst<int,int>::insert_sub' : recursive on all control paths, function will cause runtime stack overflow



TEST.CPP

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include "my_bst.h"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. my_bst<int,int> ab;
  8. ab.insert(5,10);
  9. return 0;
  10. }

MY_BST_NODE.H
C++ Syntax (Toggle Plain Text)
  1. #ifndef MY_BST_NODE_H
  2. #define MY_BST_NODE_H
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. template<class KT,class DT>
  9. class my_bst_node
  10. {
  11. public:
  12. inline my_bst_node()
  13. {
  14. left=right=0;
  15. }
  16. inline my_bst_node(KT tag,DT info, my_bst_node * l=0, my_bst_node * r=0)
  17. {
  18. key=tag;
  19. data=info;
  20. left=l;
  21. right=r;
  22. }
  23.  
  24. KT key;
  25. DT data;
  26. my_bst_node * left;
  27. my_bst_node * right;
  28. };
  29.  
  30. #endif

MY_BST.H
C++ Syntax (Toggle Plain Text)
  1. #ifndef MY_BST_H
  2. #define MY_BST_H
  3. #include <iostream>
  4. #include "my_bst_node.h"
  5.  
  6. using namespace std;
  7.  
  8. template<class KT,class DT>
  9. class my_bst
  10. {
  11. public:
  12. //default constructor
  13. my_bst();
  14.  
  15. //inserts a new node into binary tree
  16. void insert(KT searchkey, const DT &newDataItem);
  17.  
  18. //
  19. bool search(KT searchkey);
  20.  
  21. //delete node based on which key you want to delete
  22. void delet(KT searchkey);
  23.  
  24. //
  25. DT preorder_display(KT searchkey);
  26.  
  27. //
  28. DT inorder_display(KT searchkey);
  29.  
  30. //
  31. DT postorder_display(KT searchkey);
  32.  
  33. //returns height of BST
  34. int height() const;
  35.  
  36. //
  37. bool balanced() const;
  38.  
  39. //
  40. void show_bst_structure() const;
  41.  
  42. private:
  43. my_bst_node<KT,DT>* root;
  44.  
  45. void insert_sub(my_bst_node<KT,DT>*& root, KT searchkey, const DT & newDataItem);
  46.  
  47. int getHeightSub(my_bst_node<DT,KT> * p);
  48. };
  49.  
  50. template <class KT, class DT>
  51. my_bst<KT,DT>::my_bst(void)
  52. {
  53. root=0;
  54. }
  55.  
  56. template <class KT, class DT>
  57. void my_bst<KT,DT>::insert(KT searchkey, const DT &newDataItem)
  58. {
  59. insert_sub(root, searchkey, newDataItem);
  60. }
  61.  
  62. template <class KT, class DT>
  63. void my_bst<KT,DT>::insert_sub(my_bst_node<KT,DT>*& root, KT searchkey, const DT &newDataItem)
  64. {
  65. //if empty tree
  66. if(root=NULL)
  67. {
  68. root=new my_bst_node<KT,DT>(searchkey, newDataItem);
  69. }
  70. //key less than root nodes
  71. else if(searchkey<root->key)
  72. {
  73. insert_sub(root->left, searchkey, newDataItem);
  74. }
  75. //key greater than root nodes
  76. else
  77. {
  78. insert_sub(root->right, searchkey, newDataItem);
  79. }
  80. }
  81. #endif
Last edited by lotrsimp12345; Nov 9th, 2009 at 7:11 pm.
Similar Threads
Reputation Points: 47
Solved Threads: 2
Posting Pro in Training
lotrsimp12345 is offline Offline
413 posts
since Jun 2009
Nov 9th, 2009
0
Re: creating a binary search tree but running into complications in object creati
I'm not trying to offend you but your code seems overly complicated for creating binary search trees. Here is a link for a good tutorial. The code teaches you how to create nodes and how to search the tree, as well as traversal. The code is simple to understand and actually helped me understand binary trees better.
Last edited by Kontained; Nov 9th, 2009 at 8:18 pm. Reason: Typo
Reputation Points: 11
Solved Threads: 8
Light Poster
Kontained is offline Offline
34 posts
since Sep 2009

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 needed with Classes
Next Thread in C++ Forum Timeline: Delete the last line after finish writing to a file





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


Follow us on Twitter


© 2011 DaniWeb® LLC