EECS 360 - Test 1 Answers

1a. What is the worst case time complexity (Big-O) for insertion into a stack? Briefly explain why your answer is true. (Formal proofs are NOT required.) (6 points)

O(1). In a stack the insert always occurs at the beginning of the list which is accessible in constant time.

1b. What is the worst case time complexity (Big-O) for insertion into a binary search tree? Briefly explain why your answer is true. (Formal proofs are NOT required.) (7 points)

O(n). In a tree, the worst case runtime is dependent on the height of the tree. Since a binary search tree is not guarenteed to be balanced in any way, the worst case height of a tree with n nodes is n-1. Therefore, the worst case run time for insert is O(n).

1c. What is the worst case time complexity (Big-O) for insertion into an AVL tree? Briefly explain why your answer is true. (Formal proofs are NOT required.) (7 points)

O(log n). In a tree, the worst case run time is dependent on the hieght of the tree. Since an AVL tree is guarenteed to be balanced, the worst case height of a tree with n is (approximately) log n. Therefore, the worst case run time for insert is O(log n).

2. Draw the proper expression tree for the following infix expression.

		5 + 3 * (2 + 7 / 4) * 8 - 1

                          (-)
                         /   \
                      (+)     (1)
                     /   \
                  (5)     (*)
                         /   \
                      (*)     (8)
                     /   \
                  (3)     (+)
                         /   \
                      (2)     (/)
                             /   \
                          (7)     (4)

3. For the given binary search tree, delete the value of 35 from the tree. Re-draw the tree with the value deleted. (10 points)

				35
                               /  \
                 +-------------    -------------+
		20				50
               /  \                            /  \
         +-----    -----+                +-----    -----+
	10		30		40		60
       /  \                               \            /  \
     +-    -+                              -+       +--    --+
     5	   15        			   45      55	    65

Answer:
				40
                               /  \
                 +-------------    -------------+
		20				50
               /  \                            /  \
         +-----    -----+                +-----    -----+
	10		30		45		60
       /  \                                            /  \
     +-    -+                                       +--    --+
     5	   15        			           55	    65

4. For the given AVL tree, insert the value of 18 into the tree. re-draw the tree with the new value inserted. (10 points)

				35
                               /  \
                 +-------------    -------------+
		20				50
               /  \                            /  \
         +-----    -----+                +-----    -----+
	10		30		40		60
       /  \                               \            /  \
     +-    -+                              -+       +--    --+
     5	   15        			   45      55	    65

Answer:
				35
                               /  \
                 +-------------    -------------+
		15				50
               /  \                            /  \
         +-----    -----+                +-----    -----+
	10		20		40		60
       /               /  \               \            /  \
     +-              +-    --+             -+       +--    --+
     5	            18      30             45      55	    65

5. Write a function that will return the height of a node in a binary search tree. (Caution: do NOT confuse height and depth of a node!!) The function is to return an integer value and take two parameters. The first parameter is a pointer to the root of the tree. The second parameter is the value stored in the node to be found. If the value does not exist in the tree, return the value of -1. You may assume that no value is duplcated in the tree. Use the following structure and typedef for your program. (25 points)

	struct bin_node
	{
	   int 	elem;
	   struct bin_node *left;
	   struct bin_node *right;
	};

	typedef struct bin_node *bin_ptr;

Answer:

int height (bin_ptr t)
{
 if (T == NULL)
    return -1
 else
    return 1 + max(height(t->left), height(t->right));
}

int getheight(bin_ptr t, int key)
{
 if (t == NULL)
   return -1;
 else if (t->elem == key)
   return height (t);
 else if (key < t->elem)
   return getheight (t->left, key);
 else
   return getheight (t->right, key);
}

6. Write a function the will return a true value if one node is the ancestor of another node in a generic tree and false otherwise. The function is to return a "boolean" value and take three parameters. The first parameter is a pointer to the root of a tree. The second parameter is the value of supposed ancestor. The third parameter is the value of the supposed descendent. The tree uses the first-child-next-sibling structure. A generic tree means that the nodes are not ordered in any way. You may assume that no value is duplicated in the tree. Hint: a find node algorithm may be useful. Use the following structure and typedef for your program. (25 points)

	struct cs_node
	{
	   int elem;
	   struct cs_node *first_child;
	   struct cs_node *next_sibling;
	};

	typedef struct cs_node *cd_ptr;

Answer:

cd_ptr cs_find (cd_ptr t, int key)
{
 cd_ptr temp;

 if (t == NULL || t->elem == key)
    return t;
 else
    {
     temp = cs_find (t->first_child, key);
     if (temp == NULL)
        temp = cd_find(t->next_sibling, key);
     return temp;
    }
}

bool is ancestor (cd_ptr t, in ans, int des)
{
 cd_ptr temp;

 temp = cs_find (t, ans);

 if (temp == NULL)
    return false;

 if (ans == des)
   return true;

 if (cd_find (temp->first_child, des) == NULL)
    return false;
 else
    return true;
}