Вы находитесь на странице: 1из 4

Data Structures Lecture 1: Introduction Module objectives At the end of this module you i) will be familiar with a variety

of data structures, their implementation and use in different applications. ii) will have obtained a fundamental grounding in the design of various forms of data structures Introduction A data structure is a systematic way of organizing a collection of data Every data structure needs a variety of algorithms for processing the data in it: algorithms for insertion, deletion, retrieval, etc. So, it is natural to study data structures in terms of properties, organization, operations This can be done in a programming language independent way - JAVA

Why Java? Why do we need to study data structures in a language independent way? Java is just one of many languages within the category of object-oriented languages The worlds most favorite programming language changes about every five to ten years However, data structures have been around since the invention of high-level programming languages (not change). Therefore, We must be able to realize data structures in other languages We need to study algorithms

Data structures that we will consider We will study various data structures such as i. Arrays ii. Lists iii. Stacks iv. Queues v. Trees vi. Indexes In each case we will define the structure, give examples, and show how the structure is implemented in Java either directly or via other, previously defined, data structures.

The choice on which data structure to be used for a system will affect the system design and contribute to the effectiveness of that system. The correct choice of data structure allows major improvement in program efficiency.

A good approach to determine which data structure that best suit your application is to study the strengths and weaknesses of each of the data structure available.

Data Structure Array

Advantages Quick inserts Fast access (if index known) Faster search than unsorted array

Disadvantages Slow search Slow deletes Fixed size Slow inserts Slow deletes Fixed size Slow access to other items Slow access to other items Slow search Deletion algorithm is complex

Ordered Array Stack Queue Linked List Last In First Out access First In First Out access Quick inserts Quick deletes Quick inserts Quick deletes Quick search (if tree remains balanced) Fast access (if key known) Quick inserts Hash Table Model real world situation Graphs

Binary Tree

Slow deletes Slow access (if key not known) Inefficient memory usage Some algorithm are slow and complex

Table 1: Characteristics of Data Structure


Abstract Data Type (ADT) Definition: A set of data values and associated operations that are precisely specified In any class represents a data type, where a class is made up of attributes/data and operations/methods on that data. ADT is a way of looking at data structure, where it focuses on what it does rather than how it does its job. The end user should know what method to be called and how to call it, rather than how the method works. Example : A stack is an ADT, because it has a set of methods which are push, pop, peek, and isEmpty.

Static versus dynamic data structures Besides time and space efficiency another important criterion for choosing a data structure is whether the number of data items it is able to store can adjust to our needs or is bounded. This distinction leads to the notions of dynamic data structures vs. static data structures Dynamic data structures grow or shrink during run-time to fit current requirements e.g., a structure used in modeling traffic flow. Static data structures are fixed at the time of creation. e.g., a structure used to store a postcode or credit card number (which have a fixed format). Static data structures Advantages ease of specification Programming languages usually provide an easy way to create static data structures of almost arbitrary size no memory allocation overhead Since static data structures are fixed in size, there are no operations that can be used to extend static structures; such operations would need to allocate additional memory for the structure (which takes time) Static data structures Disadvantages must make sure there is enough capacity Since the number of data items we can store in a static data structure is fixed, once it is created, we have to make sure that this number is large enough for all our needs more elements? (errors), fewer elements? (waste) However, when our program tries to store more data items in a static data structure than it allows, this will result in an error (e.g. ArrayIndexOutOfBoundsException) On the other hand, if fewer data items are stored, then parts of the static data structure remain empty, but the memory has been allocated and cannot be used to store other data. Example An example of a static data structure is an array: i n t [ ] a = new i n t [ 5 0 ] ; which allocates memory space to hold 50 integers. The language provides the construct new to indicate to the compiler/interpreter how much space of which data type needs to be allocated In Java, arrays are always dynamically allocated even when we write: int a[]={10,20,30,40}; However, the size of the array is fixed

Dynamic data structures Advantages There is no requirement to know the exact number of data items Since dynamic data structures can shrink and grow to fit exactly the right number of data items, there is no need to know how many data items we will need to store Efficient use of memory space if we only extend a dynamic data structure in size whenever we need to add data items which could otherwise not be stored in the structure and if we shrink a dynamic data structure whenever there is unused space in it, then the structure will always have exactly the right size and no memory space is wasted Dynamic data structures Disadvantages Memory allocation/de-allocation overhead Whenever a dynamic data structure grows or shrinks, then memory space allocated to the data structure has to be added or removed (which requires time)

Вам также может понравиться