Docsity

Homework 1 with Solution Key for Algorithms and Data Structures | CS 2420, Assignments of Computer Science

Toggle sidebar
bg1
CS 2420 Program 1 Summer 2008
Due June 18, 2008
This assignment assumes that you have written binary tree routines. If it has been several years since
you took CS2, you will 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 will store 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 postorder traversal in which each node knows the number of
children it has. So each line contains the node name and the number of children for the node.
The input (prog1.txt) looks like:
pf3

Partial preview of the text

Download Homework 1 with Solution Key for Algorithms and Data Structures | CS 2420 and more Assignments Computer Science in PDF only on Docsity!

CS 2420 Program 1 Summer 2008

Due June 18, 2008

This assignment assumes that you have written binary tree routines. If it has been several years since you took CS2, you will 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 will store 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 postorder traversal in which each node knows the number of children it has. So each line contains the node name and the number of children for the node. The input (prog1.txt) looks like:

2. 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

  1. Write the code to find the number of nodes which have a single child.
  • e
  • a
  • f
  • g
  • o
  • p
  • h
  • b
  • i
  • c
  • j
  • q
  • r
  • s
  • k
  • l
  • m
  • n
  • d
  • t