Name: ________________________________

Honor Code Acknowledgment: _____________________________


Random Quiz 7

CPS 100, Fall 1996

8 points

Due: November 12


Use the following definition of a node for a binary tree in Problems 1 and 2.

struct Node
{
   int info;
   Node * left;
   Node * right;

   Node(int i, Node * lf, Node * rt)
   {  info = i; left = lf; right = rt} 
};

Problem 1: How many only children? (4 pts)

Write a recursive function called NumOneChild that returns the number of nodes in a binary tree T that have exactly one child. For example, NumOneChild(T) on the tree T in Figure A returns 2, NumOneChild(T) on the tree T in Figure B returns 2, and NumOneChild(T) on the tree T in Figure C returns 4.

int NumOneChild(Node * T)
// postcondition: returns the number of Nodes in tree T with exactly one child
{

Problem 2: Expand a node (4 pts)

For this problem, assume the info values in a binary tree are unique. Write a recursive function called Expand(T,v) that looks for the node with info value "v" in tree T. If this node exists, it expands this node by adding two children with the same info value to this node. If the existing node previously had children, the old children become children of the new children (in the same direction).

For example, Expand(T,6) on the tree T in Figure A above results in the tree in Figure B. Expand(T,3) on the tree in Figure A above results in the tree in Figure C.

void Expand(Node * T, int val)
// precondition: info values in T are unique
// postcondition: if T contains a node with info value "val", this
//                node is expanded.
{