Project 4: Haskell Project

Spatial Structures

Computer graphics, image processing, and GIS (geographic information systems) all make use of a data structure called a quadtree. Quadtrees represent regional or block data efficiently and support efficient algorithms for operations like the union and intersection of images.

A quadtree for a black and white image is constructed by successively dividing the image into four equal quadrants. If all the pixels in a quadrant are the same color (all black or all white) the division process for that quadrant stops. Quadrants that contain both black and white pixels are subdivided into four equal quadrants and this process continues until each subquadrant consists of either all black or all white pixels. It is entirely possible that some subquadrants consist of a single pixel.

For example, using 0 for white and 1 for black, the region on the left below is represented by the matrix of zeros and ones in the middle. The matrix is divided into subquadrants as shown on the right where gray squares represent subquadrants that consist entirely of black pixels.

image pixels image ones and zeros image blocks

A quadtree is constructed from the block structure of an image. The root of the tree represents the entire array of pixels. Each non-leaf node of a quadtree has four children, corresponding to the four subquadrants of the region represented by the node. Leaf nodes represent regions that consist of pixels of the same color and thus are not subdivided. For example, the image shown above, with the block structure on the right, is represented by the quadtree below.

quad tree
Leaf nodes are white if they correspond to a block of all white pixels, and black if they correspond to a block of all black pixels. In the tree, each leaf node is numbered corresponding to the block it represents in the diagram above. The branches of a non-leaf node are ordered from left-to-right as shown for the northwest, northeast, southwest, and southeast quadrants (or upper-left, upper-right, lower-left, lower-right).

A tree can be represented by a sequence of numbers representing the root-to-leaf paths of black nodes. Each path is a base five number constructed by labeling branches with 1, 2, 3, or 4 with NW = 1, NE = 2, SW = 3, SE = 4, and with the least significant digit of the base five number corresponding to the branch from the root. For example, the node labeled 4 has path NE, SW which is 325 (base 5) or 1710 (base 10); the node labeled 12 has path SW, SE or 435 = 2310; and the node labeled 15 has path SE, SW, NW or 1345 = 4410. The entire tree is represented by the sequence of numbers (in base 10)

9 14 17 22 23 44 63 69 88 94 113
Here are the corresponding base 5.
14 24 32 42 43 134 223 234 323 334 423
Here are the corresponding paths.
[41] [42] [23] [24] [34] [431] [322] [432] [323] [433] [324]
A tree can be represented by a sequence of numbers representing the root-to-leaf paths of black nodes. Each path is a list of characters constructed by labeling branches with 'a', 'b', 'c', or 'd' with NW = 'a', NE = 'b', SW = 'c', SE = 'd'. For example, the node labeled 4 has path NE, SW which is "bc"; the node labeled 16 has path SE, SW, NE which "dcb". All the black nodes in the figure can be represented by the list:
"bc." "bd." "cbb." "cbc." "cbd." "cd." "da." "db." "dca." "dcb." "dcc."

All images consist of black and white pixles. All images are square and the size is a power of two, e.g., 4x4, 8x8, 16x16. Write a function that converts images into root-to-leaf paths and a function that converts root-to-leaf paths into images.


The input contains one or more images. Each image is square, and the data for an image starts with an integer n, where l=|n| is the length of a side of the square (always a power of two, with l ≤ 64) followed by a representation of the image. A representation is either a sequence of n2 zeros and ones comprised of l lines of l digits per line, or the sequence of letters that represent the root-to-leaf paths of each black node in the quadtree that represents the image. The sequence ends with a senteniel value of -1. (Even the empty sequence has -1 following it.)

If n is positive, the zero/one representation follows; if n is negative, the sequence of black node paths follows. A one-node tree that represents an all-black image is represented by the number 0. A one-node tree that represents an all-white image is represented by an empty sequence.

The end of data is signaled by a value of 0 for n.


For each image in the input, first output the number of the image, the size of the image, the number of black nodes in the quad tree, and the number of black pixels, as shown in the sample output.

Then, if the image is represented by zeros and ones, output the root-to-leaf paths of all black nodes in the quadtree that represents the image. The values should be base 10 representations of the base 5 path numbers, and the values should sorted numerically when printed. Print the all the numbers on a single line.

If the image is represented by the root-to-leaf paths of black nodes, the output consists of an US-ASCII representation of the image with the character '.' used for white/zero and the character '*' used for black/one. There should be n characters per line for an n×n image.

Sample Input

9 14 17 22 23 44 63 69 88 94 113 -1
0 -1

Output for the Sample Input

Image 1:  size = 8, black nodes = 11, black pixels = 26
9 14 17 22 23 44 63 69 88 94 113

Image 2:  size = 8, black nodes = 11, black pixels = 26

Image 3:  size = 2, black nodes = 0, black pixels = 0
The empty list

Image 4:  size = 4, black nodes = 1, black pixels = 16


This problem is adapted from the 1998 ICPC World Finals, Problem G.

Information on the net

Be sure to eliminate all warnings detected by -Wall before submitting your project.

The following is a sample main program.

module Main where

import qualified System.IO as IO

-- main program to reverse each line of input
main = interact (unlines . map f . lines)

f line = reverse line

   built-in functions:
   -- unlines :: [String] -> String ; combines separate lines into one string
   --  lines  :: String -> [String] ; breaks input into separate lines
   -- reverse :: [a] -> [a]         ; reverse list back to front

Turning it in

Turn in the source code for your project in a file named main.hs. Your project will be compiled like this:

ghc -Wall --make main.hs
The compiler is:
ghc -Wall --version
The Glorious Glasgow Haskell Compilation System, version 6.4.1

Write the program as clearly as possible, including reasonable comments. You may work alone or with one other student in the class, as long as you did not work with this partner in any of the previous projects. It is important to get the output formatted correctly, as the output must conform character by character to the specification. Incorrectly formatted output will fail the test cases. If there is any question about the output, please ask.

Turn in the Haskell source code for the program using the submission server. The project is proj4. In the comments somewhere at the beginning of your source file include a header like the following:

 - Author: name1, e-mail address
 - Author: name2, e-mail address
 - Course: CSE 4520, Fall 2017
 - Project: Spatial Structures