Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It's assigned from me to implement a tree ADT with insert function with this definition
Insert (int val, char loc, int otherVal):
,where the val is a one of the childs if the the root is not null, location is either left or right , the other val is for the child of the val and it will be the opposite of the loc,The template class node should have data integer, Left child pointer and right child pointer and I implemented them, my problem is while writting the insert function in the BTS class it bring me this error:
Exception thrown: read access violation.
**this** was nullptr.


What I have tried:

<pre>void Tree::Insert(int val, char c, int OtherVal)
{
	if (Root->getdata()== NULL)
	{
		Root->setdata(val);
		Node* new5 = new Node();
		if (c == 'L')
		{
			Root->setright(new5);
		}
		else if (c == 'R')
		{
			Root->setleft(new5);
		}
	}
	else
	{
		Node* new1 = new Node();
		Node* new2 = new Node();
		new1->setdata(val);
		new2->setdata(OtherVal);
		if (c == 'L')
		{
			Root->setleft(new1);
			new1->setright(new2);
		}
		else if (c == 'R')
		{
			Root->setright(new1);
			new1->setleft(new2);
		}


	}
}


the node class:
#include "Node.h"
Node::Node()
{
	data = 0;
	left = right = nullptr;
}

Node::Node(int val)
{
	data = val;
	left = right = nullptr;
}


void Node::setdata(int d)
{
	data = d;
}
int Node::getdata()
{
	
	return data;
}
void Node::setleft(Node* p)
{
	left = p;
}
Node*& Node::getleft()
{
	return left;
}
void Node::setright(Node* p)
{
	right = p;
}
Node*& Node::getright()
{
	return right;
}



Search Function:
Node* Tree::SearchMax()
{
	return FindMax(Root);
}

Node* Tree::FindMax(Node* subroot)
{
	if (subroot == nullptr) return subroot;
	Node* temp = subroot;
	Node* lmax = FindMax(temp->getleft());
	Node* rmax = FindMax(temp->getright());
	if (lmax->getdata() > temp->getdata())
	{
		temp = lmax;
	}
	if (rmax->getdata() > temp->getdata())
	{
		temp = rmax;
	}
	return temp;
}

Print Function:
void Tree::print()
{
	return printPreorder(Root);
}

void Tree::printPreorder(Node*subroot)
{
	if (subroot != nullptr)
	{
		cout << Root->getdata() << " ";
		printPreorder(subroot->getleft());
		printPreorder(subroot->getright());
	}
	
}

int main()
{
	Tree t;
	t.Insert(5, 'L', 20);
	t.print();
	return 0;
}
Posted
Updated 31-Dec-20 8:44am
v3
Comments
Richard MacCutchan 30-Dec-20 12:03pm    
I have deleted your other question as it is just a copy of this.
Rick York 30-Dec-20 13:08pm    
new2->setleft(new2);

That new item sets its left pointer to itself. Is that line correct? The other new item sets its right pointer to that new item. In other words, it does this :

new1->setright(new2);
Member 14771149 31-Dec-20 8:35am    
Yes I rewrite it but it still give me the same error

1 solution

You have already posted this question. If you need to add information then please edit your original.

However, the above error is telling you that you do not have a main entry point in your program. The structure of a C++ console application should be:
C++
#include statements

// static helper functions and other subroutines

// class definitions

int main(int argc, char* argv[])
{
    // code to run the above classes and functions

    return 0;
}
 
Share this answer
 
v4
Comments
CPallini 30-Dec-20 11:47am    
5 again. :-)
Richard MacCutchan 30-Dec-20 11:50am    
I can't hide from you. :))
Member 14771149 31-Dec-20 8:38am    
I have a main function in the .cpp file and it still gave me the same error, and sorry for repeating my question
Richard MacCutchan 31-Dec-20 8:45am    
Please use the Improve question link above, and post the exact code that you are using. We cannot help you unless we see the code.
Member 14771149 31-Dec-20 8:53am    
okay done

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900