Displaying binary tree elements [on hold]











up vote
-2
down vote

favorite












I am able to do store the binary tree elements using class and pointers but I ain't able to show it. I am getting the result as 1 2 but not the whole list of nodes. I am wondering at which part my logic is going wrong.



This is the code:



#include <iostream>
#include<bits/stdc++.h>
using namespace std;
class node
{
int data;
public:
node *left=NULL;
node *right=NULL;
void binarytree(node **root,int num);
void display(node *head_ref);

};
void node::binarytree(node **root,int num)
{
node *cur = new node;
{
cur->data = num;
cur->left = NULL;
cur->right = NULL;
*root = cur;
}
}
void node::display(node *head_ref)
{
if(head_ref->left!=NULL || head_ref->right!=NULL)
{
cout<<(head_ref)->data<<" ";
display(head_ref->left);
display(head_ref->right);
}

}
int main() {
node bin;
node *rt=NULL;

bin.binarytree(&rt,1);
bin.binarytree(&(rt->left),2);
bin.binarytree(&((rt->left)->left),4);
bin.binarytree(&((rt->left)->right),5);
bin.binarytree(&(rt->right),3);

bin.display(rt);

return 0;
}









share|improve this question













put on hold as off-topic by Edward, Toby Speight, papagaga, Graipher, Incomputable Nov 14 at 14:27


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Edward, Toby Speight, papagaga, Graipher, Incomputable

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 4




    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
    – Edward
    Nov 14 at 13:23















up vote
-2
down vote

favorite












I am able to do store the binary tree elements using class and pointers but I ain't able to show it. I am getting the result as 1 2 but not the whole list of nodes. I am wondering at which part my logic is going wrong.



This is the code:



#include <iostream>
#include<bits/stdc++.h>
using namespace std;
class node
{
int data;
public:
node *left=NULL;
node *right=NULL;
void binarytree(node **root,int num);
void display(node *head_ref);

};
void node::binarytree(node **root,int num)
{
node *cur = new node;
{
cur->data = num;
cur->left = NULL;
cur->right = NULL;
*root = cur;
}
}
void node::display(node *head_ref)
{
if(head_ref->left!=NULL || head_ref->right!=NULL)
{
cout<<(head_ref)->data<<" ";
display(head_ref->left);
display(head_ref->right);
}

}
int main() {
node bin;
node *rt=NULL;

bin.binarytree(&rt,1);
bin.binarytree(&(rt->left),2);
bin.binarytree(&((rt->left)->left),4);
bin.binarytree(&((rt->left)->right),5);
bin.binarytree(&(rt->right),3);

bin.display(rt);

return 0;
}









share|improve this question













put on hold as off-topic by Edward, Toby Speight, papagaga, Graipher, Incomputable Nov 14 at 14:27


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Edward, Toby Speight, papagaga, Graipher, Incomputable

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 4




    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
    – Edward
    Nov 14 at 13:23













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I am able to do store the binary tree elements using class and pointers but I ain't able to show it. I am getting the result as 1 2 but not the whole list of nodes. I am wondering at which part my logic is going wrong.



This is the code:



#include <iostream>
#include<bits/stdc++.h>
using namespace std;
class node
{
int data;
public:
node *left=NULL;
node *right=NULL;
void binarytree(node **root,int num);
void display(node *head_ref);

};
void node::binarytree(node **root,int num)
{
node *cur = new node;
{
cur->data = num;
cur->left = NULL;
cur->right = NULL;
*root = cur;
}
}
void node::display(node *head_ref)
{
if(head_ref->left!=NULL || head_ref->right!=NULL)
{
cout<<(head_ref)->data<<" ";
display(head_ref->left);
display(head_ref->right);
}

}
int main() {
node bin;
node *rt=NULL;

bin.binarytree(&rt,1);
bin.binarytree(&(rt->left),2);
bin.binarytree(&((rt->left)->left),4);
bin.binarytree(&((rt->left)->right),5);
bin.binarytree(&(rt->right),3);

bin.display(rt);

return 0;
}









share|improve this question













I am able to do store the binary tree elements using class and pointers but I ain't able to show it. I am getting the result as 1 2 but not the whole list of nodes. I am wondering at which part my logic is going wrong.



This is the code:



#include <iostream>
#include<bits/stdc++.h>
using namespace std;
class node
{
int data;
public:
node *left=NULL;
node *right=NULL;
void binarytree(node **root,int num);
void display(node *head_ref);

};
void node::binarytree(node **root,int num)
{
node *cur = new node;
{
cur->data = num;
cur->left = NULL;
cur->right = NULL;
*root = cur;
}
}
void node::display(node *head_ref)
{
if(head_ref->left!=NULL || head_ref->right!=NULL)
{
cout<<(head_ref)->data<<" ";
display(head_ref->left);
display(head_ref->right);
}

}
int main() {
node bin;
node *rt=NULL;

bin.binarytree(&rt,1);
bin.binarytree(&(rt->left),2);
bin.binarytree(&((rt->left)->left),4);
bin.binarytree(&((rt->left)->right),5);
bin.binarytree(&(rt->right),3);

bin.display(rt);

return 0;
}






c++ c++14






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 at 13:14









Pygirl

17427




17427




put on hold as off-topic by Edward, Toby Speight, papagaga, Graipher, Incomputable Nov 14 at 14:27


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Edward, Toby Speight, papagaga, Graipher, Incomputable

If this question can be reworded to fit the rules in the help center, please edit the question.




put on hold as off-topic by Edward, Toby Speight, papagaga, Graipher, Incomputable Nov 14 at 14:27


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – Edward, Toby Speight, papagaga, Graipher, Incomputable

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 4




    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
    – Edward
    Nov 14 at 13:23














  • 4




    Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
    – Edward
    Nov 14 at 13:23








4




4




Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Edward
Nov 14 at 13:23




Welcome to Code Review! I'm afraid this question does not match what this site is about. Code Review is about improving existing, working code. Code Review is not the site to ask for help in fixing or changing what your code does. Once the code does what you want, we would love to help you do the same thing in a cleaner way! Please see our help center for more information.
– Edward
Nov 14 at 13:23















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Quarter-circle Tiles

build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

Mont Emei