Vous êtes sur la page 1sur 6

Tutorial on Data & File Structures

Array

1. A sparse matrix can be represented as a binary matrix along with an element vector. How can you transpose it then? 2. Propose an efficient representation for a sparse matrix and write a function to transpose a matrix so represented. How can you make your transpose time efficient, may be at the cost of a little more space? 3. Triangular matrices actually take up half the space for a conventional representation. How can you use the empty space for another matrix? How do you then do such matrix operations as addition? 4. Arrays can be used to store polynomials. Propose a representation to hold more than one polynomial in one array and write a function to add two such polynomials. Apply this algorithm to add two large integers. 5. A 3-d array is declared as table[4][7][10] and is stored in row major order. The first element of the array is stored at address 101. Define the formula to calculate the address of any element table[i][j][k]. 6. Given an array write a recursive algorithm to invert it. Alternatively, how do you modify the following to access the ith element from the end of the array? for i = 1 to n x = a[i]

Stack & Queue

7. Define 50 circular queues of integer values with maximum size of 100 each. How is the front item of the 20th queue accessed? 8. Write algorithms using a stack to transform an expression from (i) prefix to postfix, (ii) postfix to prefix and (iii) infix to postfix. Dry run the algorithm on the algebraic expression (a + b) * d + e / (f + a * d) + c. 9. Write an algorithm to represent two stacks in one array, each growing from an end. 10. Write an algorithm to implement a stack of stacks. 11. Write an algorithm to evaluate an expression in (i) prefix and (ii) postfix. 12. Implement a circular queue. 13. Implement an input-restricted deque.

Linked list

14. Write a function to invert a linked list and dry run it on I -> n -> d -> i -> a and a -> b -> r -> a -> c -> a -> d -> a -> b -> r -> a. 15. Implement a linked list with insert-x-after-y, insert-x-before-y, delete-x among other usual functions. Dont use grandchild pointers. 16. What are the advantages of a circular linked list? How does it become more useful when tailpointed instead of being head-pointed?

17. Implement a string on a linked list of characters. What are the two very well known string search algorithms? 18. Implement a doubly linked list. 19. Implement a deque represented by a doubly linked list. 20. Write an algorithm for the linked list version of insertion sort.

Tree

21. A binary tree can be traversed in three different ways, namely, preorder, inorder and postorder traversals. Given one such traversal, can you reconstruct the tree? If you cant, would you like to try with a pair of these traversals? Would you prefer some pairs to others? You would like to give any reason here. 22. In terms of the number of elements what is the minimum time a comparison sort is going to take? Would you like to prove your ideas? 23. Define an AVL tree. Taking the calendar months (three characters), in sequence, construct an AVL tree. In which cases no rebalancing is required? What are the different rebalancing cases? 24. Given the following inorder and postorder sequences, construct the tree 1, 3, 5, 6, 4, 2, 7 and 6, 5, 4, 3, 7, 2, 1 . 25. Draw a height-balanced tree of height 4 with minimum number of nodes. Prove by induction that the minimum number of nodes in a height balanced tree of height h is Nh = Fh+2 1, h >= 0, where Fi is the ith Fibonacci number. 26. How do you delete nodes from a binary search tree? 27. A sorted list of the months, a BST of the same and the AVL tree are they equivalent? If so, where do they differ? 28. Static Huffman code is an excellent application of binary tree. Given the following table construct Huffman codes for the characters
Character a Frequency 10 e 15 I 12 o 3 u 4 space 13 newline 1

Remember MSB of a code is at the leaf and its LSB is at the root. 29. Draw the game tree for a six stick game of Nim. 30. What is a B tree? How is it related to 2-3 tree? 31. Given the following inorder and postorder sequences, construct the tree 10, 60, 75, 80, 82, 83, 84, 85, 90, 120 and 83, 82, 84, 80, 85, 75, 90, 60, 120, 10. 32. Given an expression tree write an algorithm to evaluate it and dry run it on (a + b) * d + e / (f + a * d) + c where a = 10, b = 0, c = 20, d = 0, e = -10, f=0. 33. How many binary trees with n nodes can be constructed? In how many ways can a matrix expression M1 * M2 * * Mn or a scalar expression a1*a2*an be evaluated? 34. Prove that preorder traversal on a tree and preorder traversal on the associated binary tree gives the same result. 35. Which of the following is true or false? (i) In a binary tree the node x is a descendant of node y if and only if x follows y in preorder. (ii) In a binary tree the node x is a descendant of node y if and only if x follows y in preorder and precedes y in inorder. (iii) The leaves in a binary tree occur in the same relative order in preorder, inorder and postorder.

36. Which of the following is true or false? (i) An ordered list is an AVL tree. (ii) An ordered list is a binary search tree. (iii) A binary search tree is an AVL tree. (iv) An AVL tree is a binary search tree. (v) Different input sequences of the same data set give different binary search trees. (vi) Inorder sequences of binary search trees of different permutations of the same data set are different. (vii) An AVL tree is an ordered list. (viii) Inorder sequence of a binary search tree is an ordered list. 37. Construct an AVL tree from the sequence Dec, Nov, Oct, Sep, Aug, Jul, Jun, May, Apr and Mar. 38. Given the following preorder and inorder sequences, draw the binary tree {a, b, c, d, f, g, h, i} and {c, b, d, a, h, g, f, i}. 39. What is a trie structure? How is it useful in data searching and retrieval? 40. Draw the 2-3 tree which results from inserting 3, 1, 4, 5, 9, 2, 6, 8, 7, 0 into an empty 2-3 tree. Show the result of deleting 0 and then 9 from the 2-3 tree. 41. Into the 2-3 tree
30 2 4

10, 15

25

35

45, 50

Insert, in sequence, 38, 55, 37, 5, 18 and 12. 42. From the 2-3 tree
50 30

60, 70

10

40

55, 58

65

75, 80

delete 58, 65, 55, 40, 50 in sequence.

Sorting & Searching

43. Write a function for binary insertion sort with a boundary sentinel. 44. What is shaker sort and how is it useful? Combining it with flagged bubble sort will be useful? 45. Write a function for the Shell sort algorithm. On which basic sorting method it is designed? 46. What is a heap? Develop a heap sort function and then give a time space complexity analysis of the function. 47. Between LSD radix sort and MSD which one is simpler and why? Is there any exception? 48. Which sorting makes only 5 comparisons for a set of 4 data? 49. Sorting methods are of two types array sorting and file sorting. How is merge sort used for file sorting? What is the tree of losers? 50. Consider an array with n elements where n 1 elements are sorted. There is only one element which is known to be out of place and its position is known. Which sorting method will you use to bring it to its proper position and why? What will be the time complexity? Also state how can this be done using quick sort. 51. Make a comparison of sorting methods insertion sort, selection sort, bubble sort and quick sort considering the different variants of each method. 52. Sort the following by LSD radix-10 sort 26, 5, 77, 1, 61, 11, 59, 15, 48 and 90. 53. Show the steps involved to sort the following sequence using quick sort 15, 30, 14, 32, 60, 24, 8, 23, 10, 31. 54. Draw the binary heap resulting for insertion of 10, 12, 1, 14, 6, 5 and 3 one after another into an (initially) empty binary heap. 55. What is the best case time complexity for insertion sort? Justify your answer with examples. 56. Show that quick sort takes O(n2) time when the input is already in order. 57. Construct the initial heap for 15, 23, 12, 56, 21 and 10. 58. Three out of the following six statements are wrong. Identify these three statements and give the correct form in each case by least possible modification of the corresponding statement i. Worst case time for sorting n elements using quick sort is O(n2). ii. Worst case time for sorting n elements by heap sort is O(nlog2n). iii. Worst case time for quick sort is realized when the middle element is always chosen as pivot. iv. The total number of comparisons performed by selection sort to sort n elements is O(n(log2n) 2). v. Bubble sort has a worst case time of O(n2) when n elements are to be sorted.

vi.

Radix sort takes one element at a time from the input sequence and places it in sorted order in the output sequence by comparing the element with all other elements already present in the output sequence. 59. Write an algorithm for address calculation sort for data occurring multiple number of times. 60. Investigate stability of array sorting methods. 61. Explain tournament sort with reference to k-way merge sort.

Hashing

62. How is hashing different from normal table search methods? Why does the following algorithm produce a biased, though popular hash function?

int transform(char *key){ /* simple additive approach to create a natural number within the integer range */ int number = 0; while (*key) number += *key++; return number; } int hash(char *key){ /* transform key to a natural number and return this result modulus the table size */ return(transform(key) % TABLE_SIZE); } Run this algorithm on a 13-bucket table with one slot per bucket for the following data: for, do, while, if, else and function with linear probing for resolving overflows.

63. kleckleckleklc 64. eckljeklcjkflecjklfjcklfjlk


Graphs

65. ghjghjgjgjgjgjgjhghjhhhhhhhhhhhhhh 66. eeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 67. wwwwwwwwwwwwwwwwwwwwwwwwwww


File structures

68. ssssssssssssssssssssssssss 69. eeeeeeeeeeeeeeeeeeeeeeeeeeee 70. rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr 71. dddddddddddddddddddddddddddddddddddddddddd

Vous aimerez peut-être aussi