Name:
112b-ax account:
The exam is open book: you may consult your notes, books, even computer programs that you have written before. But all the work should be yours.
Show all your work: you may get partial credit. Add extra sheets if necessary.
2244+*-32+*
struct SpecialNode{
int info;
SpecialNodePtr parent;
};
typedef SpecialNode* SpecialNodePtr;
main()
{
SpecialNode a1, a2, a3, a4;
SpecialNodePtr b;
b = &a1;
a1.info = 1; a2.info = 2; a3.info = 3; a4.info = 4;
a1.parent = new SpecialNode;
a2.parent = &a1;
a3.parent = &a2;
a4.parent = &a1;
b->parent->parent = NULL;
b->parent->info = 0;
}
struct BNode{
int a;
BNodePtr b;
BNodePtr c;
};
typedef BNode* BNodePtr;
and the following definitions of variables:
BNode n1, *n2, n3[5];
BNodePtr n4[10];
Which of the following is a valid C expression? (2 points each) Cross
the invalid expressions.
The depth of a node is the length of the longest path from the node
to a leaf which is descendent of the node.
The program should use the representation of a tree as a pointer
to a tree node, where a tree node is a C++ structure defined as:
struct TNode{
int data;
int depth;
TNodePtr left;
TNodePtr right;
}
typedef TNode* TNodePtr;
Name your function depth. It should take as argument a tree.
It should check to see whether the tree is empty (i.e. if it is the NULL
pointer). If not, it should look at the right and left subtrees, compute
the depths of all their nodes, then select the largest depth of its two
children and add one more unit to it. Using this outline, write the C++
code for the function depth.
Write down the vertices of the tree in preorder (2 points), inorder
(2 points) and postorder (2 points).