1>c:\users...\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

#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

#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

#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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.