Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lecture1 data structure(introduction)

2,071 views

Published on

  • Be the first to comment

Lecture1 data structure(introduction)

  1. 1. Lecture 1 Introduction to Data Structures Abdisalam Issa-Salwe Taibah University College of Computer Science & Engineering Computer Science DepartmentOutline1. Choosing Data Structures2. Selecting Data Structure3. Data Structure Philosophy4. Abstract Data Types5. Logical vs. Physical Form6. Programs7. Algorithm Properties8. References 2 1
  2. 2. Data StructuresA data structure is a schemefor organizing data in thememory of a computer.Some of the more commonlyused data structures includelists, arrays, stacks, queues,heaps, trees, and graphs. Binary Tree 3 Data StructuresThe way in which the data isorganized affects theperformance of a programfor different tasks.Computer programmersdecide which data structuresto use based on the natureof the data and the Binary Treeprocesses that need to beperformed on that data. 4 2
  3. 3. The Need for Data StructuresData structures organize data ⇒ more efficient programs.More powerful computers ⇒ more complex applications.More complex applications demand more calculations.Complex computing tasks are unlike our everyday experience. 5 The Need for Data StructuresData structures organize data ⇒ more efficient programs.More powerful computers ⇒ more complex applications.More complex applications demand more calculations.Complex computing tasks are unlike our everyday experience. 6 3
  4. 4. Example: A Queue A queue is an example of commonly used simple data structure. A queue has beginning and end, called the front and back of the queue. Data enters the queue at one end and leaves at the other. Because of this, data exits the queue in the same order in which it enters the queue, like people in a checkout line at a supermarket. 7 Example: A Binary TreeA binary tree is anothercommonly used datastructure. It is organized likean upside down tree.Each spot on the tree, calleda node, holds an item of dataalong with a left pointer anda right pointer. Binary Tree 8 4
  5. 5. Example: A Binary TreeThe pointers are lined upso that the structure formsthe upside down tree, witha single node at the top,called the root node, andbranches increasing on theleft and right as you godown the tree. Binary Tree 9 Choosing Data StructuresBy comparing the queue withthe binary tree, you can seehow the structure of the dataaffects what can be doneefficiently with the data. 10 5
  6. 6. Choosing Data StructuresA queue is a good data structureto use for storing things that needto be kept in order, such as a setof documents waiting to beprinted on a network printer.. 11 Choosing Data StructuresThe jobs will be printed in theorder in which they are received.Most network print serversmaintain such a print queue.. 12 6
  7. 7. Choosing Data StructuresA binary tree is a good datastructure to use for searchingsorted data.The middle item from the list isstored in the root node, withlesser items to the left and greateritems to the right. 13 Choosing Data StructuresA search begins at the root. Thecomputer either find the data, ormoves left or right, depending onthe value for which you aresearching.Each move down the tree cuts theremaining data in half. 14 7
  8. 8. Choosing Data StructuresItems can be located very quicklyin a tree.Telephone directory assistanceinformation is stored in a tree, sothat a name and phone numbercan be found quickly. 15 Choosing Data StructuresFor some applications, a queue isthe best data structure to use.For others, a binary tree is better.Programmers choose fromamong many data structuresbased on how the data will beused by the program. 16 8
  9. 9. The Need for Data Structures Data structures organize data ⇒ more efficient programs. More powerful computers ⇒ more complex applications. More complex applications demand more calculations. Complex computing tasks are unlike our everyday experience. 17 Selecting a Data StructureSelect a data structure as follows:1. Analyze the problem to determine the resource constraints a solution must meet.2. Determine the basic operations that must be supported. Quantify the resource constraints for each operation.3. Select the data structure that best meets these requirements. 18 9
  10. 10. Data Structure PhilosophyEach data structure has costs and benefits.Rarely is one data structure better than another in all situations.A data structure requires: space for each data item it stores, time to perform each basic operation, programming effort. 19 Data Structure PhilosophyEach data structure has costs and benefits.Rarely is one data structure better than another in all situations.A data structure requires: space for each data item it stores, time to perform each basic operation, programming effort. 20 10
  11. 11. Abstract Data TypesAbstract Data Type (ADT): a definition for a data type solely in terms of a set of values and a set of operations on that data type.Each ADT operation is defined by its inputs and outputs.Encapsulation: Hide implementation details. 21 Data Structure A data structure is the physical implementation of an ADT. Each operation associated with the ADT is implemented by one or more subroutines in the implementation. In a OO language such as C++, an ADT and its implementation together make up a class. Data structure usually refers to an organization for data in main memory. File structure: an organization for data on peripheral storage, such as a disk drive. 22 11
  12. 12. Abstract data types we also looked at how to use an array to model a list we call a list an abstract data type it has defined operations add to the end of the list find an item in the list delete an item from the list etc. it can be implemented in different ways array, piece of paper, tree, linked list the operations have the same effect no matter how the list is implemented other examples of abstract data types set, queue, stack, map 23Sets a set is an unordered group of elements duplicates are not allowed set of foods I like otherwise how would you tell them apart? Chips Cereal the set of "foods I Chicken like" contains the Orange Tomato elements juice soup cereal, chicken, chips, tomato soup, orange Chocolate juice and chocolate 24 12
  13. 13. Listsa list is a group of elementsarranged in some order Things I ate today so we can talk about (in chronological order) the first element in the list the last element in the list 1. cereal element 3 2. orange juicethe order could be meaningful 3. chocolate alphabetical by size 4. tomato soup by time 5. chocolateor it could simply be the order the 6. chickenelements were added 7. chipsduplicates are allowed 8. chocolate they are distinguished by their position 25Queue a queue is a a group of items arranged in order of arrival new items can only be added to the back of the queue items can only be removed from the front of the queue shoppers at supermarket check-out taxis in rank computer processes awaiting execution first-in, first-out (FIFO) 26 13
  14. 14. Stacks a stack is a group of items arranged in order new items can only be added to the top of the stack items can only be removed from the top of the stack stack of chairs or books plates in cafeteria temporary data storage in CPU last in, first out (LIFO) 27Maps A map is a collection of confusion key/element pairs algorithm each element is stored in a position determined by its key can look up an element using its key also known as a dictionary university key is the word element is the definition 28 14
  15. 15. Data Structure A data structure is the physical implementation of an ADT. Each operation associated with the ADT is implemented by one or more subroutines in the implementation. In a OO language such as C++, an ADT and its implementation together make up a class. Data structure usually refers to an organization for data in main memory. File structure: an organization for data on peripheral storage, such as a disk drive. 29 Labeling collections of objects Humans deal with complexity by assigning a label to an assembly of objects. An ADT manages complexity through abstraction. Hierarchies of labelsEx1: transistors ⇒ gates ⇒ CPU.In a program, implement an ADT, then think only about the ADT, not its implementation. 30 15
  16. 16. Logical vs. Physical FormData items have both a logical and a physical form.Logical form: definition of the data item within an ADT. Ex: Integers in mathematical sense: +, -Physical form: implementation of the data item within a data structure. Ex: 16/32 bit integers, overflow. 31 Data Type ADT: Data Items: Type Logical Form Operations Data Structure: Data Items: Storage Space Physical Form Subroutines 32 16
  17. 17. Problems, Algorithms and Programs Programmers deal with: problems, algorithms and computer programs. 33Problems Problem: a task to be performed. Best thought of as inputs and matching outputs. Problem definition should include constraints on the resources that may be consumed by any acceptable solution. 34 17
  18. 18. Problems (cont) Problems ⇔ mathematical functions A function is a matching between inputs (the domain) and outputs (the range). An input to a function may be single number, or a collection of information. The values making up an input are called the parameters of the function. A particular input must always result in the same output every time the function is computed. 35 Algorithms and ProgramsAlgorithm: a method or a process followed to solve a problem. A recipe: The algorithm gives us a “recipe” for solving the problem by performing a series of steps, where each step is completely understood and doable.An algorithm takes the input to a problem (function) and transforms it to the output. A mapping of input to output.A problem can be solved by many algorithms. 36 18
  19. 19. A problem can have many algorithmsFor example, the problem of sorting can be solved by the following algorithms: Insertion sort Bubble sort Selection sort Shell sort Merge sort Others 37 Algorithm PropertiesAn algorithm possesses the following properties: It must be correct. It must be composed of a series of concrete steps. There can be no ambiguity as to which step will be performed next. It must be composed of a finite number of steps. It must terminate.A computer program is an instance, or concrete representation, for an algorithm in some programming language. 38 19
  20. 20. Programs A computer program is a concrete representation of an algorithm in some programming language. Naturally, there are many programs that are instances of the same algorithms, since any modern programming language can be used to implement any algorithm. 39References Michael Main, Data Structures and Other Objects Using Java (Third Edition) Abdisalam Issa-Salwe, Taibah University, Madinah, Saudi Arabia. 40 20

×