Re: [Dev-C++] Printing of an expression tree
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jonathan W. <jon...@gm...> - 2007-02-23 09:10:42
|
Hi,
Well it'd depend on how your tree was built.
The way I did it (in a compilation assignment) was that the since the treee
was hierarchical, print (left op right)
eg 1+5*6/7+8 is ((1+((5*6)/8))+8) would decompose to this tree :
| root:,
a , + 8
b 1 + ,
c , / 7
d 5 * 6
|
so print a prints ( printb + 8)
which is ((1+printc) + 8)
which is ((1+( printd / 7 )) + 8)
which is ((1+((5*6)/7) + 8)
just plain simple left-op-right (plus some logic for when to print a number
instead of a node, or just have an extra node which is just a number)
Hope it helps,
Jonathan
PS: Sorry for double-posting, I caught the 'Send' button instead of 'Save
Now...'
On 2/23/07, Noorez Kassam <lor...@ho...> wrote:
>
> I'm having a bit of trouble with an assignment I need to do for recursion
> and binary trees. I need to print an arithmetic expression tree. The struct
> must be defined as follows:
>
> struct TreeNode
> {
> char op;
> int number;
> TreePtr left;
> TreePtr right;
> };
>
>
> void in_order_print(TreePtr tree)
> {
> TreePtr currentNode = tree;
> if(currentNode != NULL)
> {
> if(currentNode->left != NULL)
> {
> in_order_print(currentNode->left);
> }
> // print which data?
> if(currentNode->right != NULL)
> {
> in_order_print(currentNode->right);
> }
> }
> }
>
> Since there are two fields which contain data, how would I know in the
> recursive algorithm which one to print?
>
> ------------------------------
> Explore the seven wonders of the world Learn more!<http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE>
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share
> your
> opinions on IT & business topics through brief surveys-and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
--
<Morpheus> linux, c'est une question de VI ou de MORE
|