a problem with the memory leaking

The following code shows the binary search tree:
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
#include <iostream>
#include <windows.h>
using namespace std;

struct node
{
int key;
struct node*left;
struct node*right;
};

class BinarySearch
{
public:
BinarySearch();

void insert(int k);
void search(int a);

~BinarySearch()
{}
private:
node*proot;
};
BinarySearch::BinarySearch()
{proot=NULL;}

void BinarySearch::insert(int k)
{
if(!proot)
{
proot=new node;
proot->key=k;
return;
}
node*p=proot;
while(p)
{
if(k>p->key)
{
if(p->right==NULL)
{
p->right=new node;
p->right->key=k;

cout<<"insert element"<<k<<""<<endl;
break;
}//enif
p=p->right;
}//endif
else if(k< p->key)
{
if(p->left==NULL)
{
p->left=new node;
p->left->key=k;

cout<<"insert element"<<k<<""<<endl;
break;
}//enif
p=p->left;
}
else break;
}//endwhile
Sleep(1000);
}
void BinarySearch::search(int a)
{
node*p=new node;
p=proot;
if(a==p->key)
{
cout<<"find out the element a"<<endl;
return;
}
while(p!=NULL)
{
if(a>p->key)
{p=p->right;
}
else if(a<p->key)
{p=p->left;
}
else
cout<<"find out the element"<<a<<"";

break;
}
Sleep(600);
}

int main()
{
    BinarySearch bisearch;
    bisearch.insert(4);
    bisearch.insert(5);
    bisearch.insert(6);
    bisearch.insert(2);
    bisearch.insert(13);
    bisearch.search(5);
    
 system("pause");
 return 0;   
}


the compiler throws out exception at line 39:if(k>p->key)
and line 51.
Its should be kept in mind that struct/class members should be initialized properly rather than left to the compiler to decide.

so, you have to add:
1
2
proot->right = NULL;
proot->left = NULL;


at various places like after line 33, 44, 56 etc..
Last edited on
indent your code.

1
2
node*p=new node;
p=proot; //memory leak 
This sort of thing:
1
2
3
4
5
while(p!=NULL)
{
    // ...
    break;
}

doesn't make a whole lot of sense. If you break at the end without any condition, then the while statement really turns into an if statement. That is equivalent to:
1
2
3
4
if (p != NULL)
{
    // ...
}
You use a few new keywords. If you do that, you need to add delete keywords. This is probably most appropriate in the destructor.

1
2
3
4
5
6
7
8
9
BinarySearch::~BinarySearch()
{
    while (proot)
    {
        node* temp = proot->right();        
        delete proot;
        proot = temp;
    }
}
thanks, I initialized it by add proot->right = NULL;
proot->left = NULL;
post your updated code if the problem is not fixed by including comments from everyone. We can look further.
I think I would have initialized the structure like so:
1
2
3
4
5
6
7
8
struct node
{
  int key;
  node* left;
  node* right;

  node() : left(this), right(this)  {  }
};
this one looks better:
1
2
3
4
5
6
7
8
struct node
{
  int key;
  node* left;
  node* right;

  node() : left(this), right(this)  {  }
};
Topic archived. No new replies allowed.