Debug Assertion Failed! (Binary Search tree)

I've asked the same Question here http://stackoverflow.com/questions/34422852/debug-assertion-failed-binary-search-tree

IM IN DESPERATE NEED OF HELP.

Im working on a project, What I want is to get data from a database (MySql) and store it in a Tree. And be able to do insertion, deletion or updates on the nodes, if any of these commands are performed on a tree then changes must reflect on the database as well. (via Windows Form)
**UPDATED**
This error shows up when I do the search Via search button

Debug Assertion Failed!

here's my tree code:
just showing the insertion module.

> Btree.cpp

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
#include "Btree.h"
        
        Btree::Btree()
        {
        	root = NULL;
        	rootnum = NULL;
        }
        
        Btree::~Btree(){
        	delete root;
        }
        bool Btree::isEmpty() const { 
        	return root == NULL; 
        }
        bool Btree::isnumEmpty() const {
        	return rootnum == NULL;
        }
        string Btree::treeSearch(string name){
  
        	bool found = false;
        	if (isEmpty())
        	{
        		MessageBox::Show( " This Tree is empty! " );
                        return 0;
        	}
        
        	node* curr;
        	node* parent;
        	curr = root;
        
        	while (curr != NULL)
        	{
        		if (curr->strname == name)
        		{
        			found = true;
        			return name;
        			break;
        		}
        		else
        		{
        			parent = curr;
        			if (name>curr->strname) curr = curr->right;
        			else curr = curr->left;
        		}
        	}
        	if (!found)
        	{
        		MessageBox::Show(" Data not found! ");
                        return 0;
        	}
        }
        
        void Btree::insert(string d)
        {
        	node* t = new node;
        	node* parent;
        	t->strname = d;
        	t->left = NULL;
        	t->right = NULL;
        	parent = NULL;
        	MessageBox::Show(" pointer is in insert ");
        	// is this a new tree?
        	if (isEmpty()){
        		root = t;
        		MessageBox::Show(" Root sval chngd ");
        }
        	else
        	{
        		MessageBox::Show(" storing val ");
        		//Note: ALL insertions are as leaf nodes
        		node* curr;
        		curr = root;
        		// Find the Node's parent
        		while (curr)
        		{
        			parent = curr;
        			if (t->strname > curr->strname) curr = curr->right;
        			else curr = curr->left;
        		}
        
        		if (t->strname < parent->strname)
        			parent->left = t;
        		else
        			parent->right = t;
        	}
        }




> Btree.h

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
 #include <iostream>
    #include <string>
    
    using namespace std;
    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
    
    #ifndef Btree_H
    #define Btree_H
    class Btree{
    
    private:
    	typedef struct node{
    		string strname;
    		string strnum;
    		node* left;
    		node* right;
    		node* Lnum;
    		node* Rnum;
    	};
    	node* root;
    	node* rootnum;
    public:
    	//constructor
    	Btree();
    	~Btree();
    public:	
    		void insert(string);
    		void remove(string);
    		string treeSearch(string);
    	//void insertnum(string);
    		bool isEmpty() const;
    		bool isnumEmpty() const;
    };
    
    
    
    #endif 


> my Form's coding FOR **INSERTION**:


1
2
3
4
5
6
7
 private: System::Void FetchData_Click(System::Object^  sender, System::EventArgs^  e) {
    Btree t;
    //String^ a;
    String^ c = "Syed";
    std::string ststring = msclr::interop::marshal_as<std::string>(c);
    t.insert(ststring);
    }


> For Search Button coding:

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
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    	String^ name = namep->Text;
    	//String^ st = namep->Text;
    	Btree t;
    	//conversion
    	std::string ststring = msclr::interop::marshal_as<std::string>(name);
    	string iftrue = t.treeSearch(ststring);
    	//string iftrue = "Syed";
    	String^ str2 = gcnew String(iftrue.c_str());
    	if (str2 != "n"){
    		String^ constring = L"datasource=localhost;port=3306;username=root;password=bu123";
    		MySqlConnection^ conDataBase = gcnew MySqlConnection(constring);
    		MySqlCommand^ cmdDataBase = gcnew MySqlCommand("select * from telephone.phonebook where PName = '" + str2 + "';", conDataBase);
    		MySqlDataReader^ myReader;
    		try{
    			conDataBase->Open();
    			myReader = cmdDataBase->ExecuteReader();
    			if (myReader->Read()){
    				String^ phone = myReader->GetString("PhoneNumber");
    				PN->Text = phone;
    				String^ phone1 = myReader->GetString("PhoneNumber1");
    				PN1->Text = phone1;
    				String^ phone2 = myReader->GetString("PhoneNumber2");
    				PN2->Text = phone2;
    				String^ phonety = myReader->GetString("PhoneNumberType");
    				pt->Text = phonety;
    				String^ phonety1 = myReader->GetString("PhoneNumberType1");
    				pt1->Text = phonety1;
    				String^ phonety2 = myReader->GetString("PhoneNumberType2");
    				pt2->Text = phonety2;
    				String^ cat = myReader->GetString("Category");
    				category->Text = cat;
    				String^ Gender = myReader->GetString("Gender");
    				gender->Text = Gender;
    				String^ Email = myReader->GetString("Email");
    				email->Text = Email;
    				String^ Adres = myReader->GetString("Address");
    				address->Text = Adres;
    				String^ ct = myReader->GetString("City");
    				city->Text = ct;
    			}
    			else if (!myReader->Read()){
    				MessageBox::Show("No such Name found!");
    			}
    
    		}
    		catch (Exception^ex){
    			MessageBox::Show(ex->Message);
    		}
    	}
    	else 
    		MessageBox::Show("No record");
    }
Last edited on
closed account (1vD3vCM9)
Setting MySQL On windows took me 4 hours and i still failed with it, i just gave up with it.
You may try to use a differnet database.. such as SQLite.
My problem is not with the MYSQL, its working perfectly fine in my program.

My problem is with tree implementation and its driving me nuts, Debug Assertion Failed! is the Error.
Last edited on
Have you tried using the debugger? Inspecting the call stack would let you pinpoint where the assertion is failed.

One thing that stands out is that Btree::TreeSearch does not always return a value, although it promises to. Hello, undefined behavior.
Last edited on
Nope, i didn't. (bcz i really dont know how) Im just a Noob+


search func does always return a value i have removed it with this MessageBox::Show just to know that whether my program is reaching at that point or not.

it returns a string falseCondi which I have initialiez as falseCondi="b".

but still it gives me this Error.
Last edited on
search func does always return a value i have removed it with this MessageBox::Show just to know that whether my program is reaching at that point or not.

In the code you have supplied, treeSearch does not always return a value. If you're showing us significantly different code than you're using, how are we supposed to make intelligent observations about it?
Last edited on
This line looks a bit suspious to me. Where did you get it?
 
std::string ststring = msclr::interop::marshal_as<std::string>(c);


Maybe you can test it in a separate program.
 
In the code you have supplied, treeSearch does not always return a value. If you're showing us significantly different code than you're using, how are we supposed to make intelligent observations about it?


I have Edited the code.
1
2
3
4
5
6
This line looks a bit suspious to me. Where did you get it?
 
std::string ststring = msclr::interop::marshal_as<std::string>(c);


Maybe you can test it in a separate program.


its conversion

https://msdn.microsoft.com/en-us/library/bb384865.aspx


taken from here
http://stackoverflow.com/questions/946813/c-cli-converting-from-systemstring-to-stdstring
You should stick with one form of strings. Sure, not related with your problem, but why mix 2 different programming language together in the same application ?
@paragon,

thanks I didn't know that the conversion is so simple. All the other examples I have seen were more complicated.

@modoran
but why mix 2 different programming language together in the same application ?


How else would you like to build a GUI? MFC, Qt, wxWidgets have all their own string types and you need to convert your normal std::strings.
You should stick with one form of strings. Sure, not related with your problem, but why mix 2 different programming language together in the same application ?

1. That's the the problem I'm sure of it, because i have tested passing the converted variable into a a standard string variable (in the search method) and its working perfectly, but when i do it with nodes it becomes pain in the a** Debug Assertion Failed!

2. Because it was my project requirement.
1
2
3
@paragon,

thanks I didn't know that the conversion is so simple. All the other examples I have seen were more complicated. 


My Pleasure.
Why don't you split your program in two parts - only for testing ?

One part could be a program that creates a few CLR strings, converts them to std::strings, insert, removes and searches the tree.

The other program would be a CLR program that just reads the data from the DB and prints it on the screen.

It might me more evident where the problem is.

Already did ^
that's why i need some experienced person to solve this, because I cannot find the cause.
In what part does it actually crash?

I did a little experiment and couldn't find a problem with your tree part.
Since I used VS 2005 I could not use msclr::interop::marshal_as<std::string>(c);
and had to use a different conversion method. If you want to try it...

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
#include <iostream>
#include <string>
#include <stdlib.h>
#include "btree.h"

using namespace std;
using namespace System;
using namespace System::Runtime::InteropServices; 

#define TRACE(...) { printf(__VA_ARGS__); }

string ClrToStd(String^ in)
{
  IntPtr ptr = Marshal::StringToHGlobalAnsi(in->ToString());
  string str = reinterpret_cast<char*>(static_cast<void*>(ptr));
  Marshal::FreeHGlobal(ptr);                   
  return str;
}

void main(void)
{
  char *names[] = {"Anna", "Lisa", "Tammy", "Ellie", "Debbie"};

  Btree tree;

  TRACE("** Testing insert into tree **\n\n");

  for (int i = 0; i < 5; i++)
  {
    TRACE("Creating CLR string with the name %s\n", names[i]);
    String^ n = gcnew String(names[i]);
    TRACE("Converting CLR string to std string\n");
    string s = ClrToStd(n);
    TRACE("Going to insert name %s into tree.\n", s.c_str());
    tree.insert(s);
  }

  TRACE("\n** Testing search for a name in tree **\n\n");
  for (int i = 0; i < 5; i++)
  {
    TRACE("Search for %s\n", names[i]);
    string search = tree.treeSearch(names[i]);  
    TRACE("Search for %s resulted in %s **\n", names[i], search.c_str());
  }
  system("pause");  
}
The problem is of course this:
1
2
3
4
5
        	if (!found)
        	{
        		MessageBox::Show(" Data not found! ");
                        return 0;
        	}


See:
http://www.cplusplus.com/reference/string/string/string/

The only constructor that takes one parameter is the one that takes a pointer to a c-string. In your case you pass 0 as a pointer which will crash. Return an empty string instead.
coder777 wrote:
The problem is of course this:


Except, according to the OP that code is not representative of the actual code. He said this when I mentioned the original code didn't always return a value:

search func does always return a value i have removed it with this MessageBox::Show just to know that whether my program is reaching at that point or not.

it returns a string falseCondi which I have initialiez as falseCondi="b".


and edited the code to what you quoted... which clearly differs from his claim. So, if he's unwilling to use a debugger (which will point him directly to the offending line), and he's unwilling to post code that isn't significantly different than what he's actually using, there isn't much that can be done for him on these forums.
Well, I should have read all the posts...
Topic archived. No new replies allowed.