Data Structures

26

description

An introduction to Data Structures

Transcript of Data Structures

PowerPoint Presentation

Growth rate of functions

Data abstraction The separation of a data types logical properties from its implementation.

Integers are physically represented in different ways on different computers. In the memory of one machine, an integer may be a binary-coded decimal. In a second machine, it may be a sign-and-magnitude binary. And in a third one, it may be represented in twos-complement binary notation. Although you may not be familiar with these terms, that hasnt stopped you from using integers.

The way that integers are physically represented determines how the computermanipulates them.

As a Java Programmer all you need to know is how to declare an int type variable and what operations are allowed on integers: assignment, addition, subtraction, multiplication, division, and modulo arithmetic.

Composite type A data type whose elements are composed of multiple data items.

Composite data types come in two forms: unstructured and structured.

An unstructured composite type is a collection of components that are not organized with respect to one another.

A structured composite type is an organized collection of components in which the organization determines the means of accessing individual data components or subsets of the collectionData encapsulation The separation of the representation of data from the applications that use the data at a logical level; a programming language feature that enforces information hiding.

Computers would not be very popular if every time we wanted to multiply two numbers we had to get down to the machine-representation level. But we dont have to: Java has provided the int data type for us, hiding all the implementation details and giving us just the information we need to create and manipulate data of this type.

We say that Java has encapsulated integers for us

Abstract data type (ADT) A data type whose properties (domain and operations) are specified independently of any particular implementation

A programmer can declare variables of those types without understanding the underlying implementation. The programmer can initialize, modify, and access the information held by the variables using the provided operations.

In addition to the built-in ADTs, Java programmers can use the Java class mechanism to build their own ADTs.Data structure A collection of data elements whose logical organization reflects a relationship among the elements. A data structure is characterized by accessingoperations that are used to store and retrieve the individual data elements.

lists, stacks, queues, trees, and graphsLista list is a homogeneous collection of elements, with a linear relationship between its elements.Linear relationship Each element except the first has a unique predecessor, and each element except the last has a unique successor Length The number of items in a list; the length can vary over timeUnsorted list A list in which data items are placed in no particular order; the only relationship between data elements is the list predecessor and successor relationshipsSorted list A list that is sorted by the value in the key; there is a semantic relationship among the keys of the items in the listKey The attributes that are used to determine the logical order of the items on a list

Big-O notation A notation that expresses computing time (complexity) as the term in a function that increases most rapidly relative to the size of a problemO(1) is called bounded time. The amount of work is bounded by a constant and is not dependent on the size of the problem. Assigning a value to the ith element in an array of N elements is O(l) because an element in an array can be accessed directly through its index. Although bounded time is often called constant time, the amount of work is not necessarily constant. It is, however, bounded by a constant.Symbol balancerA program that checks whether everything is balanced. For simplicity, we will just check for balancing of parentheses, brackets, and braces and ignore any other character that appears.

Make an empty stack. Read characters until end of file.If the character is an open anything, push it onto the stack. If it is a close anything, then if the stack is empty report an error. Otherwise, pop the stack. If the symbol popped is not the corresponding opening symbol, then report an error. At end of file, if the stack is not empty report an error.Evaluation of Postfix expressionpostfix or reverse Polish notation.

The easiest way to do this is to use a stack. When a number is seen, it is pushed onto the stack; when an operator is seen, the operator is applied to the two numbers (symbols) that are popped from the stack and the result is pushed onto the stack.

The time to evaluate a postfix expression is O(n), because processing eachelement in the input consists of stack operations and thus takes constant time. When an expression is given in postfix notation, there is no need to know any precedence rules;Infix to Postfix ConversionWhen an operand is read, it is immediately placed onto the output. Operators arenot immediately output, so they must be saved somewhere. The correct thing to dois to place operators that have been seen, but not placed on the output, onto thestack. We will also stack left parentheses when they are encountered. We startwith an initially empty stack.

If we see a right parenthesis, then we pop the stack, writing symbols until weencounter a (corresponding) left parenthesis, which is popped but not output.

If we see any other symbol ( '+ , '* , '( ), then we pop entries from the stackuntil we find an entry of lower priority. One exception is that we never remove a'(' from the stack except when processing a ')'.

When the popping is done, we push the operand onto the stack.

Finally, if we read the end of input, we pop the stack until it is empty, writingsymbols onto the output.

Insertion Sort

Merge Sort

Merge sort

Two stacks in an array

Use of two stacks in an array

Tree Terminology

Tree terminology

Wheninsertingany element, insert it at the end of all the elements in the normal array. Store current-top of that stack as parent for the new element (in the parents' array) and update current-top to the new position.

Whendeleting, insert NULL in the stacks array for the deleted element and reset stack-top for that stack to the parent.

When the array is full, it will have some holes corresponding to deleted elements. At this point, either the array can be compacted to bring all free space together or a linear search can be done for free space when inserting new elements.