### Thread: Algorithm for prefix in a tree

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
15
Rep Power
0

#### Algorithm for prefix in a tree

hi everyone!

I have to write a algorithm for creating a prefix using stack.

A
/ \
B C
\\\
DEF

The prefix code is ABDEFC

Can anyone explain me how to do it with using stack.I know this is simple question but I am not able to write a algorithm for it.I do not how to write an algorithm.

After coming to ABDEF by what condition i use to go back to B and then go to C?

Thanks
2. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Feb 2004
Location
San Francisco Bay
Posts
1,939
Rep Power
1313
Use recursion. In pseudocode:
Code:
string prefix(tree x) {
string val(x.root.name);
for (int k = 0; k < x.num_children; ++k) {
val = val + prefix(x.child(k));
}
return val;
}
Edit: Oh, sorry, I gather from your post that you don't want to use recursion, though technically, if you were to implement a recursive solution, it would implicitly be using a stack on the assembly level.

Nonrecursive pseudocode:
Code:
string prefix(tree x) {
string val;
stack<tree> the_stack;
the_stack.push(x);
while (!the_stack.empty()) {
tree y(the_stack.top());
the_stack.pop();
val = val + y.root.name;
for (int k = y.num_children - 1; k >= 0; --k) {
the_stack.push(y.child(k));
}
}
return val;
}

• ManiacDan agrees
Last edited by Lux Perpetua; November 11th, 2012 at 03:10 PM. Reason: Misread OP
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
15
Rep Power
0
Thanks alot! but i have to use stack with loops inside.

Originally Posted by Lux Perpetua
Use recursion. In pseudocode:
Code:
string prefix(tree x) {
string val(x.root.name);
for (int k = 0; k < x.num_children; ++k) {
val = val + prefix(x.child(k));
}
return val;
}
4. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Feb 2004
Location
San Francisco Bay
Posts
1,939
Rep Power
1313
Yes, sorry, I misread it the first time. I've edited my post.
5. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
15
Rep Power
0
Thanks alot for the post .

It is difficult for me to understand the code.

I was trying to do with link list and stack.

I m really sorry I cant understand and i think it is high level programing.
I am new to computer world :(

Originally Posted by Lux Perpetua
Yes, sorry, I misread it the first time. I've edited my post.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
15
Rep Power
0
I am using a struct_node which has
val;
node_son;
node_brother;

my algo is this but i m stucking at one place

current <- father;
if (current != NULL)
{
while( current ! = NULL)
{
insert the value of curent in the stack
current <-- current.son;
} // here the value ABD will be inserted in the stack

while ( current--> brother ! = NULL)
{
current <-- current.brother
insert the value of current into the stack

} // here the stack will have ABDEF

This is where i m stuck how i put the 'C' in the stack , how to hold the value of C .
}

as the final answer should be ABDEFC

Originally Posted by srsiddiqui
Thanks alot for the post .

It is difficult for me to understand the code.

I was trying to do with link list and stack.

I m really sorry I cant understand and i think it is high level programing.
I am new to computer world :(
7. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Feb 2004
Location
San Francisco Bay
Posts
1,939
Rep Power
1313
Originally Posted by srsiddiqui
my algo is this but i m stucking at one place
I hate to be the one to tell you this, but you're not simply stuck at one place. The pseudocode you presented is not even close.

Here is what I gather your tree structure is, from your last post:
Code:
A --------------------- *
|
B ------------- C - *
|               |
D - E - F - *   *
|   |   |
*   *   *
where '-' denotes "brother," '|' denotes "son," and '*' denotes NULL. The procedure to do a preorder traversal of this type of tree is actually even simpler than my previous pseudocode: push A onto an empty stack, and then just repeatedly pop a node X from the stack and push X.brother and X.son, in that order, ignoring the ones that are NULL. If you record the nodes in the order in which you pop them, you'll get the correct sequence.

Rather than give you new pseudocode for this, let's just go through the process and see what gets pushed onto and popped from the stack:

• Push A onto an empty stack. (Stack = {A})
• Pop A from stack. Push A.son (=B) onto the stack. (Stack = {B})
• Pop B from stack. Push B.brother (=C) and B.son (=D) onto stack. (Stack = {C, D})
• Pop D from stack. Push D.brother (=E) onto stack. (Stack = {C, E})
• Pop E from stack. Push E.brother (=F) onto stack. (Stack = {C, F})
• Pop F from stack. (Stack = {C})
• Pop C from stack. (Stack = {})
• Stack is empty, so exit.
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
15
Rep Power
0
Thanks!
It is quite simple.Sorry that you have to tell me this things as i suck in computer.

I did this way, i do not know if it is wright or not.

Code:
Procedure Prefix(input father)

current <-- father;

if (current != null)
{
while current != null)
{
push(current.value ) in stack
currentparent<-- current
}

if( current.brother != NULL)
{
current<--(current.brother)
push( current.value) in stack
}
if( currentparent.brother!= null)
{
current<-- currentparent.brother
push(current.value)
}
}
I think this is wrong as i do not POP any value.
but i thought alot , this is working with my tree.

Originally Posted by Lux Perpetua
I hate to be the one to tell you this, but you're not simply stuck at one place. The pseudocode you presented is not even close.

Here is what I gather your tree structure is, from your last post:
Code:
A --------------------- *
|
B ------------- C - *
|               |
D - E - F - *   *
|   |   |
*   *   *
where '-' denotes "brother," '|' denotes "son," and '*' denotes NULL. The procedure to do a preorder traversal of this type of tree is actually even simpler than my previous pseudocode: push A onto an empty stack, and then just repeatedly pop a node X from the stack and push X.brother and X.son, in that order, ignoring the ones that are NULL. If you record the nodes in the order in which you pop them, you'll get the correct sequence.

Rather than give you new pseudocode for this, let's just go through the process and see what gets pushed onto and popped from the stack:

• Push A onto an empty stack. (Stack = {A})
• Pop A from stack. Push A.son (=B) onto the stack. (Stack = {B})
• Pop B from stack. Push B.brother (=C) and B.son (=D) onto stack. (Stack = {C, D})
• Pop D from stack. Push D.brother (=E) onto stack. (Stack = {C, E})
• Pop E from stack. Push E.brother (=F) onto stack. (Stack = {C, F})
• Pop F from stack. (Stack = {C})
• Pop C from stack. (Stack = {})
• Stack is empty, so exit.
9. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Nov 2012
Posts
15
Rep Power
0
Can you please write the generalised algo for this question.
with using structures.

Originally Posted by Lux Perpetua
Yes, sorry, I misread it the first time. I've edited my post.
10. No Profile Picture
Contributing User
Devshed Intermediate (1500 - 1999 posts)

Join Date
Feb 2004
Location
San Francisco Bay
Posts
1,939
Rep Power
1313
Originally Posted by srsiddiqui
Thanks!
It is quite simple.Sorry that you have to tell me this things as i suck in computer.

I did this way, i do not know if it is wright or not.

Code:
Procedure Prefix(input father)

current <-- father;

if (current != null)
{
while current != null)
{
push(current.value ) in stack
currentparent<-- current
}

if( current.brother != NULL)
{
current<--(current.brother)
push( current.value) in stack
}
if( currentparent.brother!= null)
{
current<-- currentparent.brother
push(current.value)
}
}
I think this is wrong as i do not POP any value.
but i thought alot , this is working with my tree.
Again, not even close. I'm surprised, since I already gave you the answer:
Originally Posted by Lux Perpetua
...push A onto an empty stack, and then just repeatedly pop a node X from the stack and push X.brother and X.son, in that order, ignoring the ones that are NULL. If you record the nodes in the order in which you pop them, you'll get the correct sequence.
Originally Posted by srsiddiqui
Can you please write the generalised algo for this question.
with using structures.
So far, I have provided:
• Recursive pseudocode
• Nonrecursive pseudocode, using a stack
• A complete plain-language description of the stack-based algorithm, using your data structure
• A detailed walk-through of the stack algorithm on your example tree
Based on the pseudocode you last posted, you haven't comprehended even 1% of my attempts to point you in the right direction. If you are this clueless, then I'm sorry, you're simply in over your head, and there's no point in my giving you further help on this topic.