Docsity

Solutions for Program 1 - Algorithms and Data Structures - Fall 2008 | CS 2420, Assignments of Computer Science

Toggle sidebar
bg1
Program 1 Page 1
CS2420 – Fall 2008
Program 1
Due September 8, 2008
This assignment assumes that you have written binary tree routines. If it has been several years since
you took CS 1410, you need to get started immediately.
A general tree is a tree in which the number of children is not restricted to two. The problem with a
general tree is that if we use an array to store the children pointers, we need to know a maximum
number of children.
A more flexible solution (and one that requires less pointer space) stores the tree as a “left most child”
and “next right sibling.” Instead of storing pointers to all children, a node only stores a pointer to it’s
leftmost child. That child stores a pointer to its next right sibling. See the example below.
For this assignment, you are to complete the following:
1. Create the above tree from a preorder traversal in which each node knows the number of
children it has. The input (prog1.txt) looks like where the letter is the name of the node and the
number is the number of children:
pf3

Partial preview of the text

Download Solutions for Program 1 - Algorithms and Data Structures - Fall 2008 | CS 2420 and more Assignments Computer Science in PDF only on Docsity!

CS 2420 – Fall 2008

Program 1 Due September 8 , 2008 This assignment assumes that you have written binary tree routines. If it has been several years since you took CS 1410, you need to get started immediately. A general tree is a tree in which the number of children is not restricted to two. The problem with a general tree is that if we use an array to store the children pointers, we need to know a maximum number of children. A more flexible solution (and one that requires less pointer space) stores the tree as a “left most child” and “next right sibling.” Instead of storing pointers to all children, a node only stores a pointer to it’s leftmost child. That child stores a pointer to its next right sibling. See the example below. For this assignment, you are to complete the following:

  1. Create the above tree from a preorder traversal in which each node knows the number of children it has. The input (prog1.txt) looks like where the letter is the name of the node and the number is the number of children:

t 4 a 1 e 0 b 3 f 0 g 0 h 2 o 0 p 0 c 1 i 0 d 5 j 0 k 3 q 0 r 0 s 0 l 0 m 0 n 0

  1. Print out the tree prettily, in preorder, with each node on a line by itself and indented to show level. The above tree would look like: t a e b f g h o p c i d j k q r s l m n
  2. Write the code to print the height of the tree.
  3. Write the code to print the level (depth) that has the maximum number of nodes (the root has depth 0).
  4. Write the code to create the mirror image of the tree (the first child becomes the last child, recursively). Print the reversed tree.