Count recursive function acces violation

Ok, I've written myself a function that should retrieve the amount of nodes from a binary tree. I don't know why it crashes, so if someone recognises a mistake, please let me know.

Error message from debugger is Acces violation, and addresses.

Node.cpp
1
2
3
4
5
6
7
8
9
int Node::Count()
{
	int a, b;
	if(this->pLeft == NULL) a = 0; //somewhere between this point
	else a = this->pLeft->Count(); //and this point the function crashes. 
	if(this->pRight == NULL) b = 0;
	else b = this->pRight->Count();
	return a+b+1;
}

BinaryTree.cpp
1
2
3
4
5
6
void BinaryTree::Count()
{
	if(this->pRoot = NULL) this->count = 0;
	else this->count = this->pRoot->Count();
	return;
}

and declarations
Node.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#pragma once
#include "Include.h"

class Node
{
private:
	string key;
	int wordcount;
	Node* pLeft;
	Node* pRight;
protected:
public:
	Node(string x);
	virtual ~Node();
	void AddWordCount(string x);
	int GetWordCount();
	string GetKey();
	void ComputeInOrder(int x);
	bool Find(string x);
	void AddNode(string x);
	int Count();
};

BinaryTree.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#pragma once
#include "Include.h"
#include "Node.h"
class BinaryTree
{
private:
	Node* pRoot;
	int count;
	void AddNode(string x); //
	bool Find(string x); //
protected:
public:
	BinaryTree();
	virtual ~BinaryTree();
	void Input(string x); //
	void Count(); //
	int GetCount(); //
	void ComputeInOrder(); //
};
Somewhere in your tree there is an invalid pointer. Without seeing rest of the code (mainly functions which manipulates pointers) we cannot say more.
It might be because the memory at pLeft is not NULL. It might be 0xdeadbeef or 0xcdcdcdcd. Initialize pLeft and pRight in Node constructor to NULL.
adding rest of the node.cpp

I do iterate pointers in constructor.

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
#pragma once
#include "Node.h"

Node::Node(string x)
{
	this->key = x;
	this->wordcount = 1;
	this->pLeft = NULL;
	this->pRight = NULL;
	//cstr
}

Node::~Node()
{
	//dstr
}

void Node::AddWordCount(string x)
{
	if(this->key == x){ this->wordcount++; return;}
	if(this->key > x)
	{
		if(this->pLeft != NULL) this->pLeft->Find(x);
		else return;
	}
	else
	{
		if(this->pRight != NULL) this->pRight->Find(x);
		else return;
	}
	return;
}

int Node::GetWordCount()
{
	return this->wordcount;
}

void Node::ComputeInOrder(int x)
{
	if (this->pLeft != NULL)
	{
		this->pLeft->ComputeInOrder(x);
	}
	
/*	int param = this->GetWordCount();
	double param2 = (param/x);
	double entropie = (log(param2)/log(2));
	string key = this->GetKey();
	int count = this->GetWordCount();
	*/
	cout << this->GetKey() << "	| " << this->GetWordCount() << "	| "/* << entropie*/ << endl;
	/* Tady přijde výpis do souboru */
	
	if (this->pRight != NULL)
	{
		this->pRight->ComputeInOrder(x);
	}
}

string Node::GetKey()
{
	return this->key;
}

bool Node::Find(string x)
{
	if(this->key == x) return true;
	if(this->key > x)
	{
		if(this->pLeft != NULL) return this->pLeft->Find(x);
		else return false;
	}
	else
	{
		if(this->pRight != NULL) return this->pRight->Find(x);
		else return false;
	}
}
void Node::AddNode(string x)
{
	if(this->key > x)
	{
		if(this->pLeft != NULL) this->pLeft->AddNode(x);
		else
		{
			this->pLeft = new Node(x);
		}
	}
	else
	{
		if(this->pRight != NULL) this->pRight->AddNode(x);
		else
		{
			this->pRight = new Node(x);
		}
	}
}
int Node::Count()
{
	int a, b;
	if(this->pLeft == NULL) a = 0;
	else a = this->pLeft->Count();
	if(this->pRight == NULL) b = 0;
	else b = this->pRight->Count();
	return a+b+1;
}
Ok, I made it different way.
Topic archived. No new replies allowed.