BST not printing

Hi again guys, I am trying to print out some items that I inserted into my Tree, but for some strange reason nothing is printing. . .Here's my code:

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

using namespace std;

struct node
{
	int info;
	node *left;
	node *right;
};

class myTree
{
	public:
		myTree()
		{
			root = NULL;
		}
		void PutItem(int item, node *leaf)
		{
			if(item < leaf->info)
			{
				if(leaf->left != NULL)
					PutItem(item, leaf->left);
				else
				{
					leaf->left = new node;
					leaf->left->info = item;
					leaf->left->left = NULL;
					leaf->left->right = NULL;
				}
			}
			else if(item >= leaf->info)
			{
				if(leaf->right != NULL)
					PutItem(item, leaf->right);
				else
				{
					leaf->right = new node;
					leaf->right->info = item;
					leaf->right->right = NULL;
					leaf->right->left = NULL;
				}
			}
		}

		void PutItem(int item)
		{
			if(root != NULL)
				PutItem(item, root);
			else
			{
				root = new node;
				root->info = item;
				root->left = NULL;
				root->right = NULL;
			}
		}

		void PrintTree(node *leaf)
		{
			if(leaf != NULL)
			{
				PrintTree(leaf->left);
				cout << leaf->info << "  ";
				PrintTree(leaf->right);
			}
		}
	private:
		node *root;
};
int main()
{
	myTree tree;
	node *btree = NULL;

	tree.PutItem(10);
	tree.PutItem(22);
	tree.PutItem(24);
	tree.PutItem(25);
	tree.PutItem(26);
	tree.PutItem(29);
	tree.PutItem(52);

	tree.PrintTree(btree);

	///I know this is bad, but this is just for practice 
	system("pause");
	return 0;
}
i haven't read all your code, but i just read the flow.
1
2
3
4
5
6
7
8
9
		void PrintTree(node *leaf)
		{
			if(leaf != NULL)
			{
				PrintTree(leaf->left);
				cout << leaf->info << "  ";
				PrintTree(leaf->right);
			}
		}


and this:
 
node *btree = NULL;


and this:
 
tree.PrintTree(btree);


does any condition will print the btree (because it's NULL)?
For simplicity, I'm going to unwind your program a bit for you:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	myTree tree;
	node *btree = NULL;

	// ...

	tree.PrintTree(btree);

	// The first line in PrintTree

	if(leaf != NULL)


I hope that makes it a little clearer
I did not think about that.

But I get an error saying that btree isn't initialized if I don't set it to NULL. I'm still fairly new to C++, so sorry about the basic questions. Thanks for the help guys!
you can make your addNode function easier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void myTree :: PutItem( int item ){
       PutItem(root , item);
}

//Another function outside
void PutItem( node* tree , int item ){
      if( tree == NULL ){
            tree = new node;
            tree->left = NULL;
            tree->right = NULL;
            tree->item = item;
      }
      else if( item < tree->item ){
            PutItem( tree->left , item );
      }
      else
            PutItem( tree->right , item );
}


And do you know for your display function.
U cna display with preorder inorder and postorder traversal?
do you know all of these?

and these function are using recursive . do you know what is recursive function about?
Last edited on
I wanted to practice using recursion using trees. I am aware of the pre, in, and postrder traversal, it's just I keep having problems with display them to the screen.

Also thanks for the AddNode function! It IS much easier!
OK so I have recently changed my code to the following:

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

using namespace std;

struct node
{
	int info;
	node *left;
	node *right;
};

class myTree
{
	public:
		myTree()
		{
			root = NULL;
		}
		void PutItem(int item, node *tree)
		{
			if(tree = NULL)
			{
				tree = new node;
				tree->info = item;
				tree->left = NULL;
				tree->right = NULL;
			}
			
			else if(item < tree->info)
				PutItem(item, tree->left);
			
			else if(item >= tree->info)
				PutItem(item, tree->right);
		}

		void PutItem(int item)
		{
			PutItem(item, root);
		}
		void PrintTree(node *tree)
		{
			if(tree != NULL)
			{
				PrintTree(tree->left);
				cout << tree->info << "  ";
				PrintTree(tree->right);
			}
		}
		void PrintTree()
		{
			PrintTree(root);
		}
	private:
		node *root;
};
int main()
{
	myTree tree;

	tree.PutItem(10);
	tree.PutItem(22);
	tree.PutItem(24);
	tree.PutItem(25);
	tree.PutItem(26);
	tree.PutItem(29);
	tree.PutItem(52);

	tree.PrintTree();
	///I know this is bad, but this is just for practice 
	system("pause");
	return 0;
}


I am still unable to print, and it seems as though my program crashes when I try to run it :(
1
2
3
4
5
			else if(item < tree->info)
				PutItem(item, tree->left);
			
			else if(item >= tree->info)
				PutItem(item, tree->right);


What happens when your tree contains elements 3 and 5 and you try to add 4?

Or if tree is NULL, you don't actually add it to anything...
Last edited on
Thanks Volatile, I kept looking at my Print function. I didn't think to completely go over my add function again. C++ is definitely a challenge (a good challenge). Thanks again!
The language itself isn't. Logically thinking about taking what you know and translating it into code is typically the hard part. A lot of programmers start off by learning how to convert plain english, the thoughts that run through your head (or, if you're learning to be a programmer or a good programmer, the words you speak to yourself), write up pseudo code, then translate pseudo code into C++. Pseudo code is essentially a mix between all programming languages and english (or whatever language you write in). It allows you to, depending on your skills with pseudo code, to simply write out each line into C++ and watch the flow of the thoughts that you had turn into code. It's quite magnificent if you ask me.

I wish I could help you a little bit more with this code specifically, but I've never done a tree before (surprisingly, I know), but one of these days I'll look at it. I believe it works just like a list, but instead of going front to end, a tree starts in the middle and moves out, adding larger elements to the right, smaller ones to the left. I don't like the idea behind it, because, as you've noticed, it's not the easiest thing to implement.

Good luck, and if I see anything else I can help you with, I'll be sure to try.
Topic archived. No new replies allowed.