Insertion error

Im having trouble with a program to write binary trees and binary search trees.I can print the binary trees but not the binary search. I believe it has something to do with my insertion code because when looking at code to post the height and number of nodes,it doesnt show anything so i figure my problem is the insertion.I know i posted this code alot but im getting closer to the error.

main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "BTree.h"
#include "BSTree.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
main()
{
      BTree<int> BT;
      BSTree<int>BST;
      //Queue<int>q;
      //Stack<int>s;
      
      int x;
      int btcount;
      int btheight;
      int bstheight;
      int bstcount;
      int i;
      for(i=1;i<=5; i++)
      {
              x=rand()%100;
              BT.Insert(x);
              BST.Insert(x);
              //q.Insert(x);
             // s.Insert(x);
      }
      cout<<"Random Binary Tree: "<<endl;
      cout<<"Inorder"<<endl;
      BT.InOrder(cout);
      cout<<endl;
       cout<<"Preorder"<<endl;
      BT.PreOrder(cout);
      cout<<endl;
      cout<<"Postorder"<<endl;
      BT.PostOrder(cout);
      cout<<endl;
      btheight=BT.Height();
      cout<<"height is "<<btheight<<endl;
      btcount=BT.Count();
      cout<<"count is "<<btcount<<endl;
      //cout<<"BFS"<<endl;
      //q.BFS(cout);
//      cout<<endl;
//      cout<<"DFS"<<endl;
//      s.DFS(cout);
//      cout<<endl;
      cout<<"Binary Search tree: "<<endl;
       cout<<"Inorder"<<endl;
      BST.InOrder(cout);
      cout<<endl;
       cout<<"Postorder"<<endl;
      BST.PostOrder(cout);
      cout<<endl;
       cout<<"Preorder"<<endl;
      BST.PreOrder(cout);
      cout<<endl;
      bstheight=BST.Height();
      cout<<"height is "<<bstheight<<endl;
      bstcount=BST.Count();
      cout<<"count is "<<bstcount<<endl;
      x=46;
      cout<<"Search: "<<x<<endl;
      if(BST.Search(x))
      {
         cout<<"Found"<<endl;
      }
      else
      {
          cout<<"Not found"<<endl;
       }

      
      cout<<"Delete: "<<x<<endl;
      BST.Delete(x);
      BST.InOrder(cout);
      cout<<endl;
      BST.PreOrder(cout);
      cout<<endl;
      

     char quit;
     cin>>quit;
     return 0;
}


Binary search implementation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef BSTree_H
#define BSTree_H
#include "BTree.h"
#include<iostream>
#include<stdlib.h>
using namespace std;
template <class ItemType>
class BSTree: public BTree<ItemType>
{
      private:
               typedef NodeType<ItemType> * TreePtr;
               TreePtr Tree;
               void R_Insert(TreePtr &, ItemType x, int=0, int=0);
               bool R_Search(TreePtr, ItemType);
               bool R_Delete(TreePtr &,ItemType);
   public:
    BSTree(){Tree = NULL;};
    virtual void Insert(ItemType x){R_Insert(Tree, x);};
    bool Search(ItemType x){return R_Search(Tree, x);};
    bool Delete(ItemType x){return R_Delete(Tree, x);};
    ~BSTree();
};
template<class ItemType>
bool BSTree<ItemType>::R_Search(TreePtr t,ItemType x)
{
     if(t==NULL)
         return 0;
     else if(t->item==x)
          return 1;
     else if(x<t->item)
          return R_Search(t->Lt,x);
     else
             return R_Search(t->Rt,x);
}

template<class ItemType>
void BSTree<ItemType>::R_Insert(TreePtr &t,ItemType x, int lev,int bal)
{
     if(t==NULL)
                t= new NodeType<ItemType>(x,lev);
     else if(x<t->item)
          R_Insert(t->Lt,x,lev+1,bal);
     else
             R_Insert(t->Rt,x,lev+1,bal);
}
template<class ItemType>
bool BSTree<ItemType>::R_Delete(TreePtr &t,ItemType x)
{
     TreePtr delnode,parent,child;
     if(t==NULL)
      return 0;
     else if(x<t->item)
       return R_Delete(t->Lt,x);
     else if(x>t->item)
       return R_Delete(t->Rt,x);
     else
     {
         if((t->Lt==NULL)&&(t->Rt==NULL))
         {
            delete t;
            t=NULL;
         }
         else if(t->Lt==NULL)
         {
              delnode=t;
              t=t->Rt;
              delete delnode;
         }   
         else if(t->Rt==NULL)
         {
              delnode=t;
              t=t->Lt;
              delete delnode;
         } 
         else
         {
             parent=t;
             child=t->Rt;
             if(child->Lt==NULL)
             {
                parent->item=child->item;
                parent->Rt=child->Rt;
                delete child;
                return 0;
             }
             else
             {
                 while(child->Lt!=NULL)
                 {
                    parent=child;
                    child=child->Lt;
                    t->item=child->item;
                    parent->Lt=child->Lt;
                    delete child;
                    return 1;
                  }
             }
         }
     }
}
template<class ItemType>
BSTree<ItemType>::~BSTree()
{
TreePtr t;
delete t;
//BTree<ItemType>::~BTree();
}
//BSTree<int>BST;
#endif  


ignore the commented code,not relevant to my problem. Any help is appreciated.
Holy sh*t! Can't you spot the code segment which is not working? Use DDD (or any sort of debugger)... or if that's way too complicated, fill the code of couts and see where things start to go wrong. Then, if you still don't know what's going on, post here.

I'd like to help, but reading 200 code lines is way too much!
I ran the debugger and stopped it after my r_insert

i got as bugs

t->Rt=?
t->Rt=?
x=?
else=?
ItemType=?

can someone help me on how i can fix?
please someone help me,i am a horrible programmer and need help.
Topic archived. No new replies allowed.