# Chapter 3

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

# Data Structures

### Stacks, Queues, and Lists

3.1. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string ${\displaystyle ((())())()}$ contains properly nested pairs of parentheses, which the strings ${\displaystyle )()(}$ and ${\displaystyle ())}$ do not. Give an algorithm that returns true if a string contains properly nested and balanced parentheses, and false if otherwise. For full credit, identify the position of the first offending parenthesis if the string is not properly nested and balanced.

3.2. Give an algorithm that takes a string ${\displaystyle S}$ consisting of opening and closing parentheses, say )()(())()()))())))(, and finds the length of the longest balanced parentheses in ${\displaystyle S}$, which is 12 in the example above. (Hint: The solution is not necessarily a contiguous run of parenthesis from ${\displaystyle S}$.)

3.3. Give an algorithm to reverse the direction of a given singly linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time.

3.4. Design a stack ${\displaystyle S}$ that supports S.push(x), S.pop(), and S.findmin(), which returns the minimum element of ${\displaystyle S}$. All operations should run in constant time.

3.5. We have seen how dynamic arrays enable arrays to grow while still achieving constant-time amortized performance. This problem concerns extending dynamic arrays to let them both grow and shrink on demand.
(a) Consider an underflow strategy that cuts the array size in half whenever the array falls below half full. Give an example sequence of insertions and deletions where this strategy gives a bad amortized cost.
(b) Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion.

3.6. Suppose you seek to maintain the contents of a refrigerator so as to minimize food spoilage. What data structure should you use, and how should you use it?

3.7. Work out the details of supporting constant-time deletion from a singly linked list as per the footnote from page 79, ideally to an actual implementation. Support the other operations as efficiently as possible.

### Elementary Data Structures

3.8. Tic-tac-toe is a game played on an ${\displaystyle n*n}$ board (typically ${\displaystyle n=3}$) where two players take consecutive turns placing “O” and “X” marks onto the board cells. The game is won if n consecutive “O” or ‘X” marks are placed in a row, column, or diagonal. Create a data structure with ${\displaystyle O(n)}$ space that accepts a sequence of moves, and reports in constant time whether the last move won the game.

3.9. Write a function which, given a sequence of digits 2–9 and a dictionary of ${\displaystyle n}$ words, reports all words described by this sequence when typed in on a standard telephone keypad. For the sequence 269 you should return any, box, boy, and cow, among other words.

3.10. Two strings ${\displaystyle X}$ and ${\displaystyle Y}$ are anagrams if the letters of ${\displaystyle X}$ can be rearranged to form ${\displaystyle Y}$ . For example, silent/listen, and incest/insect are anagrams. Give an efficient algorithm to determine whether strings ${\displaystyle X}$ and ${\displaystyle Y}$ are anagrams.

### Trees and Other Dictionary Structures

3.11. Design a dictionary data structure in which search, insertion, and deletion can all be processed in ${\displaystyle O(1)}$ time in the worst case. You may assume the set elements are integers drawn from a finite set ${\displaystyle 1,2,..,n}$, and initialization can take ${\displaystyle O(n)}$ time.

3.12. The maximum depth of a binary tree is the number of nodes on the path from the root down to the most distant leaf node. Give an ${\displaystyle O(n)}$ algorithm to find the maximum depth of a binary tree with ${\displaystyle n}$ nodes.

3.13. Two elements of a binary search tree have been swapped by mistake. Give an ${\displaystyle O(n)}$ algorithm to identify these two elements so they can be swapped back.

3.14. Given two binary search trees, merge them into a doubly linked list in sorted order.

3.15. Describe an ${\displaystyle O(n)}$-time algorithm that takes an ${\displaystyle n}$-node binary search tree and constructs an equivalent height-balanced binary search tree. In a height-balanced binary search tree, the difference between the height of the left and right subtrees of every node is never more than 1.

3.16. Find the storage efficiency ratio (the ratio of data space over total space) for each of the following binary tree implementations on n nodes:
(a) All nodes store data, two child pointers, and a parent pointer. The data field requires 4 bytes and each pointer requires 4 bytes.
(b) Only leaf nodes store data; internal nodes store two child pointers. The data field requires four bytes and each pointer requires two bytes.

3.17. Give an ${\displaystyle O(n)}$ algorithm that determines whether a given ${\displaystyle n}$-node binary tree is height-balanced (see Problem 3-15).

3.18. Describe how to modify any balanced tree data structure such that search, insert, delete, minimum, and maximum still take ${\displaystyle O(logn)}$ time each, but successor and predecessor now take ${\displaystyle O(1)}$ time each. Which operations have to be modified to support this?

3.19. Suppose you have access to a balanced dictionary data structure that supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in ${\displaystyle O(logn)}$ time. Explain how to modify the insert and delete operations so they still take ${\displaystyle O(logn)}$ but now minimum and maximum take ${\displaystyle O(1)}$ time. (Hint: think in terms of using the abstract dictionary operations, instead of mucking about with pointers and the like.)

3.20. Design a data structure to support the following operations:
insert(${\displaystyle x,T}$) – Insert item ${\displaystyle x}$ into the set ${\displaystyle T}$.
delete(${\displaystyle k,T}$) – Delete the ${\displaystyle k}$th smallest element from ${\displaystyle T}$.
member(${\displaystyle x,T}$) – Return true iff ${\displaystyle x\in T}$.
All operations must take ${\displaystyle O(logn)}$ time on an ${\displaystyle n}$-element set.

3.21. A concatenate operation takes two sets ${\displaystyle S_{1}}$ and ${\displaystyle S_{2}}$, where every key in ${\displaystyle S_{1}}$ is smaller than any key in ${\displaystyle S_{2}}$, and merges them. Give an algorithm to concatenate two binary search trees into one binary search tree. The worst-case running time should be ${\displaystyle O(h)}$, where ${\displaystyle h}$ is the maximal height of the two trees.

### Applications of Tree Structures

3.22. Design a data structure that supports the following two operations:
insert(${\displaystyle x}$) – Insert item ${\displaystyle x}$ from the data stream to the data structure.
median() – Return the median of all elements so far.
All operations must take ${\displaystyle O(logn)}$ time on an ${\displaystyle n}$-element set.

3.23. Assume we are given a standard dictionary (balanced binary search tree) defined on a set of ${\displaystyle n}$ strings, each of length at most ${\displaystyle l}$. We seek to print out all strings beginning with a particular prefix ${\displaystyle p}$. Show how to do this in ${\displaystyle O(mllogn)}$ time, where ${\displaystyle m}$ is the number of strings.

3.24. An array ${\displaystyle A}$ is called ${\displaystyle k}$-unique if it does not contain a pair of duplicate elements within ${\displaystyle k}$ positions of each other, that is, there is no ${\displaystyle i}$ and ${\displaystyle j}$ such that ${\displaystyle A[i]=A[j]}$ and ${\displaystyle |j-i|\leq k}$. Design a worst-case ${\displaystyle O(nlogk)}$ algorithm to test if ${\displaystyle A}$ is ${\displaystyle k}$-unique.

3.25. In the bin-packing problem, we are given ${\displaystyle n}$ objects, each weighing at most 1 kilogram. Our goal is to find the smallest number of bins that will hold the ${\displaystyle n}$ objects, with each bin holding 1 kilogram at most.
• The best-fit heuristic for bin packing is as follows. Consider the objects in the order in which they are given. For each object, place it into the partially filled bin with the smallest amount of extra room after the object is inserted. If no such bin exists, start a new bin. Design an algorithm that implements the best-fit heuristic (taking as input the ${\displaystyle n}$ weights ${\displaystyle w_{1},w_{2},...,w_{n}}$ and outputting the number of bins used) in ${\displaystyle O(nlogn)}$ time.
• Repeat the above using the worst-fit heuristic, where we put the next object into the partially filled bin with the largest amount of extra room after the object is inserted.

3.26. Suppose that we are given a sequence of ${\displaystyle n}$ values ${\displaystyle x_{1},x_{2},...,x_{n}}$ and seek to quickly answer repeated queries of the form: given ${\displaystyle i}$ and ${\displaystyle j}$, find the smallest value in ${\displaystyle x_{i},...,x_{j}}$.
(a) Design a data structure that uses ${\displaystyle O(n^{2})}$ space and answers queries in ${\displaystyle O(1)}$ time.
(b) Design a data structure that uses ${\displaystyle O(n)}$ space and answers queries in ${\displaystyle O(logn)}$ time. For partial credit, your data structure can use ${\displaystyle O(nlogn)}$ space and have ${\displaystyle O(logn)}$ query time.

3.27. Suppose you are given an input set ${\displaystyle S}$ of ${\displaystyle n}$ integers, and a black box that if given any sequence of integers and an integer ${\displaystyle k}$ instantly and correctly answers whether there is a subset of the input sequence whose sum is exactly ${\displaystyle k}$. Show how to use the black box ${\displaystyle O(n)}$ times to find a subset of ${\displaystyle S}$ that adds up to ${\displaystyle k}$.

3.28. Let ${\displaystyle A[1..n]}$ be an array of real numbers. Design an algorithm to perform any sequence of the following operations:
Add(${\displaystyle i,y}$) – Add the value ${\displaystyle y}$ to the ${\displaystyle i}$th number.
Partial-sum(${\displaystyle i}$) – Return the sum of the first ${\displaystyle i}$ numbers, that is, ${\displaystyle \sum _{j=1}^{i}A[j]}$.
There are no insertions or deletions; the only change is to the values of the numbers. Each operation should take ${\displaystyle O(logn)}$ steps. You may use one additional array of size ${\displaystyle n}$ as a work space.

3.29. Extend the data structure of the previous problem to support insertions and deletions. Each element now has both a key and a value. An element is accessed by its key, but the addition operation is applied to the values. The Partial_sum operation is different.
Add(${\displaystyle k,y}$) – Add the value ${\displaystyle y}$ to the item with key ${\displaystyle k}$.
Insert(${\displaystyle k,y}$) – Insert a new item with key ${\displaystyle k}$ and value ${\displaystyle y}$.
Delete(${\displaystyle k}$) – Delete the item with key ${\displaystyle k}$.
Partial-sum(${\displaystyle k}$) – Return the sum of all the elements currently in the set whose key is less than ${\displaystyle k}$, that is, ${\displaystyle \sum _{i.
The worst-case running time should still be ${\displaystyle O(nlogn)}$ for any sequence of ${\displaystyle O(n)}$ operations.

3.30. You are consulting for a hotel that has ${\displaystyle n}$ one-bed rooms. When a guest checks in, they ask for a room whose number is in the range ${\displaystyle [l,h]}$. Propose a data structure that supports the following data operations in the allotted time:
(a) Initialize(${\displaystyle n}$): Initialize the data structure for empty rooms numbered ${\displaystyle 1,2,...,n}$, in polynomial time.
(b) Count(${\displaystyle l,h}$): Return the number of available rooms in ${\displaystyle [l,h]}$, in ${\displaystyle O(logn)}$ time.
(c) Checkin(${\displaystyle l,h}$): In ${\displaystyle O(logn)}$ time, return the first empty room in ${\displaystyle [l,h]}$ and mark it occupied, or return NIL if all the rooms in ${\displaystyle [l,h]}$ are occupied.
(d) Checkout(${\displaystyle x}$): Mark room ${\displaystyle x}$ as unoccupied, in ${\displaystyle O(logn)}$ time.

3.31. Design a data structure that allows one to search, insert, and delete an integer ${\displaystyle X}$ in ${\displaystyle O(1)}$ time (i.e., constant time, independent of the total number of integers stored). Assume that ${\displaystyle 1\leq X\leq n}$ and that there are ${\displaystyle m+n}$ units of space available, where ${\displaystyle m}$ is the maximum number of integers that can be in the table at any one time. (Hint: use two arrays ${\displaystyle A[1..n]}$ and ${\displaystyle B[1..m]}$.) You are not allowed to initialize either ${\displaystyle A}$ or ${\displaystyle B}$, as that would take ${\displaystyle O(m)}$ or ${\displaystyle O(n)}$ operations. This means the arrays are full of random garbage to begin with, so you must be very careful.

### Implementation Projects

3.32. Implement versions of several different dictionary data structures, such as linked lists, binary trees, balanced binary search trees, and hash tables. Conduct experiments to assess the relative performance of these data structures in a simple application that reads a large text file and reports exactly one instance of each word that appears within it. This application can be efficiently implemented by maintaining a dictionary of all distinct words that have appeared thus far in the text and inserting/reporting each new word that appears in the stream. Write a brief report with your conclusions.

3.33. A Caesar shift (see Section 21.6 (page 697)) is a very simple class of ciphers for secret messages. Unfortunately, they can be broken using statistical properties of English. Develop a program capable of decrypting Caesar shifts of sufficiently long texts.

### Interview Problems

3.34. What method would you use to look up a word in a dictionary?

3.35. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval?

3.36. Write a function to find the middle node of a singly linked list.

3.37. Write a function to determine whether two binary trees are identical. Identical trees have the same key value at each position and the same structure.

3.38. Write a program to convert a binary search tree into a linked list.

3.39. Implement an algorithm to reverse a linked list. Now do it without recursion.

3.40. What is the best data structure for maintaining URLs that have been visited by a web crawler? Give an algorithm to test whether a given URL has already been visited, optimizing both space and time.

3.41. You are given a search string and a magazine. You seek to generate all the characters in the search string by cutting them out from the magazine. Give an algorithm to efficiently determine whether the magazine contains all the letters in the search string.

3.42. Reverse the words in a sentence—that is, “My name is Chris” becomes “Chris is name My.” Optimize for time and space.

3.43. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop.

3.44. You have an unordered array ${\displaystyle X}$ of ${\displaystyle n}$ integers. Find the array ${\displaystyle M}$ containing ${\displaystyle n}$ elements where ${\displaystyle M_{i}}$ is the product of all integers in ${\displaystyle X}$ except for ${\displaystyle X_{i}}$. You may not use division. You can use extra memory. (Hint: there are solutions faster than ${\displaystyle O(n^{2})}$.)

3.45. Give an algorithm for finding an ordered word pair (e.g. “New York”) occurring with the greatest frequency in a given webpage. Which data structures would you use? Optimize both time and space.

Back to Chapter List