Re: How to retrieve the pointer to leaf node of a Binary tree
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Wed, 15 Nov 2006 17:37:44 -0500
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 treeJoseph M. Newcomer [MVP]
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
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Prev by Date: Re: CEdit Access Underlying String (as a const reference)
- Next by Date: how to change color of font or background on listbox
- Previous by thread: Having Problem with a String....Please help....
- Next by thread: how to change color of font or background on listbox
- Index(es):
Relevant Pages
|