You are here:

C++/to accept characters and cost of a node


QUESTION: supposed to submit this on monday. the problem am facing is to accept a character instead of integer and the cost of each node. also, how do i make the child to become a root so it could have its own children.

the small code i have below accepts only a root and its children.


struct tree

 char data;
  tree *left;
  tree *right;
   }*sptr, *q;


void rightcheck();
void leftcheck();

char insdata[3];
tree *node;

void main()
 int val,i;
  node=new tree;
   cout<<" PLEASE PUT THE  root->>";
        cout<<"enter the value of the node";
   cout<<" GIVE THE child->>";


         cout<<"the tree is"<<node->data<<endl;


ANSWER: Hello Suzzie.

I am having some difficulty understanding your question and I'm not sure what your program is to do. I don't know what the purpose of insdata is. Please explain your assignment in more detail.

I suppose you want to accept a character into val where you do
cin >> val;

To accept a character with cin, instead of an integer, simply make the destination a character. You already do this at
cin >> node->data;

So you would declare val as a character, then cin >> val would read in a character.

I am not sure what you mean by "the cost of each node"

Since you are using C++, you can have a constructor in tree which can automatically set left and right to NULL. You don't have to do it in the main program.

It would look like this:
struct tree
   tree() { left = right = NULL; }
   char data;
   tree *left;
   tree *right;

I will be available this weekend to help you, and I will check my e-mail regularly.

Best regards.

---------- FOLLOW-UP ----------

QUESTION: ok.....i will do that and if i face any difficulties, will get back to you.

the question given by the lecturer is:

using c++, write a program to accept nodes, starting with the root. construct a tree with 4 levels and give the distance from each node to its child. calculate the shortest path from the root to the 4th level.

ANSWER: Ok, I'll give you a tutorial on binary trees later, right now I have to go to my job. Please let me know about the data being stored in the tree. What does the data in the node represent? Does it represent some kind of distance from the parent node?

---------- FOLLOW-UP ----------

QUESTION: the data accepts the root node and the insdata is to accept the child nodes.

Hello Suzzie.

I am trying a little experiment. I've written a short tutorial on binary trees on google documents. It was inspired by this question.

You can see it at this link.
Let me know if you cannot see it.

I think the tutorial will help you.

I still don't fully understand your assignment, but I do understand that you need a tree constructed. There is code in the tutorial to do that.

I'll check my email periodically from 1:00 PM to 9:00 PM Ghana time if you need more help with your assignment.


All Answers

Answers by Expert:

Ask Experts




No longer taking questions.


No longer taking questions.

No longer taking questions.

©2017 All rights reserved.