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

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2009
Posts: 293
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 1
lotrsimp12345 lotrsimp12345 is offline Offline
Posting Whiz in Training

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

 
0
  #1
Nov 9th, 2009
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

  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
  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
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 19
Reputation: Kontained is an unknown quantity at this point 
Solved Threads: 5
Kontained Kontained is offline Offline
Newbie Poster
 
0
  #2
Nov 9th, 2009
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
Reply With Quote Quick reply to this message  
Reply

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




Views: 434 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC