C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 11th, 2012, 01:16 PM
srsiddiqui srsiddiqui is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 15 srsiddiqui User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old November 11th, 2012, 01:59 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 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;
}
Comments on this post
ManiacDan agrees!

Last edited by Lux Perpetua : November 11th, 2012 at 02:10 PM. Reason: Misread OP

Reply With Quote
  #3  
Old November 11th, 2012, 02:04 PM
srsiddiqui srsiddiqui is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 15 srsiddiqui User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #4  
Old November 11th, 2012, 02:11 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
Yes, sorry, I misread it the first time. I've edited my post.

Reply With Quote
  #5  
Old November 11th, 2012, 02:16 PM
srsiddiqui srsiddiqui is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 15 srsiddiqui User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #6  
Old November 11th, 2012, 03:50 PM
srsiddiqui srsiddiqui is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 15 srsiddiqui User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #7  
Old November 11th, 2012, 10:11 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 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.

Reply With Quote
  #8  
Old November 12th, 2012, 02:31 AM
srsiddiqui srsiddiqui is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 15 srsiddiqui User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #9  
Old November 12th, 2012, 02:33 PM
srsiddiqui srsiddiqui is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 15 srsiddiqui User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #10  
Old November 13th, 2012, 12:33 AM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 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.

Reply With Quote
  #11  
Old November 14th, 2012, 11:57 PM
H2ea5th7er H2ea5th7er is offline
Permanently Banned
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 H2ea5th7er User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #12  
Old November 15th, 2012, 08:19 AM
bolainmarsh5 bolainmarsh5 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 bolainmarsh5 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Algorithm for prefix in a tree

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap