Thread: [Dev-C++] Printing of an expression tree
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
From: Noorez K. <lor...@ho...> - 2007-02-23 05:21:28
|
I'm having a bit of trouble with an assignment I need to do for recursion a= nd binary trees. I need to print an arithmetic expression tree. The struct = must be defined as follows: =20 struct TreeNode{ char op; int number; TreePtr left; TreePtr right;}; =20 =20 void in_order_print(TreePtr tree){ TreePtr currentNode =3D tree; if(currentNode !=3D NULL) { if(currentNode->left !=3D NULL) { = in_order_print(currentNode->left); } // print which data? if(currentNode->right !=3D NULL) { in_order_print(currentNode->= right); } }} =20 Since there are two fields which contain data, how would I know in the recu= rsive algorithm which one to print? _________________________________________________________________ Explore the seven wonders of the world http://search.msn.com/results.aspx?q=3D7+wonders+world&mkt=3Den-US&form=3DQ= BRE= |
From: Jonathan W. <jon...@gm...> - 2007-02-23 09:04:38
|
Hi, Well it'd depend on how your tree was builtr. 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) + 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 |
From: Per W. <pw...@ia...> - 2007-02-23 09:27:54
|
For an expression to be meaningful (emitted in pre-order, in-order, post-order) you must of course always emit _all parts_ of the expression. Hence, you don't have to worry about 'which' part to emit. You must emit both parts. If one half of the tree is NULL, then you must print it anyway - either as a space-filling intendation, or as a NULL message or whatever. Exactly how you print it, is part of what the assignment wants _you_ do decide/solve. How do you know when you have solved the exercise correctly? When you can read the printout, and clearly see - and restore - the original expression from your printout. But once more: how to do it is _your_ job, and shouldn't be solved by cooperation on mailing lists. You know very well that it is considered cheating - and a lot of schools can (and will) punish students for using mailing lists. /Per W On Fri, 23 Feb 2007, Jonathan Winterflood wrote: > Hi, > > Well it'd depend on how your tree was builtr. > 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) > > > > + > 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 > |
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 |