#include <iostream>
#include <queue>
struct Node
{
public:
Node* lchild;
int data;
Node* rchild;
}*root = NULL;
void createB()
{
int x;
Node* t, * p = new Node;
std::queue<int*> q;
std::cout << "Enter the value of root : ";
std::cin >> x;
Node *root = new Node;
root->data = x;
root->lchild = root->rchild = NULL;
q.push(&root->data);
while (!q.empty())
{
p->data = *q.front();
q.pop();
std::cout << "Enter Left Child of " << p->data << " : ";
std::cin >> x;
if (x != -1)
{
t = new Node;
t->data = x;
t->lchild = t->rchild = NULL;
p->lchild = t;
q.push(&t->data);
}
std::cout << "Enter Right Child " << p->data << " : ";
std::cin >> x;
if (x != -1)
{
t = new Node;
t->data = x;
t->lchild = t->rchild = NULL;
p->rchild = t;
q.push(&t->data);
}
}
}
void preorder(Node* p)
{
while(p)
{
std::cout << p->data << ' ';
p->lchild;
p->rchild;
}
}
int main()
{
createB();
preorder(root);
}
#Preorder function not displaying
1 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
p->lchild;
p->rchild; wha!??1
why should it run? it's your job to explain why you think your code should work.. so we can see where you are going wrong.
The createB() function would create Tree
Then after creating the tree I need to display it using preorder function
don't cross post 
when can kinda see 'what' it's supposed to do.. its just a loop and cout. so now answer 'why'. comment every line of code with what you think it's going to do
(every == start with the ones that arent' doing what you want)
I guess you deleted the other one now...
Node *root = new Node;
you are hiding the outer root variable here
so your root is nullptr when you do the print
yes sorry bout cross posting
btw use nullptr instead of NULL for pointers
!solved