Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
November 11th, 2012, 02:16 PM
 srsiddiqui
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

#2
November 11th, 2012, 02:59 PM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
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
November 11th, 2012, 03:04 PM
 srsiddiqui
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; }```

#4
November 11th, 2012, 03:11 PM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
Yes, sorry, I misread it the first time. I've edited my post.

#5
November 11th, 2012, 03:16 PM
 srsiddiqui
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.

#6
November 11th, 2012, 04:50 PM
 srsiddiqui
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

#7
November 11th, 2012, 11:11 PM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
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.

#8
November 12th, 2012, 03:31 AM
 srsiddiqui
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.

#9
November 12th, 2012, 03:33 PM
 srsiddiqui
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.

#10
November 13th, 2012, 01:33 AM
 Lux Perpetua
Contributing User

Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,939
Time spent in forums: 1 Month 1 Week 3 h 27 m 29 sec
Reputation Power: 1312
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.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Algorithm for prefix in a tree

## Developer Shed Advertisers and Affiliates

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |