The purpose of this quiz is to give you a chance to focus your knowledge of simple tree data structures in C++.
A tree data structure has a root, branches, and leaves — just like in the real world. But the data structure also has nodes and is represented upside down from their real-world cousins.
Such a structure also uses lingo from our inheritance days with terms like _____ and _____.
The invariant (property that is the same for EVERY node) for a binary search tree is:
The invariant (property that is the same for EVERY node) for a heap is:
Show recursive code for an in-order traversal of a binary tree. Make it a template with a function parameter to act on each node.
template <typename TreeT, typename FuncF> void in_order(TreeT & pN, FuncF & f) { if (notNull(pN)) { in_order(pN->Left(), f); f(pN->Data()); in_order(pN->Right(), f); } return; }
Given the following tree data structure:
H | +------+------+ | | V D | | +--+ +--+--+ | | | Y F B
Label each of the following as an IN-order traversal, a PRE-order traversal, a POST-order traversal, or NOT one of these standard traversal patterns.
Traversing a binary search tree in order will process the data involved in sorted order.
Although not quite as simple, a heap data structure can also be used to effect a sorting technique on the contained data.
TRUE ✓ | FALSE ✗ | An expression tree is a way to represent a mathematical expression in tree form. | ||
---|---|---|---|---|
TRUE ✓ | FALSE ✗ | The relative levels of operators tell the precedence of them in the expression's evaluation. | ||
TRUE ✗ | FALSE ✓ | Lower operators are evaluated later than higher operators. |
Evaluation of an expression tree can be effected with a simple post-order traversal of the tree. Or we could transform the expression into post-fix order with a similar traversal and evaluate that instead. Such an evaluation would require a helper stack to accomplish.