O(1) O(log n) O(n) O(n log n) O(n2) AVL tree Splay tree Binary search tree Hash table Open addressing Separate chaining Linear probing Hash function Hash Table Size Rotation Collision Hash function Even number 2n-1 Prime number 2n
-----------------------
0 | |
| |
-----------------------
1 | |
| |
-----------------------
2 | |
| |
-----------------------
3 | |
| |
-----------------------
4 | |
| |
-----------------------
5 | |
| |
-----------------------
6 | |
| |
-----------------------
-----------------------
0 | |
| |
-----------------------
1 | |
| |
-----------------------
2 | |
| |
-----------------------
3 | |
| |
-----------------------
4 | |
| |
-----------------------
---100---
/ \
50 -150-
/ / \
25 125 -300-
/ \
175 400
\
250
/ \
200 275
-300-
/ \
175 400
\
250
/ \
200 275
20
/ \
10 40
/ \
8 16
/
12
14 19 16 26 21 19 68 65 31 32 44 23
+
/ \
- +
/ \ / \
- 4 8 10
/ \
16 5
This expression tree represents the arithmetic expression:
((16-5) - 4) + (8+10)
which evaluates to 25. An expression tree can be evaluated
by evaluating the left subtree, then the right subtree, and finally
performing the indicated operation on the values returned by evaluating
the left and right subtrees. A leaf evaluates to its number. For example,
in the above expression tree, the + node with the leaves 8 and
10 evaluates to 18 since its left subtree evaluates to 8 and its right
subtree evaluates to 10. Similarly the root + node evaluates to
25 because its left subtree evaluates to 7 and its right subtree to 18.
Your task is to write a function named evaluate that takes a binary tree node as a parameter and returns the integer value of its subtree. If the node is a leaf, the function will return the number associated with the leaf. Otherwise the function will evaluate the left and right subtrees, perform the operation indicated by the node on the results returned by the left and right subtrees, and return this value. A binary tree node has the following declaration:
typedef struct bnode {
int type; // 0 for a leaf, 1 for an operator node
union {
char operator; // can be either '-' or '+'
int number;
};
struct bnode *left;
struct bnode *right;
} BNode;
If the type field indicates that the node is a leaf, then the number
field in the union will be set. If the type field indicates that the
node is an operator node, then the operator field in the union will be set.