Segmentation fault in my Program. Please help me out.

I want to print out the binary search tree like a tree graph. After running my program, I got segmentation fault. Please help me check if or not the code has some problems.
Below is my code, it can be compiled on Linux.
But the last output always is segmentation fault.
Please some experts here help me out about the error. Thanks a lot.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include "binarysearchtree.h"
using namespace std;
int main()
{
	BinarySearchTree tree;
	tree.insert_node(10);
	tree.insert_node(12);
	tree.insert_node(11);
	tree.print();
	return 0;
}

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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
using namespace std;

class BinarySearchTree;

class Node{
	public:
	Node(int number, Node* left, Node* right){
		m_number = number;
		m_left = left;
		m_right = right;
	}
	Node *m_left;
	Node *m_right;
	int m_number;
	friend class BinarySearchTree;
};

class BinarySearchTree{
	public:
		BinarySearchTree();
		~BinarySearchTree();
		void insert_node(int number);
		int find_height();
		void print();
	private:
		void print_space(int count);
		int max(int a, int b);
		void insert_node(int number, Node*& node);	
		int find_height(Node*& node);
		void print_tree_graph(vector<Node*> &node, int height, int level);
		Node* m_root;
};

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
110
111
112
113
114
115
116
117
118
#include "binarysearchtree.h"

BinarySearchTree::BinarySearchTree()
{
	m_root = NULL;
}

BinarySearchTree::~BinarySearchTree()
{
	
}

void BinarySearchTree::insert_node(int number)
{
	insert_node(number, m_root);
}

void BinarySearchTree::insert_node(int number, Node*& node)
{
	if(node==NULL)
	{
		node = new Node(number, NULL, NULL);
	}
	else if( number < node->m_number){
		insert_node( number,node->m_left );
	}
	else if(number>node->m_number){
		insert_node(number, node->m_right);
	}
}

void BinarySearchTree::print()
{
	vector<Node*> myvector;
	int height = find_height(m_root);
	if(m_root!=NULL)
	{
		myvector.push_back(m_root);
		print_tree_graph(myvector, height, 1);
	}
}

void BinarySearchTree::print_tree_graph(vector<Node*> &node, int height, int level)
{	
	int floor = height-level;
	int edge_line = pow(2, max((floor-1), 0));
	int first_space = pow (2, floor) -1;
	int between_space = pow(2, (floor+1))-1;
	print_space(first_space);
	vector<Node*> myvector;
	for (unsigned int i=0; i<node.size(); i++) 
	{
		if(node.at(i)!=NULL)
		{
			cout<<node.at(i)->m_number;
			myvector.push_back(node.at(i)->m_left);
			myvector.push_back(node.at(i)->m_right);	
		}else{
			myvector.push_back(NULL);
			myvector.push_back(NULL);
			print_space(1);
		}
		print_space(between_space);
	}
	cout<<"myvector: " <<myvector.size()<<endl;
	for(unsigned int i=0; i<myvector.size(); i++)
		cout<<myvector[i]->m_number <<" ";
	cout<<"\n";
	for(int i=1; i<=edge_line; i++)
	{
		for(unsigned int j=0; j< node.size(); j++)
		{
			print_space(first_space-i);
			if(node[j]==NULL)
				print_space(edge_line+edge_line+1);
			if(node[j]->m_left!=NULL)
				cout<<"/";
			else
				print_space(1);
			print_space(i + i -1);
			if(node[j]->m_right!=NULL)
				cout<<"\\";
			else
				print_space(1);
			print_space(edge_line + edge_line -i);
		}
		cout<<"\n";
	}
	level++;
	print_tree_graph(myvector, height, level);
	myvector.clear();
	node.clear();
}


void BinarySearchTree::print_space(int count)
{
	for(int i=0; i<count; i++)
		cout<<" ";
}

int BinarySearchTree::find_height()
{
	return find_height(m_root);
}

int BinarySearchTree::find_height(Node*&  node)
{
	if(node == NULL)
		return 0;
	else
		return max( find_height(node ->m_left) +1, find_height(node->m_right)+1);
}

int BinarySearchTree::max(int a, int b)
{
	return (a >= b ? a : b);
}
In BinarySearchTree::print_tree_graph(vector<Node*> &node, int height, int level),
at the code
cout<<myvector[i]->m_number <<" ";

myvector[0] is a null pointer. You are trying to dereference a null pointer.
Topic archived. No new replies allowed.