Re: How to retrieve the pointer to leaf node of a Binary tree

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Looks to me like you have a function that returns the root node! So it is doing exactly
what you want.

For example, consider
GetLeafNode(a);
what does it return? Well, after calling the function PostOrder on each of the child
nodes, it returns a. So it will return the root!

Now, just to observe, there is no 'the' leaf node; both e and c are leaf nodes. So which
do you want? How can you tell? What if the tree were
a
c b
d
e

would you want c or e?

Also, it is clear you've read Knuth Volume 1 (Fundamental Algorithms), and use his bizarre
naming scheme. I never coud keep preorder, postorder and endorder straight. Some years
ago one of my professors introduced a notation based on the letters LRN. So a depth-first
left-to right arithmetic expression tree walk is an LRN walk (left, right, node). So much
easier, and it generalizes to complex multipass walks, e.g., NLRN. So try to avoid
Knuth's notation, because it is just too hard to keep straight.

So first you have to clarify if you want the leftmost leaf or the deepest leaf.

What would you expect to get back from the tree shown below? It has five leaves,
p, m, q, f and t.

a
b c
d e f g
h j s
k m n t
p q
r

But your code is doing exactly what you asked it to do, which is return the root.
joe

On Mon, 13 Nov 2006 21:58:02 -0800, Alamelu <Alamelu@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

How to retrieve the pointer to leaf node of a Binary tree

This is how the binary tree looks like...
a
b c
d
e

struct Node
{
public:
Node *LChild;
Node *RChild;
char val;
};

Node* GetLeafNode(Node *t)
{
if(t!=NULL)
{
PostOrder(t->LChild);
PostOrder(t->RChild);
return t;
}
}


The GetLeafNode method given above returns the pointer to the root node.

How to retrieve only the pointer to "e" node.

Regards,
Alamelu N
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: I thought this should work :-(
    ... within the tree is a highly unusual, and highly suspect, design. ... There is substantial amount of functionality ... My tree is similar to the one mentioned for Windows Explorer. ... root node that has some of the functionality of subnodes but also has ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array dependencies in c?
    ... to the root node of the vertices tree, ... the root node of the cell tree, ... in the cell object being constructed, it stores a direct pointer to ...
    (comp.lang.c)
  • Re: Binqry Tree sort
    ... In addition we have a network as a parallel ... But the processors which I want to use in my bianry tree architecture ... The root node has no bottle neck becasue it just receives data from its ...
    (comp.programming)
  • Re: to merge two binary tree
    ... node has to be taken as root node in the new tree ...etc ... Surely there must be an algorithm in Knuth. ... The root node of the merge will in general ... to a usual binary-tree insertion algorithm. ...
    (comp.lang.c.moderated)
  • RE: Newbie: Trouble accessing all nodes in a tree view
    ... My tree has a single root node named ... > a new node at a deep level on the tree. ... > listed in the collection is the root node Clients. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)