Exercise from B. Stroustrup book

Sep 6, 2011 at 2:14am
I'm doing an exercise from Chapter 7 of Bjarne Stroustrup's "C++ Programming Language - Third Edition". The exercise is:

-Write a function for entering new words into a tree of tNodes.

-Write a function to write out a tree of tNodes.

-Modify tNode so that it stores only a pointer to an arbitrarily long word stored as an array of characters on free store using new.

-Modify the functions to use the new definition of tNode.

Whereas tNode is:

1
2
3
4
5
6
7
struct tNode
{
	string word;
	int count;
	tNode *left;
	tNode *right;
};


I'm a little confused and I feel I am perhaps going about this in the wrong way. My current interpretation of the exercise is when he says 'tree' I am imagining it in a more literal sense where it branches out to form a tree of objects going from one parent object to perhaps 12 child objects like here:

http://cslibrary.stanford.edu/110/binarytree.gif

However, I am slowly beginning to think that perhaps the intended meaning is actually different and yet deceptively simple. Rather than a tree as show above which branches out, the intended meaning should be viewed more like a string. Imagine the phrase "the brown dog", where 'the' is left of 'brown' and 'dog' is right of 'brown', as if each node were a token in a string.

If anyone has ever read the book and done exercises, I would greatly appreciate some clarification.
Last edited on Sep 6, 2011 at 2:22am
Sep 6, 2011 at 2:28am
No your original interpretation was right. A 'tree' can have as many children as it likes. A binary tree is just a special case in which each node can only have two (or sometimes less than) children.
Sep 6, 2011 at 2:38am
Ah, excellent, thank you for the clarification. Considering the fact that this node struct can only accommodate 2 children , would this qualify as a binary tree? Again, thank you.
Sep 6, 2011 at 4:32am
Yep, definitely it's binary tree
Topic archived. No new replies allowed.