Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Need help with programming in C?
I have notes that show a simple way to print out integer binary trees in this form
20(10(5()())(17()()))(30(21()())(*))
I want to write a program that reads in a binary tree in the above form and displays it in a ‘friendlier’ style
20----30
| |
10-17 21
|
5
The tree has left branches vertically down the page and right branches horizontally right
The binary tree is read in using argv[1](this is the string itself, NOT a filename), for example :% treevis "20()()"
I'm new to C and completely new to binary trees. Could you break down what I have to do for this project and explain. A push in the right direction would be greatly appreciated
1 Answer
- JonathanLv 77 years agoFavorite Answer
I already gave you a complete program for handling your syntax, so far as I've been able to understand it by example -- as you have NOT provided a set of productions. The first thing you need to do is to create a production set. Don't even bother coding anything until you have that much. Something like this:
tree := digit digits ( tree ) ( tree ) | * | <null>
digit := 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
digits := digit digits | <null>
Even without training you should be able to approximately understand what I just wrote above. The only special meaning above is <null> which matches perfectly with exactly 0 characters.
Once you have the productions, you write code to analyze and parse them. Easiest is to just write a subroutine for each production above. I did that in the example I gave you before (shown at bottom below.)
But that isn't enough, of course. In parsing, you need to produce a structure out of that so that as you parse the ASCII string you are also creating some other equivalent structure that you will later use. So you need to also design a structure. That structure might look like:
typedef struct tree tree;
typedef struct tree {
unsigned int id;
tree *left, *right;
} tree;
This gives you a place for the identifying integer as well as the left and right branches also needed. It will be the job of parsing to construct, fill out, and then chain together these structures so that when the parsing is done you have a tree with nodes like that above. The first half of the code I provide below does that for you, already.
Then you have the problem of displaying the tree in a different ASCII form, as a visualizable tree of some kind. I chose my own ways (three different ones), none of which match yours exactly. You need to sit down with a piece of paper and work out HOW you will construct your kind of output for your trees. I didn't like it, so I did it differently. But I think you could achieve yours by thinking closely about what it means to print a right branch (head leftward in the display) and to print a left branch (head downward in the display.) You will probably need to measure and construct a large enough character matrix, print onto that matrix first, then print out the matrix when you are done. You need to think closely about how that will be achieved. The examples (three of them) I gave did that for you but not exactly in the way you suggest above.
What else do you NEED??? Certainly I've given you a "push in the right direction." Not so?
Source(s): http://ideone.com/eB4ken