Algorithms and Data Structures

Fall 2003, Test 2


  1. (10 points) Using a stack, write a non-recursive routine which print a binary tree inorder. Use the ATD operations for stack and binary tree provided on the next sheet.

  2. (10 points) Describe how a binary tree can be implemented using an array. Give code for the functions isRoot(), parent(), leftChild() and rightChild(). Draw a binary tree with at least depth 2 and show how it would be stored in an array.

  3. (10 points) Given the following algorithm:
    Tree_Insert(T, e)
    (1)   y = NULL
    (2)   x = T.root()
    (3)   while x != NULL
    (4)      y = x
    (5)      if T.element(x) < e
    (6)         then x = T.leftChild(x)
    (7)         else x = T.rightChild(x)
    (8)   if y = NULL
    (9)      then T.insertRoot(e)
    (10)     else  if T.element(y) < e
    (11)          then  T.insertLeftChild(y,e)
    (12)          else  T.insertRightChild(y,e)
         

  4. (10 points) Describe the ADT queue. Discuss how the operations Enqueue, Dequeue and Front work. Describe a C++ structure that would support a queue baised upon dynamic memory. Draw a picture of your queue.