Assignment 1 Solutions | Algorithms and Data Structures | CS 303, Assignments of Computer Science

Material Type: Assignment; Class: Algorithms and Data Structures; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 04/12/2010

koofers-user-7bw-1
koofers-user-7bw-1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ASSIGNMENT #1 SOLUTION
Fill in the Blank.
1. Name: (1 point)
2. I have satisfied the prerequisites of CS 303 Algorithms and Data Structures and CS 350
Formal Languages and Automata, or equivalent, each with a grade of C or better.
Signature: (2 points)
True or False.
For each of the problems below, indicate whether it is true or false. Justify your answer in either
case. (3 points each)
3. A context-free grammar defining a deterministic context-free language may always be con-
verted into a deterministic pushdown automaton.
False. The algorithm which converts a CFG into a PDA will always produce a nondetermin-
istic PDA.
4. The context-free grammar below is ambiguous.
Sโ†’if (e) S|if (e) Selse S|s
True. The following are two distinct left-most derivations of the same string.
Sโ‡’if (e) Sโ‡’if (e) if (e) Selse Sโ‡’if (e) if (e) selse Sโ‡’if (e) if (e) selse s
Sโ‡’if (e) Selse Sโ‡’if (e) if (e) Selse Sโ‡’if (e) if (e) selse Sโ‡’if (e) if (e) selse s
5. It is not possible to implement a tree data structure where interior nodes have an arbitrary
number of children (e.g. some nodes may have 1 child, some 2, some 3, etc.).
False. The children may be represented by a list.
Short Answer.
6. Assume a binary tree object with functions value,left and right to return the value of
the current node, left subtree, and right subtree, respectively. Using an actual programming
language or pseudo-code, write a function postorder to print the values of all nodes in the
tree using a post-order traversal. Please write your answer on back. (3 points)
void postorder () {
if (left () != null) left () . postorder ();
if (right () != null) right () . postorder ();
System . out . print (value ());
}

Partial preview of the text

Download Assignment 1 Solutions | Algorithms and Data Structures | CS 303 and more Assignments Computer Science in PDF only on Docsity!

ASSIGNMENT #1 SOLUTION

Fill in the Blank.

  1. Name: (1 point)
  2. I have satisfied the prerequisites of CS 303 Algorithms and Data Structures and CS 350 Formal Languages and Automata, or equivalent, each with a grade of C or better.

Signature: (2 points)

True or False.

For each of the problems below, indicate whether it is true or false. Justify your answer in either case. (3 points each)

  1. A context-free grammar defining a deterministic context-free language may always be con- verted into a deterministic pushdown automaton. False. The algorithm which converts a CFG into a PDA will always produce a nondetermin- istic PDA.
  2. The context-free grammar below is ambiguous. S โ†’ if (e) S | if (e) S else S | s True. The following are two distinct left-most derivations of the same string. S โ‡’ if (e) S โ‡’ if (e) if (e) S else S โ‡’ if (e) if (e) s else S โ‡’ if (e) if (e) s else s S โ‡’ if (e) S else S โ‡’ if (e) if (e) S else S โ‡’ if (e) if (e) s else S โ‡’ if (e) if (e) s else s
  3. It is not possible to implement a tree data structure where interior nodes have an arbitrary number of children (e.g. some nodes may have 1 child, some 2, some 3, etc.). False. The children may be represented by a list.

Short Answer.

  1. Assume a binary tree object with functions value, left and right to return the value of the current node, left subtree, and right subtree, respectively. Using an actual programming language or pseudo-code, write a function postorder to print the values of all nodes in the tree using a post-order traversal. Please write your answer on back. (3 points)

void postorder () { if (left () != null) left (). postorder (); if (right () != null) right (). postorder (); System. out. print (value ()); }