linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Bambach <eric@cisu.net>
To: "J." <mailing-lists@xs4all.nl>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: binary tree traversal...
Date: Wed, 26 Jan 2005 20:07:19 -0600	[thread overview]
Message-ID: <200501262007.19118.eric@cisu.net> (raw)
In-Reply-To: <Pine.LNX.4.21.0501262350200.30870-100000@hestia>

On Wednesday 26 January 2005 05:01 pm, J. wrote:
> Wednesday, January 26
>
> Hello,
>
> I am trying to traverse a binary tree, returning each node that is
> encounterd so that I can preform operations on that node. However it keeps
> looping over the same first node, it refuses to travel any further down
> the tree ... What did I overlooked ? I know the tree is loaded with data
> since a simple recursive preorder treeprint confirms that.
>
> An example:
>
>  while((root = treerecurse(root)) != NULL)
>   printf("%4d %s\n", root->count, root->word);
>
> And the treerecurse function would look something like:
>
> struct tnode *treerecurse(struct tnode *p) {
>  if(p != NULL) {
>   treerecurse(p->left);
>   return p;
>   treerecurse(p->right);
>  }
> }
>
> I guess there is something wrong in my reasoning about this...

Yea, that definatly wont work. I think you're messing up because a recursive 
function can only pass back the top node, or a node that meets a certain 
condition back up the calling stack, without static data, you cant call a 
recursive function and keep going where it left off so you can only return 
one node.

Why dont you just pass a function pointer into the recursive function so it 
looks like this:(pseudocode, my function pointer syntax is bad)

-------------(pseudo)code--------

treerecurse(root);

//Now you dont need a loop. A single line will now make your function 
//perform on all the nodes.

//And the treerecurse function would look something like:

struct tnode *treerecurse(struct tnode *p,function *dofoo) {
 if(p != NULL) {
  dofoo(p->data);
  treerecurse(p->left);
  return p;
  treerecurse(p->right);
 }
}
---------------end (pseudo)code---------------

If its C++, then its even easier, make tnode a class and define a function 
dofoo and inside the recursion call tnode.dofoo();
> Thnkx..
>
> J.
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

-- 
----------------------------------------
--EB

> All is fine except that I can reliably "oops" it simply by trying to read
> from /proc/apm (e.g. cat /proc/apm).
> oops output and ksymoops-2.3.4 output is attached.
> Is there anything else I can contribute?

The latitude and longtitude of the bios writers current position, and
a ballistic missile.

                --Alan Cox LKML-December 08,2000 

----------------------------------------
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  reply	other threads:[~2005-01-27  2:07 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-26 23:01 binary tree traversal J.
2005-01-27  2:07 ` Eric Bambach [this message]
2005-01-27 20:06   ` J.
2005-01-27  3:19 ` Joel Pareja
2005-01-30 11:49 ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200501262007.19118.eric@cisu.net \
    --to=eric@cisu.net \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=mailing-lists@xs4all.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).