# Data-structures-TADM2E

# Data Structures

**Stacks, Queues, and Lists**

<br>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 <math>((())())()</math> contains properly nested pairs of parentheses, which the strings <math>)()(</math> and <math>())</math> 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.

<br>3-2. Write a program 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.

<br>3-3. 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.

- 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.
- Then, give a better underflow strategy than that suggested above, one that achieves constant amortized cost per deletion.

**Trees and Other Dictionary Structures**

<br>3-4. Design a dictionary data structure in which search, insertion, and deletion can all be processed in <math>O(1)</math> time in the worst case. You may assume the set elements are integers drawn from a finite set <math>{1,2,..,n}</math>, and initialization can take <math>O(n)</math> time.

<br>3-5. Find the overhead fraction (the ratio of data space over total space) for each of the following binary tree implementations on <math>n</math> nodes:

- All nodes store data, two child pointers, and a parent pointer. The data field requires four bytes and each pointer requires four bytes.
- Only leaf nodes store data; internal nodes store two child pointers. The data field requires four bytes and each pointer requires two bytes.

<br>3-6. Describe how to modify any balanced tree data structure such that search, insert, delete, minimum, and maximum still take <math>O(\log n)</math> time each, but successor and predecessor now take <math>O(1)</math> time each. Which operations have to be modified to support this?

<br>3-7. Suppose you have access to a balanced dictionary data structure, which supports each of the operations search, insert, delete, minimum, maximum, successor, and predecessor in <math>O(\log n)</math> time. Explain how to modify the insert and delete operations so they still take <math>O( \log n)</math> but now minimum and maximum take <math>O(1)</math> time. (Hint: think in terms of using the abstract dictionary operations, instead of mucking about with pointers and the like.)

<br>3-8. Design a data structure to support the following operations:

*insert(x,T)*-- Insert item <math>x</math> into the set <math>T</math>.*delete(k,T)*-- Delete the <math>k</math>th smallest element from <math>T</math>.*member(x,T)*-- Return true iff <math>x \in T</math>.

All operations must take <math>O(\log n)</math> time on an <math>n</math>-element set.

<br>3-9.
A *concatenate* operation takes two sets <math>S_1</math> and <math>S_2</math>, where every
key in <math>S_1</math> is
smaller than any key in <math>S_2</math>, and merges them together.
Give an algorithm to concatenate two binary search trees into
one binary search tree.
The worst-case running time should be <math>O(h)</math>, where <math>h</math> is the maximal
height of the two trees.

**Applications of Tree Structures**

<br>3-10.
In the *bin-packing problem*, we are given <math>n</math> metal
objects, each weighing between zero and one kilogram.
Our goal is to find the smallest
number of bins that will hold the <math>n</math> objects,
with each bin holding one 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 <math>n</math> weights <math>w_1,w_2,...,w_n</math> and outputting the number of bins used) in <math>O(n \log n)</math> time. - Repeat the above using the
*worst-fit heuristic*, where we put the next object in the partially filled bin with the largest amount of extra room*after*the object is inserted.

<br>3-11. Suppose that we are given a sequence of <math>n</math> values <math>x_1,x_2,...,x_n</math> and seek to quickly answer repeated queries of the form: given <math>i</math> and <math>j</math>, find the smallest value in <math>x_i,\ldots,x_j</math>.

- Design a data structure that uses <math>O(n^2)</math> space and answers queries in <math>O(1)</math> time.
- Design a data structure that uses <math>O(n)</math> space and answers queries in <math>O(\log n)</math> time. For partial credit, your data structure can use <math>O(n \log n)</math> space and have <math>O(\log n)</math> query time.

<br>3-12. Suppose you are given an input set <math>S</math> of <math>n</math> numbers, and a black box that if given any sequence of real numbers and an integer <math>k</math> instantly and correctly answers whether there is a subset of input sequence whose sum is exactly <math>k</math>. Show how to use the black box <math>O(n)</math> times to find a subset of <math>S</math> that adds up to <math>k</math>.

<br>3-13. Let <math>A[1..n]</math> be an array of real numbers. Design an algorithm to perform any sequence of the following operations:

*Add(i,y)*-- Add the value <math>y</math> to the <math>i</math>th number.*Partial-sum(i)*-- Return the sum of the first <math>i</math> numbers, i.e.

<math>\sum_{j=1}^i A[j]</math>. There are no insertions or deletions; the only change is to the values of the numbers. Each operation should take <math>O(\log n)</math> steps. You may use one additional array of size <math>n</math> as a work space.

<br>3-14.
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.
The addition operation is applied to the values, but the elements are
accessed by its key.
The *Partial-sum* operation is different.

*Add(k,y)*-- Add the value <math>y</math> to the item with key <math>k</math>.*Insert(k,y)*-- Insert a new item with key <math>k</math> and value <math>y</math>.*Delete(k)*-- Delete the item with key <math>k</math>.*Partial-sum(k)*--

Return the sum of all the elements currently in the set whose key is less than <math>y</math>, i.e. <math>\sum_{x_j<y} x_i</math>. The worst case running time should still be <math>O(n \log n)</math> for any sequence of <math>O(n)</math> operations.

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

**Implementation Projects**

<br>3-16. 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 word that is not found. Write a brief report with your conclusions.

<br>3-17.
A Caesar shift (see **cryptography**) 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**

<br>3-18. What method would you use to look up a word in a dictionary?

<br>3-19. Imagine you have a closet full of shirts. What can you do to organize your shirts for easy retrieval?

<br>3-20. Write a function to find the middle node of a singly-linked list.

<br>3-21. Write a function to compare whether two binary trees are identical. Identical trees have the same key value at each position and the same structure.

<br>3-22. Write a program to convert a binary search tree into a linked list.

<br>3-23. Implement an algorithm to reverse a linked list. Now do it without recursion.

<br>3-24. 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.

<br>3-25. You are given a search string and a magazine. You seek to generate all the characters in 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.

<br>3-26.
Reverse the words in a sentence---i.e., *My name is Chris* becomes
*Chris is name My.* Optimize for time and space.

<br>3-27. Determine whether a linked list contains a loop as quickly as possible without using any extra storage. Also, identify the location of the loop.

<br>3-28. You have an unordered array <math>X</math> of <math>n</math> integers. Find the array <math>M</math> containing <math>n</math> elements where <math>M_i</math> is the product of all integers in <math>X</math> except for <math>X_i</math>. You may not use division. You can use extra memory. (Hint: There are solutions faster than <math>O(n^2)</math>.)

<br>3-29.
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.