SparkNotes Shopping Cart  |     |  Checkout
Brought to you by Barnes and Noble
Trees Library
  
 
Problems 1
Problem 1.1: 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). [Solution]
Problem 1.2: 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. [Solution]
Problem 1.3: 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. [Solution]
Help | Feedback | Make a request | Report an error | Send to a friend
 
Feeling hopelessly behind? No Fear Algebra will help you catch up in no time.
More...
 
SparkNotes Study Cards boil down Chemistry into digestible tidbits, making studying easier.
More...
 
 
Go to top