| | |
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:
Solved Threads: 1
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
MY_BST_NODE.H
MY_BST.H
TEST.CPP
C++ Syntax (Toggle Plain Text)
#include <iostream> #include "my_bst.h" using namespace std; int main() { my_bst<int,int> ab; ab.insert(5,10); return 0; }
MY_BST_NODE.H
C++ Syntax (Toggle Plain Text)
#ifndef MY_BST_NODE_H #define MY_BST_NODE_H #include <iostream> using namespace std; template<class KT,class DT> class my_bst_node { public: inline my_bst_node() { left=right=0; } inline my_bst_node(KT tag,DT info, my_bst_node * l=0, my_bst_node * r=0) { key=tag; data=info; left=l; right=r; } KT key; DT data; my_bst_node * left; my_bst_node * right; }; #endif
MY_BST.H
C++ Syntax (Toggle Plain Text)
#ifndef MY_BST_H #define MY_BST_H #include <iostream> #include "my_bst_node.h" using namespace std; template<class KT,class DT> class my_bst { public: //default constructor my_bst(); //inserts a new node into binary tree void insert(KT searchkey, const DT &newDataItem); // bool search(KT searchkey); //delete node based on which key you want to delete void delet(KT searchkey); // DT preorder_display(KT searchkey); // DT inorder_display(KT searchkey); // DT postorder_display(KT searchkey); //returns height of BST int height() const; // bool balanced() const; // void show_bst_structure() const; private: my_bst_node<KT,DT>* root; void insert_sub(my_bst_node<KT,DT>*& root, KT searchkey, const DT & newDataItem); int getHeightSub(my_bst_node<DT,KT> * p); }; template <class KT, class DT> my_bst<KT,DT>::my_bst(void) { root=0; } template <class KT, class DT> void my_bst<KT,DT>::insert(KT searchkey, const DT &newDataItem) { insert_sub(root, searchkey, newDataItem); } template <class KT, class DT> void my_bst<KT,DT>::insert_sub(my_bst_node<KT,DT>*& root, KT searchkey, const DT &newDataItem) { //if empty tree if(root=NULL) { root=new my_bst_node<KT,DT>(searchkey, newDataItem); } //key less than root nodes else if(searchkey<root->key) { insert_sub(root->left, searchkey, newDataItem); } //key greater than root nodes else { insert_sub(root->right, searchkey, newDataItem); } } #endif
Last edited by lotrsimp12345; Nov 9th, 2009 at 7:11 pm.
•
•
Join Date: Sep 2009
Posts: 19
Reputation:
Solved Threads: 5
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
![]() |
Similar Threads
- Binary Search Tree - how to remove root (C++)
- Create code for Binary Search Tree using compareTo method using I/O (Java)
- Binary Search Tree (C++)
- Splay- Binary search tree (Java)
- Overload Operator<< for a Binary Search Tree. Getting C.E. C3861 (C++)
- Output Binary Search Tree to file help (C++)
- binary search tree (C)
- Binary Search Tree (Java)
- Code Snippet: Binary Search Tree Template (C)
- recursive findaverage function for Binary search tree (C)
Other Threads in the C++ Forum
- Previous Thread: Help needed with Classes
- Next Thread: Delete the last line after finish writing to a file
Views: 434 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll dynamic encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project projects proxy python random read recursion recursive reference return sort stream string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





