Reading this tree...

This is a discussion on Reading this tree... within the C++ Programming forums, part of the General Programming Boards category; hi ppl... my code saves a tree in this format in a file: consider this convention: word before = sign ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    124

    Reading this tree...

    hi ppl...

    my code saves a tree in this format in a file:
    consider this convention: word before = sign is a node, after the = sign is the value of a branch of this node, after : is the value of this node...
    e.g. the file contents below would look like this:
    Code:
                            (pat)
                       /       \        \ 
                   none  some  full
                    /           \         \
                  NO         YES   (hun)
                                          /    \
                                       yes   no
                                        /        \
                                    (type)   NO
                                  /  /   \  \
    Code:
    pat = none : NO
    pat = some : YES
    pat = full : 
       hun = yes : 
          type = burger : YES
          type = french : NO
          type = italian : NO
          type = thai : 
             fri = yes : YES
             fri = no : NO
       hun = no : NO
    i hope you get the above...now i have no idea how to read the file to make the same tree...although i want to make a simpler tree because i just need it to be able to traverse it to check answer...e.g. if i say to the program that pat is full, and hun is yes, and type is italian then the output should be NO according to the tree...

    what's the best possible way to do this? is it really necessary to read it as a tree with all nodes etc etc...cause like i said i just need to know what is connected to what and what values it has for which branches...then i can traverse the tree according to this scheme (for above example)...
    Code:
    //What is the value of pat? (user: full)
    //if there are no connections (children) for branch full then stop else
    //what is the value of node connected to full branch? 
    etc etc
    Regards,

    Farooq
    Last edited by alvifarooq; 11-04-2004 at 12:28 PM.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Looking at you file something where very deductive
    = is an assignment which specifies that a given node conects to another, : specifies a key. The key can again be associated with another node.
    So
    node = node1 : key1
    node = node2 : key2

    But when going deeper:

    node = node1 :
    node = node2 : key2

    So read, maybe, like this
    node name, operator =, new node, operator :
    After that if you find 2 symbols separated only by newlines or spaces, the 1st is the key, the 2nd is another node, at the same depth.
    If you find only one symbol, then symbol is a new deeper node, then comes another ':'
    You'll have to use recursion to build the tree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Tree Search
    By C++Newbie in forum C++ Programming
    Replies: 7
    Last Post: 04-05-2011, 02:17 AM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. binary tree node structure
    By Kirsten in forum C Programming
    Replies: 2
    Last Post: 04-26-2002, 09:02 PM
  5. read records fron file into a binary tree
    By Kirsten in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 03:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21