Data Structures by D Samantha PDF

You might also like

Download as pdf
Download as pdf
You are on page 1of 167
Rs. 395.00 CLASSIC DATA STRUCTURES, 2nd ed. (With CD-ROM) Debasis Samanta © 2009 by FHI Learning Private Limited, New Delhi. All rights reserved. No part of this book may be reproduced in any form, by mimeograph or any other means, without permission in writing from the publisher. Warning and Disclaimer The author and the publisher are not responsible for any loss or damage arising from the usa of CD-ROM included wth this book. The CD-ROM included with this book has no commercial value and cannot be sold coparately. ISBN-978-81-203-9731-2 The export rights of this book are vested solely with the publisher. Seventeenth Printing (Second Edition) ~ - July, 2009 Published by Asoke K. Ghosh, PHI Leaming Private Limited, M-97, Connaught Gircus, New Delhi-1 10001 and Printed by Rajkamal Electric Press, Plot No. 2, Phase-IV, Kundli, Haryana. Contents Preface iti Preface to the First Edition av = 1.2 Concept of Data Structures 4 L3__Overview of Data Structures _6 1.4 Implementation of Data Structures 1.5 Organization of the Book 70 Arrays 12-35 - 2 22 Terminology 13 2.3 One-Dimensional Array 14 2.3.1 Memory Allocation for an Array 14 2.3.2 Operations on Arrays 15 2.3.3 Application of Arrays 21 in Contents Vii 45 Applications of Stacks. HI 4.5.1 Evaluation of Arithmetic Expressions i 45.2 Code Generation for Stack Machines _121. 4.5.3 Implementation of Recursion 123 5 Fnctorial C: 7 Ds 4.5.5 Quick Sort 128 45.6 Tower of Hanoi Problem 133 4.5.7 Activation Record Management 136 4.6 Problems to Ponder 150 References 152 Queues 153-188 : 34 —Inuoduction 153 Delinits iss 5.3 Representation of Queues 156 5.3.1 Representation of a Queue using an Array 156 5.3.2 Representation of a Queue using a Linked List 159 $4 Various Queue Structures 160 3.4.1 Circular Queue 160 54.2 Deque 164 5.43 Priority Queue 167 5.5 Applications of Queues 172 5.5.1 Simulation __172 5.5.2 CPU Scheduling in_a Multiprogramming Environment 183 5.53 Round Robin Algorithm _/85 $.6__Problems to Ponder 187 References _188 Tables. 189-211 6.1__Rectangular Tables 190 6.2 Jagged Tables 190 6.3 Inverted Tables _ 193 64 Hash Tables __[94 641 Hashing Techniques 194 64.2 Collision Resolution Techniques 199 64.3 Closed Hashing 200 644 Open Hashing 205 645 Comparison of Collision Resolution Techniques 207 6.5 __Problems to Ponder _2/0 References 211 viii Contents L_Trees 212-415 7.1 Basic Terminologies 214 7.2 Definition and Concepts 216 7.2.1 Binary Trees 217 7.2.2 Properties of a Binary Tree 2/8 7.3 Representations of Binary Tree 222 Linear Representation of a Binary Tree 223 Linked Representation of a Binary Tree 226 Physical Implementation of a Binary Tree in Memory 228 74 Operations on a Binary Tree 230 14.1___Insertion __230 1.4.2___Deletion _234 1.43 Traversa 232 744 Merging together Two Binary Trees 248 1.5 Types of Binary Trees 249 7.5.1 Expression Tree 250 7.5.2 Binary Search Tree 254 7.5.3____Heap Trees 266 Threaded Binary Trees 276 7.5.5 Height Balanced Binary Tree. 289 15.6 —Red-black Tree __306 15.7 Splay Tree __333 75.8 Weighted Binary Tree 351 759 Decision 7 16 Trees and Forests 366 7.6.1 Representation of Trees 367 L7_B Trees _ 375 1.7.1 __B Tree Indexing 376 172 rations ona B Tree 377 1.17.3 Lower and Upper Bounds of a B Tree 400 78 B+ Tree Indexing 401 19 ___Trie Tree Indexing _ 403 19.1 Trie Structure 404 192 erations on Trie___405 7.9.3 Applications of Tree Indexing 408 Problems to Ponder 4/0 References 414 8. Graphs 416-494 $1 Introduction _46 8.2 Graph Terminologies 418 Contents AX. 10. 8.3 Representation of Graphs 422 8.3.1 Set Representation 423 8.3.2 Linked Representation 424 8.3.3. Matrix Representation 425 84 Operations on Graphs 437 84.1 Operations on Linked List Representation of Graphs 437 84. Operations an Matrix Representation of Graphs 444 8.5 Application of Graph Structures 454 8.5.1 Shortest Path Problem _45¢6 8.5.2 Topological Sorting 466 8.5.3 Minimum Spanning Trees 470 8.5.4 Connectivity ina Graph 477 Pr ‘uilicnion Clea 8.6 BDD and Its Applications 487 Convers = A 8 $.6.2 Applications of BDD 490 8.7 Problems to Ponder 492 References 494 Sets. 495-527 9.1 Definition and Terminologies 497 9.2 Representation of Sets 498 9.21 Linked List Representation of Set 498 9.2.2 Hash Table Representation of Sets 499 9.2.3 Bit Vector Representation of Sets 499 924 Tree Representation of Sets 500 9.3 Operations of Sets S506 9.3.1 Operations on List Representation of Set 506 9.3.2 Operations on Hash Table Representation of Set Si 9.3.3 Operations on Bit Vector Representation of Set. $13 934 Operation on Tree Representation of Set S17 9.4 Applications of Sets S2t 941 Spelling Checker J2t 9.4.2 Information System using Bit Swings 522 2s mation Sten oe 9.5 Problems to Ponder $26. References $27 Sorting 528-711 10.1_Basic Terminologies 529 10.2 Sorting Techniques 53 Classic Data Structures With the built-in data types, programming languages provide users with a lot of advantages of processing of various types of data, For example, if'a user declares a variable of type Real (say), then several things are automatically implied, such as how to store a value for that variable, what are the different operations possible on that type of data, what amount of memory is required to store, cic, All these things are taken care of by the compiler or the run-time system manager. Abstract data type When an application requires a special kind of data which is not available as a built-in data type, then it is the programmer's responsibility to implement his own kind of data. Here, the programmer has to specify how to store a value for that data, what are the operations that can meaningfully manipulate variables of that kind of data, amount of memory required to store a variable. The programmer has to decide all these things and accordingly implement them. Programmers’ own data type is termed abstract data type. The abstract data type is also alternatively termed user-defined data type. For example, suppose we want to process dates of the form dd/mm/yy. For this, no built-in data type is known in C, FORTRAN, and Pascal. If 4 programmer wants to process dates, then an abstract data type, say Date, has to be devised and various operations such as adding a few days to a date to obtain another date, finding the days between two dates, obtaining a day for a given date, etc, have to be defined accordingly. Besides these, programmers should decide how to store the data, what amount of memory will be needed to represent a value of Date, etc. An abstract data type, in fact, can be built with the help of built-in data types and other abstract data type(s) already built by the programmer. Some programming languages provide a facility to build abstract data types easily. For example, using, struct/class in C/C++, and using record in Paseal, programmers can define their own data types. 1.2 CONCEPT OF DATA STRUCTURES A digital computer can manipulate only primitive data, that is, data in terms of Os and 1s. Manipulation of primitive data is inherent within the computer and does not require any extra effort on the part of the user. But in our real-life applications, various kinds of data other than the primitive data are involved. Manipulation of real-life data (also termed user data) requires the following essential tasks: 1. Storage representation of user data: User data should be stored in such a way that the computer can understand it. 2. Retrieval of stored data: Data stored in a computer should be retrieved in such a way that the user can understand it, 3. Transformation of user data: Various operations which require to be performed on user data so that it can be transformed from one form to another. The basic theory of computer science deals with the manipulation of various kinds of data, wherefrom the concept of data structures comes in. In faet, data structures constitute the fundamentals of computer science. For a given kind of user data, its structure implies the following: Introduction and Overview 5 1. Domain (2): This is the range of values that the data may have. This domain is also termed data object. 2. Function (Z): This is the set of operations which may legally be applied to elements of the data object, This implies that for a data structure, we must specify the set of operations, 3. Axioms (-#): This is the set of rules with which the different operations belonging to 7 can actually be implemented. Now we can define the term data structure. A data structure D is 2 wiplet, that is, D = (D, 9, #) where Dis a set of data object, 7 is aaset of functions and 4 is a set of rules to implement the functions. Let us consider an example. We know that for the integer data type (int) in the C programming language the structure includes the following types: D= (0,41, 32, 23, ..) FHA @ = (A set of binary arithmetics to perform ad multiplication, and modulo operations.) mn, subtraction, division, It can be easily recognized that the triplet (©, 7, #) is nothing but an abstract data type. Also, the elements in set D are not necessarily from primitive data; it may contain elements from some other abstract data types. Alternatively, an implementation of a data structure D is a mapping from ® to a set of other data structures D,, i = 1, 2, .... , for some m. More precisely, this mapping specifies how every object of Dis to be represented by the objects of Dj, = 1, 2, wean. Every function of D must be written using the function of the implementing data structures Dj i= 1, 2, «1. The fact is that each of the implementing data structures is either a primitive data type or an abstract data type. We can conclude the discussion with another example. Suppose, we want to implement a data type, namely Complex as an abstract data type. Any variable of the complex data type has two parts; a real part and an imaginary part. In our usual notation, if z is a complex number, then z = x + iy, where x and y are the real and imaginary parts, respectively. Both x and y are of the Real data type which is another abstract data type (available as a built-in data type). So, the abstract data type Complex can be defined using the data structure Real as Complex z { 4+ Real y : Real ) Now the set D of Complesxc can be realized from the domain of x and y which is Real in this case, Let us specify the set of operations for the Complex data type, which are stated as J F=(@,-,8 +.) Assume that 2, = xy + iy, and 22 = x; + dy ate two data of the Complex data type. Then we can define the rules for implementing the operations in , thus giving axioms #. In the current example, for the Complex data type 6 Classic Data Structures a= { 254 82 = y+) + iO + yd (complex addition) 224-22 = — 4p) + i — 2) (complex subtraction) 2% @ Zs (ty XX) — yy X ya) + Hy X yy XX y)) (Complex multiplication) zeata a Na Ya (complex division) aay t ya) ay tye Hj aty? ox ty? relal= yet ty? (complex magnitude) } Note that how different operations of the Complex data type can be implemented using the operations +, -, x, /, of the implementing data structure, namely Real. 2=Vq = (complex conjugate) Assignment 1.1 Implement Dare as an abstract data type which consists of da/mmlyy, where dd varies from 1 to 31, mm varies from 1 to 12 and yy is any integer with four digits. Specify 7 consisting of all possible operations on variables of type Date and then define 4 to implement all the operations in, Assume the implementing data structure(s) which is/are necessary. 13 OVERVIEW OF DATA STRUCTURES In computer science, several data structures are known depending on the areas of application. Out of them, a few data structures are frequently used in almost all application areas and with the help of which almost all complex data structures can be constructed. These data structures are known as fundamental data structures or classic data structures. Figure 1.2 gives a classification of all classic data structures. Classic data structures: Linear Nenlinoar Linear data structures ‘Arrays | [Linked ists] [stacks | [ Queves Nonlinear data evuctures Trees ‘Geephs_] [Tavies Sets Figure 1.2 Classification of classic data structures. 4 15 14 1 9 6 7 42 5 10 4 a 16 3 2 13 Figure 2.45 A magic square with SUM = 34, (a) Write a program to read a set of integers for a square matrix and then decide whether the matrix represents a magic square or not. (b) Write a game program as follows: (i) Read the size of the square matrix, N x N. (ii) Display a square matrix (now it is blank) of N x N. Gi) Allow the player to insert data into the matrix as displayed (you should give a chance to the user to confirm the entry and to alter the previous entries, if desired). (iv) After the completion of all the entries from the player, count the score as follows: Score = 0 (zero) if itis not a magic square. Otherwise score = T+ P*100, where T is the time required in seconds and P is the number of alteration of entries, The player's performance will be judged by the minimum score achieved other than zero. REFERENCES Gollieb, C.C. and L.R. Gotlieb, Data Types and Structures, Prentice Hall, Englewood Cliffs, New Jersey, 1986. Horowitz, Ellis, and Sartaj Sahni, Fundamentals of Data Structures, Computer Science Press, Rockville, Maryland, 1985. Kruse, Robert L., Bruce P, Leung and L, Clovis Tondo, Data Structures and Program Design in C, Prentice Hall of India, New Delhi, 1995. Tremblay, Jean Paul, and Paul G. Sorenson, An Introduction to Data Structeres with Applications, McGraw-Hill, New York, 1987. Linked Lists An array is a data structure where elements are stored in consecutive memory locations. In order to occupy the adjacent space, a block of memory that is required for the array should be allocated before hand. Once the memory is allocated, it cannot be extended any more. This is why the array is known as a static data structure. In contrast to this, the linked list is called a dynamic data structure where the amount of memory required can be varied during its use. In the linked list, the adjacency between the elements is maintained by means of links or pointers. A link or pointer actually is the address (memory location) of the subsequent element. Thus, in a linked list, data (actual content) and link (to point to the neat data) both are required to be maintained. An clement in a linked list is a specially termed node, which can be viewed as shown in Figure 3.1. A node consists of two fields: DATA (to store the actual information) and LINK (to point to the next node). LINK |—> Link to the next node Figure 3.1 Node: an element in a linked list. Linked Lists 37 3.1. DEFINITION A linked list is an ordered collection of finite, homogeneous data elements called nodes where the linear order is maintained by means of links or pointers. Depending on the requirements the pointers are maintained, and accordingly the linked list can be classified into three major groups: single linked list, circular linked list, and double linked list 3.2 SINGLE LINKED LIST In a single linked list each node contains only one link which points to the subsequent node in the list. Figure 3.2 shows a linked list with six nodes. Here, NI, N2, ..., N6 are the constituent nodes in the list. HEADER is an empty node (having data content NULL) and only used to store a pointer to the first node NI. Thus, if one knows the address of the HEADER node from the link field of this node, the next node can be traced, and so on, This means that starting from the first node one can reach to the last node whose link field does not contain any address but has a null value. Note that in a single linked list one can move from left to right only; this is why a single linked list is also called one way list. 3.2.1 Representation of a Linked List in Memory There are two ways to represent a linked list in memory: 1. Static representation using array 2. Dynamic representation using free pool of storage Static representation In static representation of a single linked list, two arrays are maintained: one array for data and the other for links. The static representation of the linked list in Figure 3.2 is shown in Figure 3.3. HEADER ¢ 5 NS Figure 3.2 A single linked list with six nodes. 38 Classic Data Structures DATA LINK a” 38 42{_— «alta wal _88 45[_ 80 Header 4g} —— al 7 4a 49) 50[ 64 Memory | Array of location pointers, Figure 3.3 Static representation using arrays of the single linked list of Figure 3.20. ‘Two parallel arrays of equal size are allocated which should be sufficient to store the entire linked list. Nevertheless this contradicts the idea of the linked list (that is non-contagious location of elements). But in some programming languages, for example, ALGOL, FORTRAN, BASIC, etc. such a representation is the only representation to manage a linked list. Dynamic representation ‘The efficient way of representing a linked list is using the free pool of storage. In this method, there is a memory bank (which is nothing but a collection of free memory spaces) and a memory manager (a program, in fact). During the creation of a linked list, whenever a node is required the request is placed to the memory manager; the memory manager will then search the memory bank for the block requested and, if found, grants the desired block to the caller. Again, there is also another program called the garbage collector: it plays whenever a node is no more in use; it returns the unused node to the memory bank. It may be noted that memory bank is basically a list of memory spaces which is available to a programmer. Such a memory management is known as dynamic memory management. The dynamic representation of linked list uses the dynamic memory management policy. ‘The mechanism of dynamic representation of single linked list is illustrated in Figures 3.4(a) and 3.4(b). A list of available memory spaces is there whose pointer is stored in AVAIL, For a request of a node, the list AVAIL is searched for the block of right size. If AVAIL is null or if the block of desired size is not found, the memory manager will return a message accordingly, Suppose the block is found and let it be XY. Then the memory manager will return the pointer of XY to the caller in a temporary buffer, say NEW. The newly availed node XY then can be inserted at any position in the linked list by changing the pointers of the concerned nodes. In Figure 3.4(a), the node XY is inserted at the end and change of pointers is shown by the dotted arrows. Figure 3.4(b) explains the mechanism of how a node can be returned from a linked list to the memory bank.

You might also like