Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help on contructing a tree in C++ requirements are in the comments below st

ID: 3714047 • Letter: N

Question

Need help on contructing a tree in C++

requirements are in the comments below

struct tnode

{ int Elem;

tnode *Rchild; // pointer to the right child

tnode *Lchild; // pointer to the left child

};

int main()

{

// declare the root pointer

tnode *root; // declared the pointer

// declare another pointer V

tnode *V; //declared point V

//create a binary tree with 5 nodes with elements obtained from the user   

// first create 3 nodes (parent, lchild, rchild)

//then go down to the lchild (V points there) and add its children

//make sure all leaf pointers are NULL   

Explanation / Answer

#include <iostream>

using namespace std;

struct tnode {
    int Elem;
    tnode* Rchild; // pointer to the right child
    tnode* Lchild; // pointer to the left child
};
int main()
{
    // declare the root pointer
    tnode* root; // declared the pointer
    // declare another pointer V
    tnode* V; //declared point V
//create a binary tree with 5 nodes with elements obtained from the user
// first create 3 nodes (parent, lchild, rchild)
//then go down to the lchild (V points there) and add its children
//make sure all leaf pointers are NULL
   int n;
   cout << "Enter 5 numbers: ";
   cin >> n;
   V = new tnode;
   V->Elem = n;
   V = new tnode;
   V->Lchild = new tnode;
   cin >> n;
   V->Lchild->Elem = n;
   V->Rchild = new tnode;
   cin >> n;
   V->Rchild->Elem = n;
   V->Rchild->Lchild = NULL;
   V->Rchild->Rchild = NULL;
   V->Lchild->Lchild = new tnode;
   cin >> n;
   V->Lchild->Lchild->Elem = n;
   V->Lchild->Rchild = new tnode;
   cin >> n;
   V->Lchild->Rchild->Elem = n;
   V->Lchild->Lchild->Lchild = NULL;
   V->Lchild->Lchild->Rchild = NULL;
   V->Lchild->Rchild->Lchild = NULL;
   V->Lchild->Rchild->Rchild = NULL;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote