c++ - Binary Tree (Not Binary Search Tree) Creating New Node and Children -


i trying implement method create node insert binary tree (not bst).

struct node{        struct node *left;        int data;        struct node *right; };  typedef struct node *n;  void createnewnode() {       char r; // stands response (whether or not left or right child should created)      int d; //data stored in node       n newnode = new(struct node); //creates new node       cout<<"enter data new node:"<<endl;      cin>>d;      newnode->data = d;       cout<<"any left child? y/n"<<endl;      cin>>r;      switch (r) {             case 's':                  createnewnode(); // thought make recursive , if child going created, method call on again                  break;              case 'n':                  newnode->left = null; // if child not created pointer null                  break;             }       cout<<"any right child? y/n"<<endl;      cin>>r;      switch (r) {             case 's':                  createnewnode(); //recursive method again                  break;              case 'n':                  newnode->right = null; // if child not created pointer null                  break;             } } 

the problem facing when use recursive method create left or right child. think not pointing value parent node created first. right or wrong? guess question whether or not linking parent node right or left child node method trying implement.

in createnewnode() function, create new node , leave without relating them each other! should bind left or right pointer.

this should do:

  1. change output of function void n
  2. at end of function return newly created node
  3. in both of switch statements, call function recursively, assign output of function call newnode->left or newnode->right accordingly.

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -