preorder of binary search tree

hi guys,
can you help me with my code I have a problem with my function it is not print the number in pre-order.
please i need a quick help because i have one hour to send the assignment
my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void preOrder(BSTNode* root)
{
	queue<BSTNode*> Pre;
	Pre.push(root);
	while(!Pre.empty())
	{
		BSTNode *root = Pre.front();
		Pre.pop();
		if(root!= NULL)
		{
			
			Pre.push(root->left);
			Pre.push(root->right);
			cout << root->value << " ";
		}
		
	}
	
}


it spouse to print the number like this
54 22 17 41 36 30 26 27 45 74 76 82
but it print
54 22 47 17 41 76 36 45 82 30 26 27

please guys I want some help before send the assignment
pls
Instead of a queue, try using a stack. Then, at each visit, push the right child then the left child.
sorry oky I used the stack but the problem still exist
Last edited on
here is my new one
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void preOrder(BSTNode* root)
{
	stack<BSTNode*> Pre; // initialize the pre-order with the queue
	Pre.push(root);
	while(!Pre.empty()) // check if it is not empty 
	{
		BSTNode *root = Pre.push();
		Pre.pop();
		if(root!= NULL) // check if the root not pointing to NULL
		{
			
			Pre.push(root->left);
			Pre.push(root->right);
			cout << root->value << " ";// print the number for pre-order
		}
		
	}
	
}
can you help i have 20 min please
With a stack, push the right then the left.
like this
1
2
3
4
5
6
7
if(root!= NULL) // check if the root not pointing to NULL
		{
			Pre.push(root->right);
			Pre.push(root->left);
			
			cout << root->value << " ";// print the number for pre-order
		}

if you mean this
but I have error in this line
 
BSTNode *root = Pre.push();
1
2
BSTNode *root = Pre.top();
Pre.pop();
look what can i say to you I thank you so much man
I really was in trouble

thank in advance
Topic archived. No new replies allowed.