Trees Library


Problems 1

Problem : Recall from that it is possible to represent arithmetic, parenthesized expressions using a tree. If a node is an operator, such as a plus or a division sign, each of the children must be either a number, or another expression. In other words, the two children of an operator will be its operands. + 3 4 The above means (3+4). Write a function which will take in a tree_t of the form:


typedef struct _tree {
	char 	op;
	int	value;
	struct _tree *left, *right;
} tree_t;
and will evaluate the tree according to the above specification that the children of an operator will evaluate to numbers. The op field will be one of the following values, '+' '-', '*', '/', or '_', which are sharp defined to be ADD, SUB, MULT, DIV, and EMPTY respectively. Assume that the tree is a well formed expression (you don't need to do any error checking).


Problem : Assume now that your nodes represent people and their ages and as a result have fields for a person's name and age. Use the following definition for tree_t:


typedef struct _tree {
	int	age;
	char 	*name;
	struct _tree *left, *right;
} tree_t;
Write a single function which will take in a pointer to a tree_t and will free the entire tree and all of the memory associated with it.


Problem : A Huffman tree is a means of encoding characters, that is, a way of assigning a certain sequence of bits to a character (ASCII is another convention). The idea is that you can save space when storing a file if you can find an encoding for the characters such that the file requires fewer bits overall. We will not cover the process of building such a tree, but we will consider the process of using one. Starting from the root node, you keep walking along either the left or the right branch until you reach the desired character. Moving left corresponds to a 0 bit and moving right to a 1 bit. So, if you have to go left, right, right to get to the character 'A', then the encoding for 'A' is 011. How can you describe the location of all of the nodes that have characters associated with them? The root node, for example, has no character associated with it.


Take a Study Break

SparkLife

Star Trek gets SEXY

Chris Pine and Zoe Saldana heat up the red carpet!

SparkLife

Are you afraid of relationships?

Auntie SparkNotes can help!

SparkLife

Wanna get JLaw's gorgeous glow?

Click here for simple, sexy makeup tricks!

SparkLife

Sexy starlet style

See every single look from the Met Gala!

SparkLife

Who'd be on your zombie-apocalypse crew?

We already dib'sed Genghis Khan.

Geek out!

The MindHut

Geeky Actresses: Then and Now

Before the fame!

The MindHut

9 Scientific Inaccuracies in Iron Man 3

Click to see what they got wrong.

The MindHut

Top 10 Predictions Sci-Fi Got WRONG

So wrong, they're WRONG.

The MindHut

The 15 Most Awesome Robots, Ever

These Robots Rock!

The MindHut

If You Like Game of Thrones...

...Then you'll LOVE these books!

The Book

Cover image

Read What You Love, Anywhere You Like

Get Our FREE NOOK Reading Apps