The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Algorithm for prefix in a tree
Discuss Algorithm for prefix in a tree in the C Programming forum on Dev Shed. Algorithm for prefix in a tree C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 11th, 2012, 01:16 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 15
Time spent in forums: 3 h 8 m 53 sec
Reputation 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
|

November 11th, 2012, 01:59 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
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;
}
Last edited by Lux Perpetua : November 11th, 2012 at 02:10 PM.
Reason: Misread OP
|

November 11th, 2012, 02:04 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 15
Time spent in forums: 3 h 8 m 53 sec
Reputation Power: 0
|
|
Thanks alot! but i have to use stack with loops inside.
Quote: | 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;
}
|
|

November 11th, 2012, 02:11 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
|
Yes, sorry, I misread it the first time. I've edited my post.
|

November 11th, 2012, 02:16 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 15
Time spent in forums: 3 h 8 m 53 sec
Reputation 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
Quote: | Originally Posted by Lux Perpetua Yes, sorry, I misread it the first time. I've edited my post. |
|

November 11th, 2012, 03:50 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 15
Time spent in forums: 3 h 8 m 53 sec
Reputation 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
Quote: | 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  |
|

November 11th, 2012, 10:11 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | 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.
|

November 12th, 2012, 02:31 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 15
Time spent in forums: 3 h 8 m 53 sec
Reputation 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.
Quote: | 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.
|
|

November 12th, 2012, 02:33 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 15
Time spent in forums: 3 h 8 m 53 sec
Reputation Power: 0
|
|
Can you please write the generalised algo for this question.
with using structures.
Quote: | Originally Posted by Lux Perpetua Yes, sorry, I misread it the first time. I've edited my post. |
|

November 13th, 2012, 12:33 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | 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: Quote: | 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. | Quote: | 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.
|

November 14th, 2012, 11:57 PM
|
|
Permanently Banned
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 16 m 43 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
|
|
|
I have to write a algorithm for creating a prefix using stack.
URL
URL
URL
URL
URL
|

November 15th, 2012, 08:19 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 2
Time spent in forums: 8 m 40 sec
Reputation Power: 0
|
|
|
I don't see anything wrong with your approach. If you're looking for a spike solution, perhaps the action taken in case 4 is actually feasible for the first three cases, IE find the common prefix to k and k' and rebuild the node with that in mind. If it happens that the keys were prefixes of one-another, the resulting trie will still be correct, only the implementation did a bit more work than it really had to. but then again, without any code to look at it's hard to say if this works in your case.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|