Prinintg Binary search tree more details in description

When i step into my Insert method it does what it should, but once i call my preOrder function it only prints the last integer from the file. I tried using '&' thinking it was a pass by reference issue but did not work.
Also i used a node*temp to fill out the tree so that the node*root would stay the same(i.e. just like using head node and a iter node in a linked list ) but i still got the same issue.

in the file - {17 20 9 17 5}

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
  #include<iostream>
#include<fstream>

#define ifile "read.txt"
#define ofile "out.txt"

using namespace std;

struct node {
	int data;
	node*left;
	node*right;
};
node*root;
void insert(int, node*);
void preOrder(node*);
void preOrder(node*root) {
	if (root == NULL) { return; }
	cout<<root->data<<" ";//just prints 5
	preOrder(root->left);
	preOrder(root->right);
}
void insert(int key, node*root) {
	node*temp;
	temp = root;
	if (key < temp->data) {
		if (temp->left != NULL) {
			insert(key, temp->left);
		}
		else {
			temp->left = new node;
			temp->data = key;
			temp->left = NULL;
			temp->right = NULL;
		}
	}
	else if(key >=temp->data){
		if (temp->right != NULL) {
			insert(key, temp->right);
		}
		else {
			temp->right = new node;
			temp->data = key;
			temp->left = NULL;
			temp->right = NULL;
		
		}

	}
}
int main() {
	ifstream infile;
	ofstream out;
	int key = 0;
	//root = new node;
	node*temp;
	infile.open("read.txt");
	out.open("out.txt");
	
		infile >> key;
			root = new node;
			root->data = key;
			root->left = NULL;
			root->right = NULL;
		
			while (!infile.eof()) {
				infile >> key;
				insert(key, root);

			}
	//cout << "Pre Order : ";
	preOrder(root);


	infile.close();
	out.close();
	system("pause");
	return 0;
}
When i step into my Insert method it does what it should

Wrong. Insert is 99% of the problem. It only ever saves over the root value. You need to look over this more carefully.

The other 1% of the problem, which you haven't encountered since you can't get a tree, is that you print the root before you print the left side of the tree. You need to switch the positions of lines 19 & 20.
now i am printing 15 digits but memory locations.
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
void insert(int key, node*&root) {
	node*temp;
	temp = root;
	if (key < temp->data) {
		if (temp->left != NULL) {
			insert(key, temp->left);
		}
		else {
			temp->left = new node;
			temp->data = key;
			temp->left->left = NULL;
			temp->left->right = NULL;
		}
	}
	else if(key >=temp->data){
		if (temp->right != NULL) {
			insert(key, temp->right);
		}
		else {
			temp->right = new node;
			temp->data = key;
			temp->right->left = NULL;
			temp->right->right = NULL;
		
		}

	}
}
Anyone? please what am i doing wrong?
1
2
3
4
5
6
7
int myinteger = 5;

int *intpointer = &myinteger;

std::cout << intpointer << std::endl; //prints memory address

std::cout << *intpointer << std::endl; //prints the value of the pointer that intpointer is pointing to 
The other 1% of the problem, which you haven't encountered since you can't get a tree, is that you print the root before you print the left side of the tree.

What the OP is doing is correct for pre-order traversal. Printing the left side of the tree first would be in-order traversal.

Your main program can be simplified. To read the file just do:
1
2
3
while (infile >> key) {
     insert(key, root);
}

This takes advantage of the fact that while() is expecting a bool and std::stream has a convert-to-bool operator that returns stream.good();

The problem with inset() is lines 10 and 21. THey should be
tmp->left->data = key;
and
tmp->right->data = key;
respectively because you're setting the key of the NEW node.

But all that duplicated code in insert() should make you uncomfortable. Is there a way to simplify it? Since you're passing the pointer by reference, just let insert() handle the case where the pointer is NULL. If you do that, the replicated code goes away:
1
2
3
4
5
6
7
8
9
10
11
12
void insert(int key, node * &root)
{
    if (root == NULL) {
        root = new node;
        root->left = root->right = NULL;
        root->data = key;
    } else if (key < root->data) {
        insert(key, root->left);
    } else {
        insert(key, root->right);
    }
}


Wow dhayden hat is much less code...and i just got my code to work i used this for insert:
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
void insert(int key, node*root) {
	node*temp; node*tempright;
	if (key < root->data) {
		if (root->left != NULL) {
			insert(key, root->left);
		}
		else {
			root->left = new node;
			temp = root->left;
			temp->data = key;
			temp->left = NULL;
			temp->right = NULL;
		}
	}
	else if(key >= root->data){
		if (root->right != NULL) {
			insert(key, root->right);
		}
		else {
			root->right = new node;
			tempright = root->right;
			tempright->data = key;
			tempright->left = NULL;
			tempright->right = NULL;
		
		}

	}
}

i set my root to Null before i enter my while loop, But THANK YOU i will use your way..they both work, not sure why you have to use & though if i declare node*root in global???
Topic archived. No new replies allowed.