Tree implementation, ideas for keyboard input method needed

B

BartC

ImpalerCore said:
Greetings,
[snip]

Now I need a sane way to input a tree from keyboard. What would be the
easiest one?
If you want to input a tree from the keyboard, you could type in the
tree data in XML format, feed it to one of the parsers, and use its
events to trigger using your interface to populate your tree. It'll
take some work, but learning how to use an XML parser can be quite
useful.

It'll take quite some work on the part of the user as well, who will also
need to be an expert on XML. (As well as needing to be prepared for quite a
lot of typing..)

XML I would have thought was more suitable for file rather than keyboard
input, and would be best for machine-generated data.
 
I

ImpalerCore

Greetings,
Now I need a sane way to input a tree from keyboard. What would be the
easiest one?
Thanks in advance!
The simplest is probably to leverage an existing XML parser and write
your own XML format to populate your own custom "DOM" structure.  The
expat and libxml2 are fairly popular, and there are some varying
implementations of DOM floating around.

     Angels and ministers of grace defend us!  The O.P. is trying
to build a tree in which each node carries a payload of

        ONE

        LONESOME

        `CHAR'

        VALUE

... and you want to inflict XML on him?!?!

     Whereabouts do you live, ImpalerCore?  I want to be about five
hundred miles away, and upwind, when you find a cockroach in your
cupboard and exterminate it with nuclear armaments.

S/He doesn't need to use to entire subset that is XML. The XML file
format represents a hierarchical structure that directly maps to a
tree. What's wrong with this?

<a>
<b>
<c/>
<d/>
</b>
<e>
<f/>
</e>
</a>

---Corresponding tree---

a
/ \
b e
/ \ \
c d f

If one is uncomfortable with using a XML parser, one could devise his
own scheme/parser. The XML file format is just one popular model for
how to read and write information in a hierarchical tree-based
structure.

One could use a simple 'child:parent' format string like
'a,b:a,c:b,d:b,e:a,f:e', tokenize it, and using appropriate searches
and inserts, and would probably work fine for small trees.

I guess I've had too much XML on the mind lately.

Best regards,
John D.
 
I

ImpalerCore

Greetings,
Now I need a sane way to input a tree from keyboard. What would be the
easiest one?
If you want to input a tree from the keyboard, you could type in the
tree data in XML format, feed it to one of the parsers, and use its
events to trigger using your interface to populate your tree.  It'll
take some work, but learning how to use an XML parser can be quite
useful.

It'll take quite some work on the part of the user as well, who will also
need to be an expert on XML.  (As well as needing to be prepared for quite a
lot of typing..)

If you have a document that has a well defined structure, and you have
decent 'sed' or 'perl' skills, one can construct an XML document, or
at least a document fragment. I'm currently tasked to create an XML
representation of a document from an approximate 2000+ PDF file, and
yes it is a lot of work, but 'sed' can provide some of the fine
document structure for data elements that have a uniform format. I
still have to stitch the fragments together manually into a cohesive
XML document, and flesh out any format problems my script stumbles
across.
XML I would have thought was more suitable for file rather than keyboard
input, and would be best for machine-generated data.

We have web forms that people fill out that get translated into an XML
document, so while the content is user-entered, the XML structure is
computer generated. People rarely have to manually encode raw XML
text files except for testing.

I agree that XML format is probably too much for toy tree input.

Best regards,
John D.
 
J

Joe keane

Now I need a sane way to input a tree from keyboard. What would be the
easiest one?

Like this?

int user_edit(struct tree *tree)
{
...
for (;;)
{
...
fputs("Command? [acdlsq] ");
fflush(stdout);
fgetstnl(cmd, sizeof cmd, stdin);
c = cmd[0];

switch (c)
{
case 'a':

fputs("Key? ", stdout);
fflush(stdout);
fgetstnl(key, sizeof key, stdin);
fputs("Data1? ", stdout);
fflush(stdout);
fgetstnl(data1, sizeof data1, stdin);
err = add_node(tree, key, data1);
break:
...
case 'l':
fputs("Filename? ", stdout);
fflush(stdout);
fgetstnl(filename, sizeof filename, stdin);
err = load_tree(tree, filename);
break;
...
case 'q':
goto done;
}

if (err != 0)
fprintf(stdout, "Error: your command failed (%d)\n", err);
...
}
...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top