Sei sulla pagina 1di 610

Resmi N.G. Reference: Data Structures and Algorithms: Alfred V. Aho, John E. Hopcroft, Jeffrey D.

Ullman

Syllabus
Module I (11 hours) Review of Data Types - Scalar Types - Primitive types - Enumerated types - Subranges - Arrays- sparse matrices - representation - Records - Complexity of Algorithms - Time & Space Complexity of Algorithms Recursion: Recursive algorithms - Analysis of Recursive algorithms Module II (18 hours) Linear Data Structures - Stacks Queues -Lists - Dequeus - Linked List - singly, doubly linked and circular lists - Application of linked lists - Polynomial Manipulation - Stack & Queue implementation using Array & Linked List - Typical problems - Conversion of infix to postfix - Evaluation of postfix expression priority queues Module III (18 hours) Non Linear Structures - Graphs - Trees - Graph and Tree implementation using array and Linked List Binary trees - Binary tree traversals - pre-order, in-order and postorder - Threaded binary trees Binary Search trees - AVL trees - B trees and B+ trees - Graph traversals - DFS, BFS - shortest path - Dijkstras algorithm, Minimum spanning tree - Kruskal Algorithm, Prims algorithm Module IV (18 hours) Searching - Sequential Search - Searching Arrays and Linked Lists - Binary Searching - Searching arrays and Binary Search Trees - Hashing - Open & Closed Hashing - Hash functions - Resolution of Collision Sorting- n2 Sorts - Bubble Sort - Insertion Sort - Selection Sort - n log n Sorts - Quick Sort - Heap Sort Merge Sort - External Sort - Merge Files
CS09 303 Data Structures - Module 1

References
Text Books Aho A.V, Hopcroft J.E. & Ullman J.D, Data Structures and Algorithms, AddisonWesley Reference Books 1. Sahni S, Data Structures, Algorithms and Applications in C++, McGrawHill 2. Wirth N, Algorithms + Data Structures = Programs, Prentice Hall. 3. Cormen T.H, Leiserson C.E & Rivest R.L, Introduction to Algorithms in C++, Thomson Books. 4. Fundamentals of Computer Algorithms, Ellis Horowitz, S. Sahni 5. Deshpande P.S, Kakde O.G, C and Data Structures, Dream- tech India Pvt. Ltd.
CS09 303 Data Structures - Module 1

Module 1
Introduction Review of Data Types Scalar types Primitive types Enumerated types Subrange types Arrays - representation sparse matrices Records - representation Sets - representation

CS09 303 Data Structures - Module 1

Complexity of Algorithms Time complexity Space Complexity Recursion: Recursive algorithms Analysis of Recursive algorithms

CS09 303 Data Structures - Module 1

Problems to Programs
Problem formulation and specification Design of the solution Implementation Testing and documentation Evaluation of the solution
CS09 303 Data Structures - Module 1

Problems to Programs
Identify the problem Formal model (mathematical model informal algorithm) Check for existing programs for the given problem Algorithm pseudo-language and stepwise refinement Implementation
CS09 303 Data Structures - Module 1

Algorithm
Finite sequence of instructions Clear meaning Finite amount of effort Finite length of time (never enters an infinite loop on any input.)

CS09 303 Data Structures - Module 1

Algorithm Specification
Pascal Language Pseudo Language Constructs of a programming language + Informal English statements

CS09 303 Data Structures - Module 1

Problem Solving Process


Mathematical Model Informal Algorithm Abstract Data Type (ADT) Data Structure Pascal program Pseudo language Program (Step-wise refinement) :

Mathematical model (together with various operations defined on the model). Designing Phase Data Structure: Logical Model (to represent the mathematical model underlying an ADT) Implementation Phase Both are models with collection of operations
CS09 303 Data Structures - Module 1

ADT

CS09 303 Data Structures - Module 1

The model defines an abstract view to the problem. This implies that the model focuses only on problem and tries to define properties of the problem. These properties include: the data which are affected and the operations which are identified by the problem.

CS09 303 Data Structures - Module 1

As an example, consider the administration of employees in an institution. What employee information administration? What tasks should be allowed? is needed by the

Employees are real persons who can be characterized with many properties; a few are: name, size, date of birth, social number, room number, hobbies.
CS09 303 Data Structures - Module 1

Only some of them are problem specific. Consequently, you create a model of an employee for the problem. This model only implies properties which are needed to fulfill the requirements of the administration, for instance, name, date of birth and social number. These properties are called the data of the (employee) model. Now you have described real persons with help of an abstract employee.
CS09 303 Data Structures - Module 1

There must be some operations defined with which the administration is able to handle the abstract employees. For example, there must be an operation which allows you to create a new employee once a new person enters the institution. Also, you have to identify the operations which should be able to be performed on an abstract employee. You also decide to allow access to the employees' data only with associated operations.
CS09 303 Data Structures - Module 1

Data Abstraction
Abstraction is the structuring of a nebulous(not properly defined) problem into well-defined entities by defining their data and operations. These entities combine data and operations.

CS09 303 Data Structures - Module 1

Properties of Abstract Data Types


With abstraction, you create a well-defined entity which can be properly handled. These entities define the data structure of a set of items. For example, each administered employee has a name, date of birth and social number. The data structure can only be accessed with defined operations.
CS09 303 Data Structures - Module 1

An entity with the properties just described is called an abstract data type (ADT). ADT consists of an abstract data structure and operations. Only the operations are viewable from the outside.

CS09 303 Data Structures - Module 1

CS09 303 Data Structures - Module 1

Once a new employee is ``created', the data structure is filled with actual values; You then have an instance of an abstract employee. As many instances of an abstract employee as needed can be created to describe every real employed person.

CS09 303 Data Structures - Module 1

An abstract data type (ADT) is characterized by the following properties: 1. It exports a type.

2. It exports a set of operations. This set is called interface. 3. Operations of the interface are the one and only access mechanism to the type's data structure. 4. Axioms and preconditions define the application domain of the type.
CS09 303 Data Structures - Module 1

Data Structure Encapsulation


The principle of hiding the used data structure and to only provide a well-defined interface is known as encapsulation.

CS09 303 Data Structures - Module 1

To define an ADT for complex numbers. Complex numbers consists of two parts: real part and imaginary part. Both parts are represented by real numbers. Complex numbers define several operations: addition, subtraction, multiplication, division etc.

CS09 303 Data Structures - Module 1

To represent a complex number, it is necessary to define the data structure to be used by its ADT. Two possibilities: Both parts are stored in a two-valued array where the first value indicates the real part and the second value the imaginary part of the complex number. If x denotes the real part and y the imaginary part, you could think of accessing them via array subscription: x=c[0] and y=c[1].
CS09 303 Data Structures - Module 1

Both parts are stored in a two-valued record. If the element name of the real part is r and that of the imaginary part is i, x and y can be obtained with: x=c.r and y=c.i. ADT definition also says that for each access to the data structure, there must be an operation defined. The addition of two complex numbers requires you to perform an addition for each part. Consequently, you must access the value of each part which is different for each version.
CS09 303 Data Structures - Module 1

By providing an operation ``add'' you can encapsulate these details from its actual use. In an application context you simply ``add two complex numbers'' regardless of how this functionality is actually achieved. Once you have created an ADT for complex numbers, say Complex, it can be used in the same way as well-known data types such as integers.
CS09 303 Data Structures - Module 1

Abstract Data Type (ADT) Mathematical model with collection of operations defined on that model Generalization of primitive data types Eg; Sets of integers together with operations of union, intersection and set difference. Eg; LIST (of integers)
Make list empty Get first member of list, return null if empty Get next member of list, return null if empty Insert integer into list
CS09 303 Data Structures - Module 1

Implementation of ADT is a translation into statements of a programming language of the declaration that defines a variable to be that ADT, plus a procedure in that language for each operation of that ADT. Implementation chooses a data structure to represent the ADT. Ie; Data structure is used to represent the mathematical model underlying an ADT.

CS09 303 Data Structures - Module 1

ADT and DATA ABSTRACTION


Encapsulation of operations and data (eg:ADT) 2 properties Generalization Encapsulation Advantages Changes done easily by revising a small section. No concern of underlying implementation.
CS09 303 Data Structures - Module 1

Data Types, Data Structures and ADTs


Data type determines the set of values which may be assumed by a variable or expression. Type of a value denoted by a variable or expression may be derived from its declaration. Each operator or function expects arguments of a fixed type and yields result of a fixed type.

CS09 303 Data Structures - Module 1

DATA TYPES IN PASCAL


Pascal supports two major types of data: Simple Vs. Structured Within these types there are scalar and ordinal kinds of data. Simple data types cannot be further broken down into anything finer. Eg; INTEGER, REAL, BOOLEAN and CHAR. These four types of data are also called the Pascal standard data types. These four types are also scalar data, since the relational characters < > = <> <= and >= are all valid between values of any data of these types. INTEGER, BOOLEAN and CHAR are also ordinal data. This means that every value of a given type (except the last) has a unique value which comes after it (called its successor) and every value (except the first) has a unique value which comes before it (called its predecessor). Data of type REAL however, is not ordinal, since finding the "next" value after a real number value is determined by the number of decimal digits the computer can store.
CS09 303 Data Structures - Module 1

Different Data Types


Scalar Primitive Enumerated Subranges Arrays Records Sets Files Pointers and Cursors Structure

CS09 303 Data Structures - Module 1

Primitive Data types


Machine dependent boolean char Integer real
Variable declaration in Pascal var result : real Variable result of type real is declared using keyword var.

CS09 303 Data Structures - Module 1

BOOLEAN Values: FALSE , TRUE Operations: NOT, AND, OR, assignment Predefined functions: PRED, SUCC, ORD Predefined procedures: WRITE, WRITELN CHAR Values: space < '!' < ... < '0' < '1' < ... < '9' < ':' < ';' < '<' < '=' < '>' < '?' < '@'
< 'A' < 'B' < ... < 'Z' < ... < 'a' < 'b' < ... < 'z

Operations: assignment, comparison with relational characters Predefined functions: PRED, SUCC, ORD Predefined procedures: READ, READLN, WRITE, WRITELN

CS09 303 Data Structures - Module 1

INTEGER Values: -32768, ..., -1, 0, 1, 2, 3, ... 32767 (= MAXINT) Operations: +, -, *, DIV, MOD, assignment, comparison with relationals Predefined functions: PRED, SUCC, ORD, ABS, SQR, SQRT, ODD, CHR Predefined procedures: READ, READLN, WRITE, WRITELN REAL Values: ratios of integers Operations: +, -, *, /, assignment, comparison Predefined functions: ABS, SQR, SQRT, ROUND, TRUNC Predefined procedures: READ, READLN, WRITE, WRITELN

CS09 303 Data Structures - Module 1

CHR The chr or character position function returns the character associated with the ASCII value being asked. eg; chr( 65 ) will return the character A. ORD The ord or ordinal function returns the ASCII value of a requested character. In essence, it works backwards to the chr function. Ordinal data types are those which have a predefined, known set of values. Each value which follows in the set is one greater than the previous. Characters and integers are thus ordinal data types. ord( 'C' ) will return the value 67.

CS09 303 Data Structures - Module 1

SUCC The successor function determines the next value or symbol in the set, thus succ( 'd' ) will return e. PRED The predecessor function determines the previous value or symbol in the set, thus pred( 'd' ) will return c. ord(false) ord(true) ord(21) ord('A') returns returns returns returns 0 1 21 65

CS09 303 Data Structures - Module 1

USER DEFINED TYPES


In Pascal, a programmer can define his/her own constants (in a const section) and similarly his/her own variables (in a var section). User defined data types are placed in a new section of the program, the TYPE section. This section is found between CONST and VAR sections using the reserved word TYPE. One way to declare a type is to use a subrange. For example: const Last = 50; type Valid_numbers = 1..Last; var X, Y: Valid_numbers;
CS09 303 Data Structures - Module 1

Enumerated data Type


type <name> = (const1,const2,); eg: type day = (Mon,Tues,,Sun); type boolean = (true,false) ;

CS09 303 Data Structures - Module 1

ENUMERATED DATA TYPES


type Work_days = (Monday, Tuesday, Thursday, Friday); Week_end = (Saturday, Sunday); Wednesday,

var School_days: Work_days; Free_days: Week_end; Worst_days: Monday..Thursday; { user defined subrange type }
CS09 303 Data Structures - Module 1

type beverage = ( coffee, tea, cola, soda, milk, water ); color = ( green, red, yellow, blue, black, white ); var drink : beverage; chair : color; drink := coffee; chair := green; if chair = yellow then drink := tea;
CS09 303 Data Structures - Module 1

SUBRANGE TYPES
Identifiers can be defined so that they have a restricted range of values of a given ordinal type. The syntax is: Identifier: First value..Last value; These constructs are called subrange types. Examples: var num: -10..19; { Num has integer values -10 to 19 inclusive } alphabet : 'A'..'Z; { Alphabet is char with values 'A' to 'Z' }
CS09 303 Data Structures - Module 1

Subranges

type name = <constant1> .. <constant2> eg: type range = low..high type digit = 0..99

Used as array bounds

CS09 303 Data Structures - Module 1

CS09 303 Data Structures - Module 1

Cell
Cell Basic building block of data structures Capable of holding a value

Data Structure
Created by giving names to aggregates of cells.

CS09 303 Data Structures - Module 1

Arrays
Homogeneous Random access structure Index array[0..99] of type var A :array[0..99] of char name:array[index type] of celltype Array is a sequence of cells of a given type often referred to as celltype. Indextype can be an enumerated data type or subrange.

CS09 303 Data Structures - Module 1

Records
Collection of cells called fields May be of dissimilar types Records can be grouped into arrays.
record <name1> : <type1> <name2> :<type2> . . <namek> :<typek> end

CS09 303 Data Structures - Module 1

Kinds Of Record Declaration


Using Enumeration eg; type complex = record re:real; im:real; end;

type complex = record re,im:real; end;

Using var & array combination var reclist: array[1..4]of record data:real; next:integer; end;

CS09 303 Data Structures - Module 1

type cardsuit = (clubs, diamonds, hearts, spades); card = record suit: cardsuit; value: 1 .. 13; end; var hand: array [ 1 .. 13 ] of card; trump: cardsuit;

CS09 303 Data Structures - Module 1

Array Vs Record
Array Homogeneous Run Time Record Heterogeneous Compile time

CS09 303 Data Structures - Module 1

SETS
Ordered collection of elements + No repetition Set brackets - individually or subranges eg:[a..z,A..Z],[0..9],[mon..sun] Operators defined on all set types - [+,-,*, in ]
type T = set of T0 Eg; type set1 = set of [1..3] var A: set1; or var A: set of [1..3];
CS09 303 Data Structures - Module 1

Set Operations =>Membership operator (in) =>Bitwise operator (Union, difference, intersection) =>Relational (<=,=,>=)

CS09 303 Data Structures - Module 1

FILES
Sequence of values of some particular type No index type, accessed in order Number of elements in a file can be time-varying and unlimited.
type T = file of T0 Eg; type file1 = file of char var A: file1; Or var A: file of char;

File operators: {rewrite, put, reset, get}


CS09 303 Data Structures - Module 1

POINTERS
Cell whose value indicates another cell.
var ptr : cell type;

type link = cell; cell = record info: integer; next: link end;

CS09 303 Data Structures - Module 1

CURSOR
Integer valued cell, used as array(used in Fortran)
header

pointer to an

.
1.2 3.4 5.6 7.8
data reclist

1 2

3 0 2 1
next

type recordtype = record cursor:integer; ptr: recordtype end;


CS09 303 Data Structures - Module 1

3 4

Complexity of algorithm
Time

Space

Understandability

More important is runtime


CS09 303 Data Structures - Module 1

Measuring runtime of a program(Factors)


Input

Quality of code generated by the compiler

Nature and speed of the instructions on the machine

Time complexity of the algorithm


CS09 303 Data Structures - Module 1

Running time of a program


Depends on input It is a function of size of the input. Denoted by T (n) where n is the size of the input.

CS09 303 Data Structures - Module 1

Views of running time


(1) Worst case: maximum inputs (2) Average case: average number of inputs (3) Best case: no risk considered

T(n) is measured not in seconds but by growth functions.

Generally worst case running time is calculated .

CS09 303 Data Structures - Module 1

Big-Oh(Worst case)
The time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the size of the input to the problem. The time complexity of an algorithm is commonly expressed using Big - Oh notation. Time complexity is commonly estimated by counting the number of elementary operations performed by the algorithm Big - Oh notation usually only provides an upper bound on the growth rate of the function.

Asymptotic efficiency- how running time of an algorithm increases with the size of input.
CS09 303 Data Structures - Module 1

CS09 303 Data Structures - Module 1

Definition of Big-Oh
T(n) is O (n) Means that there exist +ve constants c and n0 such that for all n n0 , we have, T(n) c n O(g(n)) = f(n) : if there exist +ve constants c and n0 such that 0 f(n) c.g(n) , for all n n0 Find time complexity of T(n) = (n+1) Find time complexity of T(n) = 3n+2n
CS09 303 Data Structures - Module 1

CS09 303 Data Structures - Module 1

Rules for Big-Oh


Rule for sum T(n) of P = O(f(n)) T2(n) of P2 = O(g(n)) Therefore, running time of P1 followed by P2, ie; T(n) +T2(n) = O(max(f(n),g(n))) eg; O(n+n) = O(n)

Find running time of O(n),O(n),O(n log n) Find O(max(f(n),g(n))), if f(n)={n4, if n is even; n, if n is odd} g(n)={n, if n is even; n, if n is odd}

CS09 303 Data Structures - Module 1

Rules for products T(n) = O(f(n)) T2(n) = O(g(n)) T(n).T2(n) = O(f(n).g(n))

O(c.f(n)) = O(f(n)) Eg: O(n/2) = O(n)

CS09 303 Data Structures - Module 1

Points to consider when analysing an algorithm


Count the total number of elementary operations in the algorithm for any given input. For algorithms with loops, count the maximum number of iterations for any given input. For algorithms with recursive function calls, count the maximum number of recursive function calls on any given input.
CS09 303 Data Structures - Module 1

Big O notation will always assume the upper limit where the algorithm will perform the maximum number of operations or iterations. O(1) describes an algorithm that will always execute in the same time (or space) regardless of the size of the input data set. O(1) operations run in constant time. O(N) describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input data set. O(n) operations run in linear time.

CS09 303 Data Structures - Module 1

O(N2) represents an algorithm whose performance is directly proportional to the square of the size of the input data set. This is common with algorithms that involve nested iterations over the data set. Deeper nested iterations will result in O(N3), O(N4) etc. O(log n) - Any algorithm which cuts the problem in half each time is O(log n). O(log n) operations run in logarithmic time. O(n log n) Performs O(log n) operation for each item in your input. O(n log n) operations run in loglinear time.
CS09 303 Data Structures - Module 1

O(2^n) - means that the time taken will double with each additional element in the input data set. O(2^n) operations run in exponential time. O(n!) - involves doing something for all possible permutations of the n elements. O(n!) operations run in factorial time.

CS09 303 Data Structures - Module 1

Elementary operations:
Arithmetic operations Assignment operation Testing a condition Read operation Write operation These operations taken independently has complexity O(1).

CS09 303 Data Structures - Module 1

1. Sequence of statements which is executed only once. Constant time O(1) Statement1; Statement2; : : Statement k; Total time taken = time(Statement1) + time(Statement2) + + time(Statement k) Time for each statement is a constant. Therefore, total time is a constant and hence T(n) is O(1). This has complexity O(1) as it does not depend on the number of statements in the sequence.
CS09 303 Data Structures - Module 1

Example: Algorithm to check if a number is even or odd. if n%2 = 0 then ------O(1) writeln(number is even); ------O(1) else writeln(number is odd); ------O(1) T(n) is O(1).

CS09 303 Data Structures - Module 1

2. For loop Linear time O(n) for i:=1 to n do begin sequence1; end To find time complexity of an algorithm with loops, count the number of times the loop executes. Worst case: loop executes n times. Here, each of the statements in the sequence is O(1). Total no. of iterations = n Therefore, total time taken = n*O(1) = O(n). Hence, T(n) is O(n).
CS09 303 Data Structures - Module 1

Example: Linear Search or Sequential Search Algorithm to search for a number in an array. Best case: number is found at first position. Worst case: number is found at the last position or it is not present in the array( entire array is searched). for i:=1 to n do begin if num = arr[i] then ---O(1) writeln(number found); ---O(1) end T(n) is n*O(1) = O(n).
CS09 303 Data Structures - Module 1

n times

3. If-then-else statements if condition then sequence1; else sequence2; Either sequence1 or sequence2 will execute. Worst case time is the slowest of the 2 possibilities. T(n) = O(max[time(sequence1), time(sequence2)]) Suppose time(sequence1) = O(1) and time(sequence2) = O(n), then T(n) = O(n).
CS09 303 Data Structures - Module 1

Example: Algorithm to print first n numbers if choice = 1 else print wrong choice. Best case: choice not equal to 1. Worst case: choice = 1. All n numbers are printed. if choice = 1 then for i:=1 to n do begin if num = arr[i] then ---O(1) n times writeln(number found); ---O(1) end else writeln(Wrong choice!); ---O(1) T(n) is max(O(n), O(1)) = O(n).
CS09 303 Data Structures - Module 1

4. Nested for loop a)Quadratic time O(n2) for i:=1 to m do begin for j:=1 to n do begin sequence1; end end To find time complexity of an algorithm with loops, count the total number of iterations. Worst case: Inner loop executes n times and outer loop executes m times.
CS09 303 Data Structures - Module 1

Here, each of the statements in the sequence is O(1). For each iteration of outer loop, no. of iterations of inner loop = n. Therefore, T(inner loop) = n*O(1) = O(n). No. of iterations of outer loop = m. Therefore, total no. of iterations of O(1) sequence = m*n. Or There are m repetitions of O(n) sequence. Hence, T(n) = m* O(n) = O(mn)

Hence, T(n) is O(n2) when m = n.


CS09 303 Data Structures - Module 1

Example: Displaying a matrix for i:=1 to n do begin for j:=1 to n do begin writeln(arr[i,j]) ; ---O(1) end end T(n) is n*(n*O(1))) = O(n2).

n times n times

CS09 303 Data Structures - Module 1

4. Nested for loop b) Cubic time O(n3) Matrix multiplication for i:=1 to n do begin for j:=1 to n do begin c[i,j] := 0; for k:=1 to n do c[i,j] := c[i,j] + a[i,k]*b[k,j]; end To find time complexity of an algorithm with loops, count the total number of iterations.

CS09 303 Data Structures - Module 1

Here, c[i,j] := c[i,j] + a[i,k]*b[k,j]; is O(1). The innermost loop executes n times. ie; There are n repetitions of O(1) sequence. Therefore, T(innermost loop) = n*O(1) = O(n). No. of iterations of loop with index j = n. Therefore, total no. of iterations of O(1) sequence = n*n = n2. Or There are n repetitions of O(n) sequence. Outer loop executes n times. Therefore, total no. of iterations of O(1) sequence = n2 *n = n3. Or There are n repetitions of O(n2) sequence. Hence, T(n) is O(n3). The algorithm takes Cubic time.
CS09 303 Data Structures - Module 1

5. Nested loop where inner loop index depends on outer loop index. for i:=1 to m do begin for j:= i+1 to n do sequence1;
Value of i 1 2 : n-2 n-1 No. of iterations of inner loop n-1 n-2 : 2 1

CS09 303 Data Structures - Module 1

Total number of iterations = 1+ 2 + + n-2 + n-1 = (n-1)n/2 = (n2/2) - n/2 = O(n2)

T(n) is O(n2). The algorithm takes Quadratic time.


Hence,

CS09 303 Data Structures - Module 1

Some more examples


2 loops one after the other for i:=1 to n do sequence1; for j:=1 to m do sequence2; Complexity of first loop is O(n) and second is O(m). By sum rule, runtime of the whole sequence is T(n) is O(m+n) = O(max(m,n)).
CS09 303 Data Structures - Module 1

Nested loop followed by non-nested loop for i:=1 to n do for j:= 1 to n do sequence1; for k:=1 to n do sequence2; Complexity of first loop is O(n2) and second is O(n). By sum rule, runtime of the whole sequence is T(n) is O(max(n2,n)) = O(n2).

CS09 303 Data Structures - Module 1

Analysis of bubble sort algorithm

CS09 303 Data Structures - Module 1

Analysis of bubble sort algorithm


Assignment statement takes some constant amount of time, independent of input size . ie, (4),(5),(6) each take O(1) time. Testing condition requires O(1) time. Thus if group statements (3) (6) takes O(1) time. For loop lines (2) (6), the running time is the sum over each iteration of the loop, of the time spent executing the loop body for that iteration. no. of iteration of the loop = n-i
CS09 303 Data Structures - Module 1

Thus, Time taken by loop body for each iteration is O(1). Time spent in the loop of line (2) - (6) = O((n-i)* 1) = O(n-i). Statement (1) is executed (n-1) times. So the total running time of the program is bounded above by some constant times.

CS09 303 Data Structures - Module 1

n-1 i=1

(n-i) = (n-1)(n-2)(n-3)(1)

Sum of n natural numbers = n(n-1)/2 - So Here, T(n) = O(n2 ).

CS09 303 Data Structures - Module 1

Most Familiar Big-Oh notations (Orders of growth in increasing order) (1) (2) (3) (4) (5) (6) (7) (8) Constant time Logarithmic Time Linear time n log n Quadratic time Cubic time Polynomial time Exponential Time O(1) O(logn) O(n) O(nlogn) O(n2) O(n3) O(nk) O(kn)

CS09 303 Data Structures - Module 1

CS09 303 Data Structures - Module 1

Recursion Recursive Algorithms


Procedure repeatedly calling itself eg: Factorial Procedure

function fact (n : integer):integer; {fact(n) computes n!} begin (1) if n<=1 then (2) fact:=1; else (3) fact:=n*fact(n-1); end; {fact}

Sample Data fact(3)=3 *fact(2) fact(2)=2*fact(1) fact(1)=1*fact(0) fact(0)=1 fact(1)=1 fact(2)=2 fact(3)=6

CS09 303 Data Structures - Module 1

Analysis of Factorial Procedure


function fact(n : integer):integer; {fact(n) computes n!} begin (1) if n<=1 then (2) fact:=1; else (3) fact:=n*fact(n-1); end; {fact}
CS09 303 Data Structures - Module 1

Time Complexity

O(1) O(1)

O(1) + T(n-1)

Computing total running time of factorial procedure T(n) = c+ T(n-1) if n>1 (1) =d if n<=1 Hence, T(n-1) = c + T((n-1)-1) = c + T(n-2) Substitute (2) in (1) T(n) = c + [c + T(n-2)] T(n) = 2c + T(n-2) Similarly, T(n-2) = c + T(n-3)

(2)

(3) (4)

CS09 303 Data Structures - Module 1

Substitute (4) in (3) Thus, T(n) = 3c + T(n-3) Or, For n>k, T(n) = k.c + T(n-k) Finally, when k = n-1, T(n) = c(n-1) + T(1) = c(n-1) + d = cn c + d Therefore ,T(n) is O(n).

if n>3 .(5)

CS09 303 Data Structures - Module 1

The Towers of Hanoi

Goal: Move stack of rings to another peg


Rule 1: Can move only 1 ring at a time Rule 2: Can never have a larger ring on top of a smaller ring
CS09 303 Data Structures - Module 1

Solution
Move top (n-1) disks from A to B.

CS09 303 Data Structures - Module 1

The Towers of Hanoi


Move larger ring from A to C.

CS09 303 Data Structures - Module 1

The Towers of Hanoi


Move the (n-1) rings from B to C.

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

The Towers of Hanoi

CS09 303 Data Structures - Module 1

Towers of Hanoi - Complexity


For 1 rings we have 1 operations. For 2 rings we have 3 operations. For 3 rings we have 7 operations. For 4 rings we have 15 operations. In general, the cost is 2N

1 = O(2N).

Each time we increment N, we double the amount of work. This grows incredibly fast!
CS09 303 Data Structures - Module 1

Deriving Recurrence Relation function THanoi(n, A, B, C): THanoi (n-1, A, C, B); Move larger disc from A to C; THanoi (n-1, B, A, C); T(n) = T (n-1) + O(1) + T(n-1) = 2T(n-1) + O(1) //A to B using C //B to C using A

CS09 303 Data Structures - Module 1

T(n) = 2T(n-1) + O(1) = 2[2T(n-2) + O(1)] + O(1) = 2[2 [2T(n-3) + O(1)] + O(1)] + O(1) =23 T(n-3) + 22 O(1) + 2O(1) + O(1) : = 2n T(0) + (2n -1)O(1) Therefore, T(n) is O(2n). The algorithm takes exponential time.

CS09 303 Data Structures - Module 1

Additional Points
Space complexity Recursive algorithm(Fibonacci) Steps in recursion Recursion Vs Iteration Disadvantages of recursion

CS09 303 Data Structures - Module 1

Thank You

CS09 303 Data Structures - Module 1

Resmi N.G. References: Data Structures and Algorithms: Alfred V. Aho, John E. Hopcroft, Jeffrey D. Ullman A Practical Approach to Data Structures and Algorithms: Sanjay Pahuja

Module 2
Module II (18 hours) Linear Data Structures - Stacks Queues -Lists Dequeues - Linked List - singly, doubly linked and circular lists - Application of linked lists - Polynomial Manipulation - Stack & Queue implementation using Array & Linked List - Typical problems - Conversion of infix to postfix - Evaluation of postfix expression priority queues

9/29/2012

CS 09 303 Data Structures - Module 2

LIST
Flexible data structure: grows and shrinks on demand. Elements can be accessed, inserted, or deleted at any position within

a list.
Lists can be concatenated or split into sub-lists. Mathematically, a list is a sequence of zero or more elements of a

given type. Represented by a comma separated sequence of elements a1,a2,an where n>=0 and each ai is of a given type.
n is the length of the list.
9/29/2012 CS 09 303 Data Structures - Module 2 3

Features Of List
Can be linearly ordered according to their position on the list. Concatenation and splitting possible Main application areas are: => Information retrieval => Programming language translation => Programming language simulation

9/29/2012

CS 09 303 Data Structures - Module 2

LIST OPERATIONS
INSERT(x, p,L) : Inserts x at position p in list L. Move elements at p and following positions to next higher positions. Insert x at position p in list L. If L is a1, a2, , an, then L becomes a1, a2, , ap-1, x, ap, , an . If p is END (L), then L becomes a1, a2, , an, x.

9/29/2012

CS 09 303 Data Structures - Module 2

END(L) : returns the last position of list L LOCATE(x, L) : returns the position of x on list L.
If x appears more than once, it returns the position of first occurrence of x. If x does not appear at all, then it returns END(L).

RETRIEVE(p,L) : returns element at position p on list L.


Result is undefined if p = END (L) or if L has no position p.

9/29/2012

CS 09 303 Data Structures - Module 2

DELETE(p, L) : deletes the element at position p of list L.


If list is a1,a2, , an, then the list L becomes a1, a2, ap-1, ap+1, ,an.

NEXT(p, L): returns position following p on list L. PREVIOUS(p, L):returns position preceding p on list L. MAKENULL(L) : Makes the list L empty and returns the position END(L). FIRST(L) : returns first position on list L. PRINTLIST(L) :prints elements of L in order of occurrence.

9/29/2012

CS 09 303 Data Structures - Module 2

Implementation Of List
2 Ways Array Implementation Pointer Implementation(Linked List representation)

9/29/2012

CS 09 303 Data Structures - Module 2

Array Implementation Of List


Elements are stored in contiguous cells of an array. Easily traversed Elements can be appended readily to the end of the list. Definition of LIST: is a record that contains 2 fields:
an array of elements with maximum required length an integer last : indicating position of the last list element in the array.

9/29/2012

CS 09 303 Data Structures - Module 2

Array Implementation Of List (Definition)


const MAXLEN = 100; type LIST = record elements:array[1..MAXLEN] of elementtype; last :integer; end;

position = integer;
9/29/2012 CS 09 303 Data Structures - Module 2 10

END(L) Algorithm
function END (var L: List) : position; begin return (L.last + 1) {gives total no. of elements in the list} end; {END}
0 50 1 107 2 15 3 201 L.last

9/29/2012

CS 09 303 Data Structures - Module 2

11

INSERTION Algorithm
procedure INSERT(x : elementtype; p : position; var L: LIST) {INSERT places x at position p in list L} var q:position; begin if L.last >= MAXLEN then error (list is full) else if (p > L.last + 1 ) or ( p < 1) then error(position does not exist) else begin for q := L.last downto p do {shift elements at p, p+1 down by one position} L.elements[q+1] := L.elements[q]; L.last := L.last+1; L.elements[p] := x; end end;{INSERT}
9/29/2012 CS 09 303 Data Structures - Module 2 12

Insertion
Insert (47, 0, L)
0 1 2 3

L.last := L.last + 1 = -1 + 1 = 0 L.elements[0] := 47;


47 p
9/29/2012

L.last
CS 09 303 Data Structures - Module 2 13

Insert (59, 1, L)
0 47 L.last p 1 2 3

L.last := L.last + 1 = 0 + 1 = 1 L.elements[1] := 59;


47 p 59 L.last

9/29/2012

CS 09 303 Data Structures - Module 2

14

Insert (65, 2, L)
0 47 1 59 L.last p 2 3

L.last := L.last + 1 = 1 + 1 = 2 L.elements[2] := 65;


47 59 p 65 L.last

9/29/2012

CS 09 303 Data Structures - Module 2

15

Insert (53, 1, L)
0 47 1 59 p q 2 65 L.last 3

Shift elements down to insert at position 1. Start with q = L.last. L.elements[q+1] := L.elements[q] { Move element at position 2 to position 3. L.elements[2+1] = L.elements[2] L.elements[3] = L.elements[2] }
47 59 p
9/29/2012 CS 09 303 Data Structures - Module 2

65 q L.last q+1
16

Decrement q until q = p. So, q = 1.


0 47 1 59 p q L.last 2 3 65

L.elements[q+1] := L.elements[q] { Move element at position 1 to position 2. L.elements[1+1] = L.elements[1] L.elements[2] = L.elements[1] }
47 p q
9/29/2012

59 q+1 L.last

65

CS 09 303 Data Structures - Module 2

17

Now, q = p = 1. So, insert element at position 1.


0 47 p q 1 2 59 L.last 3 65

L.last := L.last + 1 = 2+1 = 3; L.elements[p] := L.elements[1] = 53;

47

53 p q

59

65 L.last
18

9/29/2012

CS 09 303 Data Structures - Module 2

DELETION Algorithm
procedure DELETE (p:position; var L : LIST) {DELETE removes the element at position p of list L} var q:position; begin if ( p > L.last ) or ( p < 1) then error(position does not exist) else begin L.last := L.last-1; for q := p to L.last do {shift elements at p+1, p+2, up one position} L.elements[q] := L.elements[q+1]; end end;{DELETE}
9/29/2012 CS 09 303 Data Structures - Module 2 19

Deletion
Delete (1, L)
0 47 1 53 p 2 59 3 65

L.last

L.last := L.last - 1 = 3-1= 2

47

53 p

59 L.last

65

9/29/2012

CS 09 303 Data Structures - Module 2

20

To delete element at 1, shift elements at 2 and 3 up by one position. Start with q = p. ie. q = 1.
0 47 1 53 p q q+1 2 59 L.last 3 65

Move element at location q+1 = 2 to location q = 1. L.elements[q] := L.elements[q+1];

47

59 p q q+1 L.last

65

9/29/2012

CS 09 303 Data Structures - Module 2

21

Increment q until q = L.last = 2. So, q = q + 1 = 2.


0 47 1 59 p q 2 59 L.last 3 65 q+1

Move element at location q+1 = 3 to location q = 2. L.elements[q] := L.elements[q+1];


47 59 p
9/29/2012

65 q L.last
22

CS 09 303 Data Structures - Module 2

LOCATE Algorithm
function LOCATE( x:elementtype; var L:LIST) : position; {LOCATE returns position of x on list L} var q:position; begin for q:=1 to L.last do if L.elements[q] = x then return(q); else return( L.last + 1 ) {if not found} end;{LOCATE}

9/29/2012

CS 09 303 Data Structures - Module 2

23

Removing Duplicates in a LIST


Procedure PURGE (Var L:List); {PURGE removes duplicate elements from list L} Var p,q : position; {p will be the current position in L, and q will move ahead to find equal elements} Begin (1) p := FIRST(L); (2) While p <> END(L) do begin (3) q := NEXT(p, L); (4) while q <> END(L) do (5) if same (RETRIEVE (p, L), RETRIEVE (q, L ) then (6) DELETE(q,L); (7) else (8) q:=NEXT(q,L); (9) p:=NEXT(p,L) (10) End; End; {PURGE}
9/29/2012 CS 09 303 Data Structures - Module 2 24

Pointer Implementation Of List: (LINKED LIST)


Use of special data structure - Linked List Singly-linked cells which use pointers to link successive list

elements are used here. Does not require contiguous memory for storage. Allocates memory dynamically Linked List is made up of cells, and each cell contains: Element of the list Pointer to next cell on the list If the list is a1,a2,an, the cell holding ai has a pointer to the cell holding ai+1, for i = 1, 2, n-1. Header holds no element but points to cell holding a1. The cell holding an has a nil pointer.
CS 09 303 Data Structures - Module 2 25

9/29/2012

9/29/2012

CS 09 303 Data Structures - Module 2

26

Classification Of Linked Lists


Linear or singly linked list Doubly Linked List Circular Linked List Circular Doubly linked List

9/29/2012

CS 09 303 Data Structures - Module 2

27

Linear Singly Linked List


Definition type celltype = record element:elementtype; next: celltype; end; LIST= celltype; position = celltype;

9/29/2012

CS 09 303 Data Structures - Module 2

28

Operations Of Singly Linked List


END(L) Algorithm Function END(var L : List) : position; {END returns a pointer to the last cell of L} Var q:position; begin (1) q:=L; (2) While q .next <> nil do (3) q:= q .next; (4) return(q) end; {END}
9/29/2012 CS 09 303 Data Structures - Module 2 29

Operations Of Singly Linked List INSERTION


Steps involved are : Find the correct location Insert the element

9/29/2012

CS 09 303 Data Structures - Module 2

30

Finding the Correct Location


Three possible positions: The front The end Somewhere in between the existing cells

9/29/2012

CS 09 303 Data Structures - Module 2

31

Inserting to the Front


head 93 head 48 17 142

There is no work to find the location. Insertion at the beginning of the list

No Worries!
9/29/2012 CS 09 303 Data Structures - Module 2 32

Inserting to the End


head 48 17 142 93 //

//

Find the end of the list (when at NIL)

No Worries!
9/29/2012 CS 09 303 Data Structures - Module 2 33

Inserting to the Middle


head 17 48 142 93 142 //

//

Used when the order is important.

9/29/2012

CS 09 303 Data Structures - Module 2

34

Operations Of Singly Linked List(contd..)


INSERTION Algorithm Procedure INSERT(var x : elementtype, p : position); {Inserts a cell with element x after position p} Var temp : position; Begin (1) temp := p . next; (2) New(p .next); (3) p .next .element := x; (4) p .next .next := temp; End;{INSERT}

9/29/2012

CS 09 303 Data Structures - Module 2

35

Explanation
(1) temp := p . next; {address of the cell next to p is stored in temp}

head

4 p

6 Temp

9/29/2012

CS 09 303 Data Structures - Module 2

36

(2) New(p .next); {A new cell is created and its address is assigned to p .next}
head

4 p

6 temp p . next = address of new cell

p . next
9/29/2012 CS 09 303 Data Structures - Module 2 37

(3) p .next .element := x; {Element at p .next is assigned as x}

head

4 p

6 temp p . next . element = 5 5 p . next

9/29/2012

CS 09 303 Data Structures - Module 2

38

(3) p .next . next := temp; {Next of p .next is assigned as temp}

head

4 p

6 temp

p . next . next = temp 5 p . next


9/29/2012 CS 09 303 Data Structures - Module 2 39

head

9/29/2012

CS 09 303 Data Structures - Module 2

40

Operations Of Singly Linked List(contd..)


DELETION Algorithm Deletes the element after position p. Procedure DELETE( var p:position); Begin p .next := p .next .next; End;{DELETE}

9/29/2012

CS 09 303 Data Structures - Module 2

41

Deletion at the front


head
6 4 17 42

Head has the address of first node

9/29/2012

CS 09 303 Data Structures - Module 2

42

Deletion at the front


head
6 4 17 42

Head is made to point to the next cell.

9/29/2012

CS 09 303 Data Structures - Module 2

43

Deletion from in between the cells


head
4 17 42

9/29/2012

CS 09 303 Data Structures - Module 2

44

Deletion from in between the cells


head
4 17 42

9/29/2012

CS 09 303 Data Structures - Module 2

45

Deletion from in between the cells


p p . next = p . next . next p . next p . next . next

9/29/2012

CS 09 303 Data Structures - Module 2

46

Operations Of Singly Linked List(contd..)


LOCATE Algorithm Function LOCATE( var x : elementtype; var L:LIST):position; Var p : position; Begin p := L; while p . next <> nil do if p . next . element = x then return (p); else p := p . next ; return (p) {if not found} End;{LOCATE}

9/29/2012

CS 09 303 Data Structures - Module 2

47

Deleting a particular element


head

17

42

Target = 4

9/29/2012

CS 09 303 Data Structures - Module 2

48

head p 6

17

42

Start searching from the beginning of the list L. Initially, p = L; Target = 4

9/29/2012

CS 09 303 Data Structures - Module 2

49

head p 6

17

42

Check if p.next.element = 4. NO. So, move onto next cell. p = p.next Target = 4

9/29/2012

CS 09 303 Data Structures - Module 2

50

head

17

42

p Check if p.next.element = 4. YES. So, perform the operation p.next = p.next.next Target = 4

9/29/2012

CS 09 303 Data Structures - Module 2

51

head

17

42

p .next = p.next.next

Target = 4

9/29/2012

CS 09 303 Data Structures - Module 2

52

head

17

42

Target = 4

9/29/2012

CS 09 303 Data Structures - Module 2

53

head

17

42

9/29/2012

CS 09 303 Data Structures - Module 2

54

Operations Of Singly Linked List(contd..)


MAKENULL Algorithm Function MAKENULL(Var L:LIST):position; Begin new(L); L .next := nil; return(L); End; {MAKENULL}

Creates an empty list L


9/29/2012 CS 09 303 Data Structures - Module 2 55

Doubly Linked List


Each cell has a pointer to the next and previous cells on the list. Given an element, it is easy to determine the preceding and following elements quickly. Can be used to traverse a list in both forward and backward directions.

9/29/2012

CS 09 303 Data Structures - Module 2

56

9/29/2012

CS 09 303 Data Structures - Module 2

57

9/29/2012

CS 09 303 Data Structures - Module 2

58

Doubly Linked List


Definition Type celltype = record element : elementtype; next , previous : celltype; End; position = celltype; newnode = celltype;
9/29/2012 CS 09 303 Data Structures - Module 2 59

Doubly Linked List


INSERTION ALGORITHM Procedure INSERT( var element:elementtype; var pos:position) Var Temp : position; Begin Temp := L; i : =1; while (i < pos and Temp < > NIL ) begin Temp : = Temp . Next; i : = i + 1; End;
9/29/2012 CS 09 303 Data Structures - Module 2 60

If Temp < > NIL and i = pos then begin New( newNode); newNode .element : = element; newNode .next : = Temp .next; newNode . previous : = Temp; Temp .next . previous : = newNode; Temp . next := newNode; end { if } Else begin error ( position not found ) end; { INSERT}
9/29/2012 CS 09 303 Data Structures - Module 2 61

Explanation
temp := L; i : =1; temp 5 15 20 head

Check if i<pos, and temp<>NIL. i=1, pos = 2 and temp<>NIL. So, move forward. temp = temp.next i = i+1
9/29/2012 CS 09 303 Data Structures - Module 2 62

head

5 temp

15

20

Check if i<pos, and temp<>NIL. i=2, pos = 2 and temp<>NIL. So, do insertion.

9/29/2012

CS 09 303 Data Structures - Module 2

63

If Temp < > NIL and i = pos then begin new( newNode); {Creates a new node in memory and assigns its address to newnode.}

newNode .element : = element; {Inserts element at newnode.}


10 head

5 temp

15

20

9/29/2012

CS 09 303 Data Structures - Module 2

64

head

5 temp

15

20

10

newNode .next : = temp .next;

9/29/2012

CS 09 303 Data Structures - Module 2

65

head

5 temp

15

20

10

newNode . previous : = temp;

9/29/2012

CS 09 303 Data Structures - Module 2

66

head temp . next

5 temp

15

20

temp .next . previous : = newNode;

10

9/29/2012

CS 09 303 Data Structures - Module 2

67

head

5 temp

15

20

temp .next : = newNode;


10

9/29/2012

CS 09 303 Data Structures - Module 2

68

head

10

15

20

9/29/2012

CS 09 303 Data Structures - Module 2

69

Deletion in Doubly Linked List

9/29/2012

CS 09 303 Data Structures - Module 2

70

DELETION ALGORITHM Procedure DELETE ( Var p : position); Begin if p . Previous < > nil then { deleted cell not the first } p . Previous . Next : = p . Next; if p . Next < > nil then { deleted cell not the last } p . Next . Previous : = p . Previous End { DELETE}

9/29/2012

CS 09 303 Data Structures - Module 2

71

p . previous . next = p . next;

p. next . previous = p . previous ;

9/29/2012

CS 09 303 Data Structures - Module 2

72

STACK
An Abstract Data Type Special List : insertions & deletions at one

end(top)
Also called LIFO(Last In First Out) list.
9/29/2012 CS 09 303 Data Structures - Module 2 73

9/29/2012

CS 09 303 Data Structures - Module 2

74

9/29/2012

CS 09 303 Data Structures - Module 2

75

Operations performed on stack


MAKENULL(S) : Make stack S an empty stack. TOP(S) : Return the top element of stack S. POP(S) : Delete the top element from the stack. {check for stack empty condition} PUSH(x,S) : Insert the element x at the top of stack S. { Stack overflow condition should be checked} EMPTY(S) : Returns true if S is an empty stack.
9/29/2012 CS 09 303 Data Structures - Module 2 76

Stack Implementation
Static implementation(Using arrays) Dynamic implementation(Using pointers)

9/29/2012

CS 09 303 Data Structures - Module 2

77

Definition of Array-Based Stack


type STACK = record top : integer; elements : array[1..MAXLENGTH] of elementtype; end;

9/29/2012

CS 09 303 Data Structures - Module 2

78

Array Implementation
Procedure PUSH( var x:elementtype; Var s:STACK); Begin If s.top = MAXLENGTH -1 then error(stack is full) Else begin s.top:=s.top+1; s.elements[s.top]:=x; End; End;{PUSH}
9/29/2012 CS 09 303 Data Structures - Module 2 79

Initial stack
3 2 1 0
Top = -1

9/29/2012

CS 09 303 Data Structures - Module 2

80

Push (12, S)
s.top:=s.top+1 = -1 + 1 = 0; s.elements[s.top]:= s.elements[0] = 12; 3 2 1 0 12
Top = 0

9/29/2012

CS 09 303 Data Structures - Module 2

81

Push (34, S)
s.top:=s.top+1 = 0 + 1 = 1; s.elements[s.top]:= s.elements[1] = 34; 3 2 1 0 34 12
Top = 1

9/29/2012

CS 09 303 Data Structures - Module 2

82

Push (47, S)
s.top:=s.top+1 = 1 + 1 = 2; s.elements[s.top]:= s.elements[2] = 47; 3 2 1 0 47 34 12
Top = 2

9/29/2012

CS 09 303 Data Structures - Module 2

83

Push (53, S)
s.top:=s.top+1 = 2 + 1 = 2; s.elements[s.top]:= s.elements[3] = 53; 3 2 1 0 53 47 34 12
Top = 3

Stack full (Top = max-1);

9/29/2012

CS 09 303 Data Structures - Module 2

84

Procedure POP( Var s:STACK); begin if EMPTY(s) then error(stack is empty) else s.top := s.top-1; end;{POP}

9/29/2012

CS 09 303 Data Structures - Module 2

85

Procedure MAKENULL( Var s:STACK); Begin s.top:= -1; End;{MAKENULL}

9/29/2012

CS 09 303 Data Structures - Module 2

86

Function EMPTY( Var s:STACK):boolean; begin If s.top<0 then return(true); else return(false); end;{EMPTY}

9/29/2012

CS 09 303 Data Structures - Module 2

87

Function TOP ( Var s:STACK):elementtype; begin If EMPTY(s) then error(stack is empty) else return(s.elements[s.top]); end;{TOP}

9/29/2012

CS 09 303 Data Structures - Module 2

88

Applications of stack
Explicit and implicit recursive calls Conversion of Infix to postfix Postfix expression evaluation Conversion of infix to prefix Prefix expression evaluation Parenthesis checker

9/29/2012

CS 09 303 Data Structures - Module 2

89

Applications of stack
Expression : operands together with operator Infix notation(A+B) Prefix notation( +AB) Postfix notation( AB+) Operator precedence ^ (Exponential ) .Highest *, /(mul, div) Next highest +, - (add,sub) Lowest

9/29/2012

CS 09 303 Data Structures - Module 2

90

Applications of stack
Conversion from infix to postfix Rules 1. Parenthesize the expression starting from left to right 2. The operands associated with the operator having higher precedence will be parenthesized first. 3. The sub expression which has been converted to postfix is to be treated as single operand. 4. Once the expression is converted to postfix form, remove the parenthesis

9/29/2012

CS 09 303 Data Structures - Module 2

91

Infix to postfix conversion algorithm


P =>postfix expression Q =>infix expression (1) Push ( onto stack and add ) to the end of Q. (2) Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the stack is empty. (3) If an operand is encountered, add it to P. (4) If an operator # is encountered, then (a) Repeatedly pop from stack and add to P each operator (on the top of stack) which has equal or higher precedence than #.
9/29/2012 CS 09 303 Data Structures - Module 2 92

(b) Add # to stack. 5. If a left parenthesis is encountered, push it onto the stack. 6. If a right parenthesis is encountered then (a) Repeatedly pop from stack and add to P, until a left parenthesis is encountered. (b) Remove the left parenthesis. [ do not add the left parenthesis to P]. 7. Exit #
9/29/2012

Symbolizes any operator in Q


CS 09 303 Data Structures - Module 2 93

9/29/2012

CS 09 303 Data Structures - Module 2

94

9/29/2012

CS 09 303 Data Structures - Module 2

95

Evaluating Postfix expression


(1) Add a right parenthesis ) at the end of P[this acts as a

sentinel]. (2) Scan P from left to right and repeat steps 3 and 4 for each element of P until the sentinel ) is encountered. (3) If an operand is encountered, put it on STACK. (4) If an operator # is encountered, then (a) Remove the two top elements of STACK ,where A is the top element and B is the next-to-top element. (b) Evaluate B # A. (c) Place the result onto STACK. 5) RESULT equal to the top element on stack. 6) Exit
9/29/2012 CS 09 303 Data Structures - Module 2 96

Evaluate 45+72-*

9/29/2012

CS 09 303 Data Structures - Module 2

97

Evaluate 59+2*65*+

9/29/2012

CS 09 303 Data Structures - Module 2

98

Infix to prefix conversion algorithm


I => infix expression O => output expression P => prefix expression
(1) Push ) onto stack and add ( to the beginning of I. (2) Scan I from right to left and repeat steps 3 to 6 for each

element of I until the stack is empty. (3) If an operand is encountered, add it to O. (4) If an operator # is encountered, then (a) Repeatedly pop from stack and add to O each operator (on the top of stack) which has higher precedence than #.
9/29/2012 CS 09 303 Data Structures - Module 2 99

(b) Add # to stack. 5. If a right parenthesis is encountered, push it onto the stack. 6. If a left parenthesis is encountered then (a) Repeatedly pop from stack and add to O, until a right parenthesis is encountered. (b) Remove the right parenthesis. [ do not add the right parenthesis to O]. 7. Reverse O to get the prefix expression P. 8. Exit #
9/29/2012

Symbolizes any operator in I.


CS 09 303 Data Structures - Module 2 100

Convert (A*B-F*H)^D
Symbol Scanned D ^ ) H * F B * A ( ( Operator Stack ) )^ )^) )^) )^)* )^)* )^))^))^)-* )^)-* )^ (Empty) Output Expression D D D DH DH DHF DHF* DHF*B DHF*B DHF*BA DHF*BA*DHF*BA*-^

Result: ^-*AB*FHD
9/29/2012 CS 09 303 Data Structures - Module 2 101

Home Work
Convert to prefix: A+ [ B+C - D+E] * F / G +A/*+-+BCDEFG (ab)/c*(d + e f / g) */-abc-+de/fg

9/29/2012

CS 09 303 Data Structures - Module 2

102

Convert (ab)/c*(d + e f / g)

9/29/2012

CS 09 303 Data Structures - Module 2

*/-abc-+de/fg 103

Evaluating Prefix expression


(1) Add a left parenthesis ( at the beginning of P[this acts

as a sentinel]. (2) Scan P from right to left and repeat steps 3 and 4 for each element of P until the sentinel ( is encountered. (3) If an operand is encountered, push it onto STACK. (4) If an operator # is encountered, then (a) Remove the two top elements of STACK ,where A is the top element and B is the next-to-top element. (b) Evaluate A # B. (c) Place the result onto STACK. 5) RESULT equal to the top element on stack. 6) Exit
9/29/2012 CS 09 303 Data Structures - Module 2 104

Evaluate *+45-72

9/29/2012

CS 09 303 Data Structures - Module 2

105

Parenthesis Checker
To check whether a mathematical expression is properly parenthesized or not. 3 sets of grouping symbols The standard parenthesis () The braces { } The brackets []

9/29/2012

CS 09 303 Data Structures - Module 2

106

Algorithm for Parenthesis Checker


Make an empty stack. Read symbols until end of file is reached. If the symbol is an opening symbol, push it onto the stack. If it is a closing symbol, do the following: If the stack is empty, then report an error. Otherwise, pop the stack. If the symbol popped does not match the closing symbol, then report an error. At the end of the file, if the stack is not empty, then report an error.

9/29/2012

CS 09 303 Data Structures - Module 2

107

Algorithm for parenthesis checker


Begin read expression for each character c in expression if(current symbol or character is a left symbol) then push the character onto stack. else if (current symbol is a right symbol ) then if(stack is empty) then print (Error no matching open symbol) exit

9/29/2012

CS 09 303 Data Structures - Module 2

108

else pop a symbol s from the stack if (s does not correspond to c ) then print error incorrect nesting of symbols exit end {if} end {if} end {if} end {for}

9/29/2012 CS 09 303 Data Structures - Module 2 109

if (stack is not empty ) then print error missing closing symbol else print input expression is OK end {if} END

9/29/2012

CS 09 303 Data Structures - Module 2

110

Self-Study (Homework)
Stack using Linked List Write algorithms to:
Push an element onto stack Pop an element Create an empty stack Check if stack is empty Return the top element from the stack

9/29/2012

CS 09 303 Data Structures - Module 2

111

QUEUES
Abstract data type which is special kind of List where items are

inserted at one end (rear) and deleted from the other end (front).
Insertion :Rear Deletion :Front Also called FIFO(First In First Out) list.
9/29/2012 CS 09 303 Data Structures - Module 2 112

front

rear

9/29/2012

CS 09 303 Data Structures - Module 2

113

QUEUE OPERATIONS
MAKENULL(Q) : makes queue Q an empty list FRONT(Q) : returns first element of Q ENQUEUE(x, Q) : inserts element x at end of Q DEQUEUE(Q) : deletes first element of Q EMPTY(Q) : returns true if Q is an empty queue.

9/29/2012

CS 09 303 Data Structures - Module 2

114

9/29/2012

CS 09 303 Data Structures - Module 2

115

9/29/2012

CS 09 303 Data Structures - Module 2

116

IMPLEMENTATION OF QUEUE
Using Arrays (Static implementation) Using Pointers (Dynamic implementation)

9/29/2012

CS 09 303 Data Structures - Module 2

117

Array Implementation
Type QUEUE = record elements : array[1..MAXLENGTH] of elementtype; front, rear: integer; end;

9/29/2012

CS 09 303 Data Structures - Module 2

118

Queue operations using Arrays


Insertion Operation procedure ENQUEUE( Var data:integer, Var Q: QUEUE) begin read(data); If Q.rear >= SIZE then begin error(Queue Overflow); exit(); end else Q.rear := Q.rear+1; end; Q.elements[Q.rear] := data; end;{ENQUEUE}
(1)

9/29/2012

CS 09 303 Data Structures - Module 2

119

(1)Empty Queue
Rear := -1 Front := -1 0 rear front 1 2 3

(2)ENQUEUE(10,Q)
Front := 0 Rear := rear + 1 = 0 Q[Rear] := Q[0] = 10

10
0 rear 1 front 2 3

(3) ENQUEUE(3,Q) Rear = rear + 1 = 0 + 1 = 1 Q[Rear] := Q[1] = 3

10
0 front 1

3
2 3 rear
120

9/29/2012

CS 09 303 Data Structures - Module 2

(4)ENQUEUE(17,Q)
Rear := rear + 1 = 1 + 1 =2 Q[Rear] := Q[2] = 17

10
0 1 front

17
2 rear 3

(5)ENQUEUE(29,Q)
Rear := rear + 1 = 2+1 = 3 Q[Rear] := Q[3] = 29

10
0 front 1

17
2

29
3 rear

Queue Full (rear = max - 1)

9/29/2012

CS 09 303 Data Structures - Module 2

121

Deletion Operation Function DEQUEUE(Var Q: QUEUE) : elementtype; Begin if Q.rear < Q.front then begin Q.front:=0; Q.rear:= -1; error(Queue is empty); exit(); end else begin dequeue := Q.elements[Q.front]; Q.front:= Q.front+1; end; end;{DEQUEUE}
(2)

9/29/2012

CS 09 303 Data Structures - Module 2

122

(1)DEQUEUE(Q)
data := Q[front] = Q[0] =10 front := front + 1 = 0 + 1 = 1 0 1 front

17
2

29
3 rear

(1)DEQUEUE(Q)
data := Q[front] = Q[1] = 3 front := front + 1 = 1 + 1 = 2

17
0 1 front 2

29
3 rear

(2)DEQUEUE(Q)
data := Q[front] = Q[2] = 17 front := front + 1 = 2 + 1 = 3

29
0 1 2 3 front rear

9/29/2012

CS 09 303 Data Structures - Module 2

123

(4)DEQUEUE(Q)
data := Q[front] = Q[3] = 29 front := front + 1 = 3 + 1 = 4 0 1 2 3 rear front = 4

Queue empty (rear < front)

9/29/2012

CS 09 303 Data Structures - Module 2

124

Queue operations using Pointers


(1) Definition of Queue

type celltype = record element: elementtype; next : celltype; end; type QUEUE = record front, rear : celltype; end;
9/29/2012 CS 09 303 Data Structures - Module 2 125

(1) To Make a NULL QUEUE procedure MAKENULL(Var Q:QUEUE); begin new(Q.front);{create header cell } Q.front .next := nil; Q.rear := Q.front; {header is both first &last cell } end;{MAKENULL}

9/29/2012

CS 09 303 Data Structures - Module 2

126

(2) Check if QUEUE is empty function EMPTY(Var Q:QUEUE): boolean; begin if Q.front = Q.rear then return(true); else return(false); end;{EMPTY}

9/29/2012

CS 09 303 Data Structures - Module 2

127

(3) Returning first element from a queue

function FRONT(Var Q:QUEUE):elementtype begin If EMPTY(Q) then error(Queue is empty); else return(Q . front . next . element) end;{FRONT}

9/29/2012

CS 09 303 Data Structures - Module 2

128

(4) Insertion into a queue

Procedure ENQUEUE( var x:elementtype;Var Q:Queue); begin new(Q.rear . next); {add new cell to rear of queue} Q.rear := Q.rear . next; Q. rear .element := x; Q.rear .next:=nil; end; {ENQUEUE}

9/29/2012

CS 09 303 Data Structures - Module 2

129

(5) Deletion from a queue

Function DEQUEUE(Var Q:Queue): elementtype; Begin if EMPTY(Q) then error(queue is empty) else begin dequeue := Q.front .element; Q.front := Q.front .next; end; end; {DEQUEUE}
9/29/2012 CS 09 303 Data Structures - Module 2 130

Different Types Of Queue


Circular Queue Double ended Queue Priority Queue

9/29/2012

CS 09 303 Data Structures - Module 2

131

Circular Queue

9/29/2012

CS 09 303 Data Structures - Module 2

132

CIRCULAR QUEUE
Q[1] following Q[n] Conditions of circular Queue: Insertion Condition
rear = (rear+1) % SIZE

Deletion Condition
front = (front + 1) % SIZE

QUEUE FULL
rear = front
9/29/2012 CS 09 303 Data Structures - Module 2 133

Algorithms of Circular Queue


Insertion Procedure ENQUEUE(Var data:integer,Var Q : QUEUE); Begin if Q.front = ((Q.rear + 1) % SIZE) then begin error(Queue Full) exit(); end else begin Q.rear := (Q.rear + 1) % SIZE; end; if Q.front = -1 then begin Q.front := 0; Q.rear := 0; end; read(data); Q.elements[Q.rear] := data; end;{ENQUEUE}
9/29/2012 CS 09 303 Data Structures - Module 2 134

Queue is empty (front = rear = -1)


0 1

Rear=-1 Front=-1

9/29/2012

CS 09 303 Data Structures - Module 2

135

Enqueue (14, Q) Rear := (rear + 1) % SIZE = (-1 + 1) % 4 = 0 % 4 = 0 Q.elements[0] := 14;


Rear = 0

0
Front = 0

1 14

3
9/29/2012 CS 09 303 Data Structures - Module 2

2
136

Enqueue (42, Q) Rear := (rear + 1) % SIZE = (0 + 1) % 4 = 1 % 4 = 1 Q.elements[1] := 42;


1 14 42

0
Front = 0

Rear = 1

3
9/29/2012 CS 09 303 Data Structures - Module 2

2
137

Enqueue (20, Q) Rear := (rear + 1) % SIZE = (1 + 1) % 4 = 2 % 4 = 2 Q.elements[2] := 20;


1 14 42

0
Front = 0

20 3
9/29/2012 CS 09 303 Data Structures - Module 2

Rear = 2
138

Enqueue (37, Q) Rear := (rear + 1) % SIZE = (2 + 1) % 4 = 3 % 4 = 3 Q.elements[3] := 37;


0
Front = 0

1 14 42

Queue full
20 2
CS 09 303 Data Structures - Module 2 139

37
Rear = 3
9/29/2012

Queue full front := (rear + 1) % SIZE = (3 + 1) % 4 = 4 % 4 = 0


0
Front = 0

1 14 42

37
Rear = 3
9/29/2012

20 2

3
CS 09 303 Data Structures - Module 2

140

Deletion Function DEQUEUE(Var Q : QUEUE): elementtype; begin if Q.front = -1 then begin error(Queue is empty) exit(); end else dequeue := Q.elements[Q.front]; Q.front := (Q.front + 1) % SIZE; end;{DEQUEUE}

9/29/2012

CS 09 303 Data Structures - Module 2

141

Is this situation possible in a circular queue?

9/29/2012

CS 09 303 Data Structures - Module 2

142

Double-ended Queue (DEQUEUE)


Homogeneous list Insertion &deletion can be done at both ends. 2 types of dequeue: (1) Input restricted queue (insertion: rear end deletion: both ends) An input-restricted dequeue is one where deletion can be made from both ends, but insertion can only be made at one end. (2)Output restricted queue (insertion: both ends deletion: front end) An output-restricted dequeue is one where insertion can be made at both ends, but deletion can be made from one end only.
9/29/2012 CS 09 303 Data Structures - Module 2 143

Operations on Dequeue
(1) Add an element at the rear end ( insert right) (2)Delete an element from the front end ( delete left ) (3) Add an element at the front end ( insert left ) (4) Delete an element from the rear end (delete right )

9/29/2012

CS 09 303 Data Structures - Module 2

144

Insert an element at the right side of dequeue


procedure insertRightDequeue(Var data: integer, Var Q: QUEUE); Begin read(data); if ((Q.left = 0 && Q.right = MAX - 1) || (Q.left = Q.right + 1)) then begin error(Queue overflow ); exit(); end;

9/29/2012 CS 09 303 Data Structures - Module 2 145

if Q.left = -1 then begin Q.left := 0; Q.right := 0; end else begin if Q.right = MAX-1 then Q.right := 0; else Q.right := Q.right+1; end; Q.elements[Q.right] := data; end; {insertRightDequeue}

9/29/2012

CS 09 303 Data Structures - Module 2

146

Insert an element at the right side of dequeue


Empty Queue (If left = right = -1)
0 1 2 3

l r

9/29/2012

CS 09 303 Data Structures - Module 2

147

insertRightDequeue ( 5, Q) Set left = 0, right = 0; Insert at 0. Q.elements[right] : = Q.elements[0] = 5;


0 5 left = 0 right = 0 1 2 3

9/29/2012

CS 09 303 Data Structures - Module 2

148

insertRightDequeue ( 2, Q) Increment right. Right := right + 1 = 0 + 1 = 1; Insert at right. Q.elements[right] : = Q.elements[1] = 2;


0 5 left = 0 1 2 right = 1 2 3

9/29/2012

CS 09 303 Data Structures - Module 2

149

insertRightDequeue ( 10, Q) Increment right. Right := right + 1 = 1 + 1 = 2; Insert at right. Q.elements[right] : = Q.elements[2] = 10;
0 5 left = 0 1 2 2 10 right = 2 3

9/29/2012

CS 09 303 Data Structures - Module 2

150

insertRightDequeue ( 17, Q) Increment right. Right := right + 1 = 2 + 1 = 3; Insert at right. Q.elements[right] : = Q.elements[3] = 17;
0 5 left = 0 1 2 2 10 3 17 right = 3

Queue full (left = 0 and right = MAX 1 = 4 1 = 3)

9/29/2012

CS 09 303 Data Structures - Module 2

151

Perform a delete operation. So, left will be incremented by one. left := left + 1 = 0 + 1 = 1;

1 2 left = 1

2 10

3 17 right = 3

9/29/2012

CS 09 303 Data Structures - Module 2

152

insertRightDequeue (15, Q) 15 can be inserted at 0. If left <> -1 and right = MAX 1, there are vacant positions at the beginning of the queue. Set right = 0 and insert at 0.
0 15 right = 0 1 2 left = 1 2 10 3 17

Queue full (left = right + 1= 0 + 1 = 1)

9/29/2012

CS 09 303 Data Structures - Module 2

153

Insert an element at the left side of dequeue


procedure insertLeftDequeue(Var data:integer, Var Q: QUEUE); Begin read(data); if((Q.left = 0 && Q.right = MAX - 1) | | (Q.left = Q.right+1)) then begin error(Queue overflow ); exit(); end

9/29/2012

CS 09 303 Data Structures - Module 2

154

if Q.left = -1 then begin Q.left := 0; Q.right := 0; end else begin if Q.left = 0 then Q.left := MAX-1; else Q.left := Q.left-1; end; Q.elements[Q.left] := data; end; {insertLeftDequeue}
9/29/2012 CS 09 303 Data Structures - Module 2 155

Insert an element at the left side of dequeue


Empty Queue (If left = right = -1)
0 1 2 3

l r

9/29/2012

CS 09 303 Data Structures - Module 2

156

insertLeftDequeue ( 5, Q) Set left = 0, right = 0; Insert at 0. Q.elements[left] : = Q.elements[0] = 5;


0 5 left = 0 right = 0 1 2 3

9/29/2012

CS 09 303 Data Structures - Module 2

157

insertLeftDequeue ( 2, Q) If left = 0, set left = MAX - 1. left := MAX - 1 = 4 - 1 = 3; Insert at left. Q.elements[left] : = Q.elements[3] = 2;
0 5 right = 0 1 2 3 2 left = MAX- 1

9/29/2012

CS 09 303 Data Structures - Module 2

158

insertLeftDequeue ( 10, Q) Decrement left. left := left - 1 = 3 - 1 = 2; Insert at left. Q.elements[left] : = Q.elements[2] = 10;
0 5 right = 0 1 2 10 left = 2 3 2

9/29/2012

CS 09 303 Data Structures - Module 2

159

insertLeftDequeue ( 17, Q) Decrement left. left := left - 1 = 2 - 1 = 1; Insert at left. Q.elements[left] : = Q.elements[1] = 17;
0 5 right = 0 1 17 left = 1 2 10 3 2

Queue full (left = right + 1 = 0 + 1 = 1)

9/29/2012

CS 09 303 Data Structures - Module 2

160

Perform a delete operation. Right will be set to MAX 1 so that element at zero is deleted first. right := MAX - 1 = 4 - 1 = 3;

1 17 left = 1

2 10

3 2 right = 3

9/29/2012

CS 09 303 Data Structures - Module 2

161

insertLeftDequeue (15, Q) 15 can be inserted at 0. If left <> -1 and left <> 0, there are vacant positions at the beginning of the queue. Set left := left - 1 = 2 -1 = 0 and insert at 0.
0 15 left = 0 1 2 2 10 3 17 right = 3

Queue full (left = 0 and right = MAX - 1)

9/29/2012

CS 09 303 Data Structures - Module 2

162

Dequeue Operations(Normal Queue Operations)


Insert Right (Rear) and Delete Left (Front) Insert Left (Front) and Delete Right (Rear)

9/29/2012

CS 09 303 Data Structures - Module 2

163

Delete an element from the right side of dequeue


procedure deleteRightDequeue(Var Q: QUEUE); Begin Var data: integer; if((Q.left = -1) then begin error(Queue underflow ); exit(); end;{if} data := Q.elements[Q.right];
9/29/2012 CS 09 303 Data Structures - Module 2 164

if (Q.left = Q.right ) then begin {only one element} Q.left:= -1; Q.right := -1; end;{if} if (Q.right = 0) then Q.right = MAX -1; else Q.right = Q.right -1; end; { deleteRightDequeue}

9/29/2012

CS 09 303 Data Structures - Module 2

165

Delete an element from the right side of dequeue

deleteRightDequeue ( Q) Right = 0. So, delete the first element. data := Q.elements[right] = Q.elements[0] = 5; Set Right := MAX - 1 = 3; {Next deletion occurs at right = 3}.
0 5 right = 0 1 2 left = 1 2 10 3 17

9/29/2012

CS 09 303 Data Structures - Module 2

166

deleteRightDequeue ( Q) Right = 3. data := Q.elements[right] = Q.elements[3] = 17; Set Right := right - 1 = 3 1 = 2;


0 1 2 left = 1 2 10 3 17 right = 3

9/29/2012

CS 09 303 Data Structures - Module 2

167

deleteRightDequeue ( Q) Right = 2. data := Q.elements[right] = Q.elements[2] = 10; Set Right := right - 1 = 2 1 = 1;


0 1 2 left = 1 2 10 right = 2 3

9/29/2012

CS 09 303 Data Structures - Module 2

168

deleteRightDequeue (Q) data : = Q.elements[right] : = Q.elements[1] = 5; Left = right = 1; {only one element} Set left := -1, right := -1; {Queue becomes empty}
0 1 2 left = 1 right = 1 2 3

9/29/2012

CS 09 303 Data Structures - Module 2

169

deleteRightDequeue (Q) Empty Queue (left = right = -1) Cannot perform deletion

l r

9/29/2012

CS 09 303 Data Structures - Module 2

170

Delete an element from the left side of dequeue


procedure deleteLeftDequeue(Var data:integer,Var Q: QUEUE); Begin if((Q.left = -1) then begin error(Queue underflow ); exit(); end;{if} data:=Q.elements[Q.left];
9/29/2012 CS 09 303 Data Structures - Module 2 171

if Q.left = Q.right then begin {only one element} Q.left:= -1; Q.right := -1; end;{if} if ( Q.left = MAX - 1 ) then Q.left = 0; else Q.left = Q.left + 1; end; { deleteLeftDequeue}

9/29/2012

CS 09 303 Data Structures - Module 2

172

Delete an element from the left side of dequeue


deleteLeftDequeue ( Q) Left = 0. So, delete the first element. data := Q.elements[left] = Q.elements[0] = 12; Set left := left + 1 = 1; {Next deletion occurs at left = 1}.
0 12 left = 0 1 27 2 60 3 18 right = 3

9/29/2012

CS 09 303 Data Structures - Module 2

173

deleteLeftDequeue ( Q) Left = 1. data := Q.elements[left] = Q.elements[1] = 27; Set left := left + 1 = 1 + 1 = 2;


0 1 27 left = 1 2 60 3 18 right = 3

9/29/2012

CS 09 303 Data Structures - Module 2

174

deleteLeftDequeue ( Q) Left = 2. data := Q.elements[left] = Q.elements[2] = 60; Set left := left + 1 = 2 + 1 = 3;


0 1 2 60 left = 2 3 18 right = 3

9/29/2012

CS 09 303 Data Structures - Module 2

175

deleteLeftDequeue (Q) data : = Q.elements[left] : = Q.elements[3] = 18; Left = right = 3; {only one element} Set left := -1, right := -1; {Queue becomes empty}
0 1 2 3 18 left = 3 right = 3

9/29/2012

CS 09 303 Data Structures - Module 2

176

deleteLeftDequeue (Q) Empty Queue (left = right = -1) Cannot perform deletion

l r

9/29/2012

CS 09 303 Data Structures - Module 2

177

Example
Insert right (rear)

0 21 left = 0 right = 0

9/29/2012

CS 09 303 Data Structures - Module 2

178

Insert left (front)

0 21 right = 0

3 42 left = 3

9/29/2012

CS 09 303 Data Structures - Module 2

179

Insert right (rear)

0 21

1 35 right = 1

3 42 left = 3

9/29/2012

CS 09 303 Data Structures - Module 2

180

Insert left (front)

0 21

1 35 right = 1

2 5 left = 2

3 42

9/29/2012

CS 09 303 Data Structures - Module 2

181

Delete left (front)

0 21

1 35 right = 1

3 42 left = 2

9/29/2012

CS 09 303 Data Structures - Module 2

182

Delete right (rear)

0 21 right = 0

3 42 left = 2

9/29/2012

CS 09 303 Data Structures - Module 2

183

Delete right (rear)

3 42 left = 2 right = 3

9/29/2012

CS 09 303 Data Structures - Module 2

184

Delete left (rear)

l ,r = -1

9/29/2012

CS 09 303 Data Structures - Module 2

185

PRIORITY QUEUE
Collection of elements such that each element is assigned a priority number. Rules are: An element of higher priority is processed before any element of lower priority. Elements of same priority are processed according to the order in which they were added into the queue.

9/29/2012

CS 09 303 Data Structures - Module 2

186

Two types of priority queues


Ascending priority queue
Items are inserted arbitrarily and element with least priority is removed.

Descending priority queue


Items are inserted arbitrarily and element with highest priority is removed.

9/29/2012

CS 09 303 Data Structures - Module 2

187

Implementation
Implementation of priority queue: (1) One way list representation (2) Multiple Queues (one for each priority - Array representation) (3) Maximum or minimum heap

9/29/2012

CS 09 303 Data Structures - Module 2

188

One Way List Representation


Each node contains: INFO: Information field PRT : Priority number NEXT: Next pointer A node x precedes a node y in the list when x has higher priority than y or when both have same priority but x was inserted before y.

9/29/2012

CS 09 303 Data Structures - Module 2

189

Operations performed: Searching an element with maximum priority Inserting an element Deleting an element with maximum priority

9/29/2012

CS 09 303 Data Structures - Module 2

190

Insert A with priority number 2.

Start

A 2 NULL

9/29/2012

CS 09 303 Data Structures - Module 2

191

Insert B with priority number 1.

Start

B 1

A 2 NULL

9/29/2012

CS 09 303 Data Structures - Module 2

192

Insert C with priority number 2.

Start

B 1

A 2

C 2 NULL

9/29/2012

CS 09 303 Data Structures - Module 2

193

Delete operation deletes element with highest priority.

Start

B 1

A 2

C 2 NULL

9/29/2012

CS 09 303 Data Structures - Module 2

194

Delete operation deletes element with highest priority.

Start

A 2

C 2 NULL

9/29/2012

CS 09 303 Data Structures - Module 2

195

Insertion algorithm of one way list representation of priority queue


(1) Traverse the one way list until you find a node X

whose priority number exceeds N (item to be inserted).


(2) Insert ITEM N in front of node X. (3) If no such node is found, insert ITEM as the last element

of the list.

9/29/2012

CS 09 303 Data Structures - Module 2

196

Deletion algorithm of one way list representation of priority queue


(1) Set ITEM := INFO[START] { This saves the data in the

first node }
(2) Delete first node from the list (3) Process ITEM (4) Exit

9/29/2012

CS 09 303 Data Structures - Module 2

197

Array Implementation
As multiple queues. Multiple queues are defined in the same array queue[] of size n. Each queue i is allocated a predefined space bounded by array indices. A queue is maintained for each priority. In order to process an element of the priority queue, element from non-empty highest priority number queue is accessed.
9/29/2012 CS 09 303 Data Structures - Module 2 198

front[0]

rear[0] front[1]

rear[1] front[2]

rear[2] front[3]

rear[3]

Queue1

Queue2

Queue3

Queue4

9/29/2012

CS 09 303 Data Structures - Module 2

199

Insertion algorithm of array representation of priority queue


(1) Insert ITEM as the rear element in row M of QUEUE. (2)Exit

9/29/2012

CS 09 303 Data Structures - Module 2

200

Deletion algorithm of array representation of priority queue


(1)Find the smallest k such that FRONT[K]!=NULL. {Find the first non-empty queue} (2)Delete and process the front in row k of queue. (3) Exit

9/29/2012

CS 09 303 Data Structures - Module 2

201

Comparison of array & one way list representation


Array :Time efficient One way list :Space efficient

9/29/2012

CS 09 303 Data Structures - Module 2

202

Applications of Queue
Round robin techniques :Process scheduling Print server routines All types of customer service software(Railway/airticket reservation)

9/29/2012

CS 09 303 Data Structures - Module 2

203

Application of linked lists Polynomial Manipulation


Polynomial of single variable of degree m f(x) = amxm + am-1xm-1 + + aixi + + a1x + a0 ai are non-zero coefficients with exponents i. Each term is represented by a node with three fields, which represent the coefficient, exponent of a term and a pointer to the next term.

9/29/2012

CS 09 303 Data Structures - Module 2

204

A term of a polynomial of single variable x:


powerx coef next

p = 6x5 + 2x3 + x + 4 Store the polynomial in descending order of powers.


5 6 1570 1526 3 2 1607 1570 1 1 1750 1607 0 4 NULL 1750

9/29/2012

CS 09 303 Data Structures - Module 2

205

A term of a polynomial of variables x, y, z:


Power x Power y Power z coef next

p = 2x3 + 3xy + y2 + yz Store the polynomial such that terms are arranged in descending order of powers of x first, then y and then z.
3002 1103 0201 0111

9/29/2012

CS 09 303 Data Structures - Module 2

206

Addition of Two Polynomials of Single variable


p = 6x5 + 2x3 + x + 4 q = 2x4 + 10 x + 1
6x5 6x5 + + 2x4 2x4 2x3 + + 2x3 + + x + 4 1 5

10 x + 11 x +

9/29/2012

CS 09 303 Data Structures - Module 2

207

5 6 1570
ptrp

3 2 1607 1570 1 10 1700 1660

1 1 1750 1607 0 1 NULL 1700

0 4 NULL 1750

1526

4 2 1660
ptrq

1500

p.powerx > q.powerx (Add first term of p. Increment ptrp)


5 6
ptrr

1826

9/29/2012

CS 09 303 Data Structures - Module 2

208

5 6 1570 1526 4 2 1660


ptrq

3 2 1607
ptrp

1 1 1750 1607 0 1 NULL 1700

0 4 NULL 1750

1570

1 10 1700 1660

1500

p.powerx < q.powerx


5 6 1870 1826
ptrr

4 2 Null 1870

9/29/2012

CS 09 303 Data Structures - Module 2

209

5 6 1570 1526 4 2 1660 1500

3 2 1607
ptrp

1 1 1750 1607 0 1 NULL 1700

0 4 NULL 1750

1570

1 10 1700
ptrq

1660

p.powerx > q.powerx


5 6 1870 1826 4 2 1860 1870
ptrr

3 2 Null 1860

9/29/2012

CS 09 303 Data Structures - Module 2

210

5 6 1570 1526 4 2 1660 1500

3 2 1607 1570 1 10 1700


ptrq

1 1 1750
ptrp

0 4 NULL 1750

1607

0 1 NULL 1700

1660

p.powerx = q.powerx (Add the coefficients and increment both the pointers)
5 6 1870 1826 4 2 1860 1870 3 2 1807 1860
ptrr

1 11 Null 1807

9/29/2012

CS 09 303 Data Structures - Module 2

211

5 6 1570 1526 4 2 1660 1500

3 2 1607 1570 1 10 1700 1660

1 1 1750 1607 0 1 NULL


ptrq

0 4 NULL
ptrp

1750

1700

p.powerx = q.powerx (Add the coefficients)


5 6 1870 1826 4 2 1860 1870 3 2 1807 1860 1 11 1950 1807 0 5
9/29/2012 CS 09 303 Data Structures - Module 2

NULL 1950
212

ptrr

Algorithm for polynomial addition (single variable)


1. Repeat upto step 3 while there are terms in both polynomials yet to be processed. 2. Obtain values for terms from each polynomial. 3. If the powers associated with the two terms are equal then If the sum of coefficients of both terms is not zero then Insert sum of the coefficients into the sum polynomial. Obtain the next terms from both polynomials.

9/29/2012

CS 09 303 Data Structures - Module 2

213

Else if the power of first polynomial > power of the second polynomial then
Insert the term from first polynomial into sum polynomial. Obtain the next term in the first polynomial.

Else
Insert the term from second polynomial into sum polynomial. Obtain the next term in the second polynomial. 4. Copy remaining terms from non-empty polynomial into sum polynomial. 5. Return the address of the sum polynomial.
9/29/2012 CS 09 303 Data Structures - Module 2 214

Thank You

9/29/2012

CS 09 303 Data Structures - Module 2

215

Resmi N.G. References: Data Structures and Algorithms: Alfred V. Aho, John E. Hopcroft, Jeffrey D. Ullman A Practical Approach to Data Structures and Algorithms: Sanjay Pahuja

Syllabus
Non Linear Structures - Graphs - Trees - Graph and Tree implementation using array and Linked List -Binary trees - Binary tree traversals - pre-order, in-order and postorder - Threaded binary trees Binary Search trees - AVL trees B trees and B+ trees - Graph traversals - DFS, BFS shortest path - Dijkstras algorithm, Minimum spanning tree - Kruskal Algorithm, Prims algorithm

9/29/2012

CS 09 303 Data Structures - Module 3

Trees

9/29/2012

CS 09 303 Data Structures - Module 3

Trees
A tree is a collection of nodes, one of which is designated as the root, along with a relation (parenthood). Represents hierarchical relationship. A node can be of any type.

9/29/2012

CS 09 303 Data Structures - Module 3

Trees
A tree can be recursively defined as : A single node by itself is a tree. This node is called the root of the tree. A tree is a finite set of one or more nodes such that there is a specially designated node called the root and the remaining nodes are partitioned into n>=0 disjoint sets T1, T2, ,Tn, where each of these sets is a subtree. Every node except the root has a parent node associated to it.
9/29/2012 CS 09 303 Data Structures - Module 3 5

TREE TERMINOLGIES
Root :Specially designed first node Degree of a node: Number of subtrees of a node Degree of a Tree :Maximum degree of any node in a given tree. Terminal node/Leaf node: Node with degree zero.

9/29/2012

CS 09 303 Data Structures - Module 3

Non terminal node : Any node (except root) whose

degree is non-zero.
Siblings: Children nodes of same parent node. Level : If a node is at level n, then its children will be

at level n+1. Root is at level 0, next immediate is level 1 etc..


Path: If n1,n2,nk be the node sequence of a tree such

that ni is the parent of ni+1 for 1<=i<k, then this sequence is called a path from node n1 to node nk.
9/29/2012 CS 09 303 Data Structures - Module 3 7

Length of path :One less than the number of nodes in a

path.
Ancestor/descendant of a node: If there is a path from

node a to node b, then a is an ancestor of b, and b is descendant of a.


Proper Ancestor/Proper Descendant : An ancestor or

descendant of node ,other than the node itself is called proper ancestor or proper descendant respectively.

9/29/2012

CS 09 303 Data Structures - Module 3

Height of a node :Length of the longest path from the

node to a leaf.
Height of a tree: Height of the root. Depth of a node : Length of the unique path from root

to that node. Depth of a tree :One more than the maximum level of any node in a given tree.

9/29/2012

CS 09 303 Data Structures - Module 3

Order of nodes
Children of a node are usually ordered from left-to-right.

These are two distinct ordered trees.


9/29/2012 CS 09 303 Data Structures - Module 3 10

If a and b are siblings, and a is to the left of b, then all the descendants of a are to the left of all descendants of b. A tree in which the order of nodes is ignored is referred to as unordered tree.

9/29/2012

CS 09 303 Data Structures - Module 3

11

Tree Traversal (Orderings)


Preorder Inorder Postorder These orderings are recursively defined as:
If a tree t is null (with no nodes), then the empty list is the preorder, inorder and postorder listing of T. If T consists of a single node, then that node itself is the preorder, inorder and postorder listing of T.
9/29/2012 CS 09 303 Data Structures - Module 3 12

Otherwise, let T be a tree with root n and subtrees T1, T2, , Tk.

T1

T2

Tk

The preorder listing of the nodes of T is the root n of T followed by the nodes of T1 in preorder, then the nodes of T2 in preorder, and so on, upto the nodes of Tk in preorder.

9/29/2012

CS 09 303 Data Structures - Module 3

13

The inorder listing of the nodes of T is the nodes of T1 in inorder, followed by the root n of T , followed by the nodes of T2, , Tk in inorder. The postorder listing of the nodes of T is the nodes of T1 in postorder, then the nodes of T2 in postorder, and so on, upto Tk, all followed by node n.

9/29/2012

CS 09 303 Data Structures - Module 3

14

Preorder Procedure
Procedure PREORDER (n : node) begin (1) list n; (2) for each child c of n, if any, in order from the left do PREORDER(c) end;

9/29/2012

CS 09 303 Data Structures - Module 3

15

a a b c

9/29/2012

CS 09 303 Data Structures - Module 3

16

a b a b c

9/29/2012

CS 09 303 Data Structures - Module 3

17

a b d a b c

9/29/2012

CS 09 303 Data Structures - Module 3

18

a b d e a b c

9/29/2012

CS 09 303 Data Structures - Module 3

19

a b d e c a b c

9/29/2012

CS 09 303 Data Structures - Module 3

20

a b d e c f a b c

9/29/2012

CS 09 303 Data Structures - Module 3

21

a b d e c f g a b c

9/29/2012

CS 09 303 Data Structures - Module 3

22

A, B, D, H, I, E, C, F, J, G, K
9/29/2012 CS 09 303 Data Structures - Module 3 23

Postorder Procedure
Procedure POSTORDER (n : node) begin (1) for each child c of n, if any, in order from the left do POSTORDER(c) (2) list n; end;

9/29/2012

CS 09 303 Data Structures - Module 3

24

d a b c

9/29/2012

CS 09 303 Data Structures - Module 3

25

d e a b c

9/29/2012

CS 09 303 Data Structures - Module 3

26

d e b a b c

9/29/2012

CS 09 303 Data Structures - Module 3

27

d e b f a b c

9/29/2012

CS 09 303 Data Structures - Module 3

28

d e b f g a b c

9/29/2012

CS 09 303 Data Structures - Module 3

29

d e b f g c a b c

9/29/2012

CS 09 303 Data Structures - Module 3

30

d e b f g c a a b c

9/29/2012

CS 09 303 Data Structures - Module 3

31

H, I, D, E, B, J, F, K, G, C, A
9/29/2012 CS 09 303 Data Structures - Module 3 32

Inorder Procedure
Procedure INORDER (n : node) begin if n is a leaf then list n; else begin INORDER (leftmost child of n); list n; for each child c of n, except for the leftmost, in order from the left do INORDER(c) end end;
9/29/2012 CS 09 303 Data Structures - Module 3 33

d a b c

9/29/2012

CS 09 303 Data Structures - Module 3

34

d b a b c

9/29/2012

CS 09 303 Data Structures - Module 3

35

d b e a b c

9/29/2012

CS 09 303 Data Structures - Module 3

36

d b e a a b c

9/29/2012

CS 09 303 Data Structures - Module 3

37

d b e a f a b c

9/29/2012

CS 09 303 Data Structures - Module 3

38

d b e a f c a b c

9/29/2012

CS 09 303 Data Structures - Module 3

39

d b e a f c g a b c

9/29/2012

CS 09 303 Data Structures - Module 3

40

H, D, I, B, E, A, J, F, C, K, G
9/29/2012 CS 09 303 Data Structures - Module 3 41

Labeled Trees and Expression Trees


Every leaf is labeled by an operand and consists of that operand alone. Every interior node n is labeled by an operator. Suppose, n is labeled by a binary operator, #, and that the left child represents expression E1 and the right child E2. Then, n represents (E1) # (E2).

9/29/2012

CS 09 303 Data Structures - Module 3

42

n1 = (a + b) * (a + c)

*
n2 = (a + b)

+ a b a

n3 = (a + c)

n4 = (a)

n7 = (c)

n5 = (b) n6 = (a)

9/29/2012

CS 09 303 Data Structures - Module 3

43

The preorder listing of labels in an expression tree gives the prefix form of the expression, where the operator precedes its left and right operands. The prefix expression for (E1)#(E2), with # a binary operator, is #P1P2 , where P1 and P2 are the prefix expressions for E1 and E2 respectively.

9/29/2012

CS 09 303 Data Structures - Module 3

44

The postorder listing of labels in an expression tree gives the postfix form of the expression, where left and right operands precede the operator. The postfix expression for (E1)#(E2), with # a binary operator, is P1P2#, where P1 and P2 are the postfix expressions for E1 and E2 respectively. The inorder listing of labels in an expression tree gives the infix expression.

9/29/2012

CS 09 303 Data Structures - Module 3

45

The ADT Tree Operations


PARENT (n, T) : Returns the parent of node n in tree T.
If n is the root, it returns NULL.

LEFTMOST_CHILD (n, T) : Returns the leftmost child of node n in tree T.


It returns NULL if n is a leaf.

RIGHT_SIBLING (n, T) : Returns the right sibling of node n in tree T, defined to be that node m with same parent p as n such that m lies immediately to the right of n in the ordering of children of p.
9/29/2012 CS 09 303 Data Structures - Module 3 46

LABEL (n, T) : Returns the label of node n in tree T. CREATEi (v, T1, T2, , Ti) : For each value of i = 0, 1, 2, CREATEi makes a new node r with label v and gives it i children, which are the roots of trees T1, T2, , Ti, in order from the left. The tree with root r is returned. ROOT (T) : Returns the node that is the root of tree T, or returns NULL T is null tree. MAKENULL (T) : makes T the null tree.
9/29/2012 CS 09 303 Data Structures - Module 3 47

IMPLEMENTATION OF TREE
Array Representation Linked List representation

9/29/2012

CS 09 303 Data Structures - Module 3

48

IMPLEMENTATION OF TREES
Array Representation (Parent Representation) Uses the property of trees that each node has a unique parent. Uses a linear array A where A[i] =j, if node j is parent of node i, and A[i]=0,if node i is the root. It supports LABEL operator, where L be an array with L[i], the Label of node i.
9/29/2012 CS 09 303 Data Structures - Module 3 49

9/29/2012

CS 09 303 Data Structures - Module 3

50

With this representation, the parent of a node can be found in constant time. Limitations: Lacks child-of information. Given a node n, it is expensive to determine the children of n, or the height of n. Parent pointer representation does not specify the order of the children of a node.

9/29/2012

CS 09 303 Data Structures - Module 3

51

Definition type node = integer; TREE = array[1..MAXNODES] of node;

9/29/2012

CS 09 303 Data Structures - Module 3

52

Right Sibling Operation function RIGHT_SIBLING (n:node; T:Tree): node; {returns right sibling of node n in tree T} Var i,parent:node ; begin parent:=T[n]; for i = n+1 to maxnodes do {search for node after n with same parent} if T[i] = parent then return(i); return(0);{null node will be returned if no right sibling is ever found} End;(RIGHT_SIBLING}

9/29/2012

CS 09 303 Data Structures - Module 3

53

Linked List representation Of Tree


Way of representing trees where a list of children is formed for each node. Header : An array of header cells, indexed by nodes. Each header points to a linked list of nodes. Elements on the list headed by header[i] are the children of node i.
9/29/2012 CS 09 303 Data Structures - Module 3 54

9/29/2012

CS 09 303 Data Structures - Module 3

55

Definition Type node = integer; LIST = {appropriate definition for list of nodes}; position = {appropriate definition for positions in lists}; TREE = record header : array[1..maxnodes] of LIST; labels : array[1..maxnodes] of labeltype; root : node; End;
9/29/2012 CS 09 303 Data Structures - Module 3 56

LEFTMOST-CHILD Operation function LEFTMOST_CHILD (n:node ;T:Tree): node; {returns the leftmost child of node n of tree T} Var L : LIST {list of ns children} begin L:= T.header[n]; if EMPTY(L) then {n is a leaf} return(0) else return (RETRIEVE(FIRST(L),L); End;{LEFTMOST_CHILD}

9/29/2012

CS 09 303 Data Structures - Module 3

57

Definition of cellspace For PARENT Operation


T.header[n] points directly to the first cell of the list. Var cellspace :array[1..maxnodes] of record node :integer; next :integer; end;

9/29/2012

CS 09 303 Data Structures - Module 3

58

PARENT Operation function PARENT(n:node ;T:Tree): node; {returns the parent of node n of tree T} Var p: node; {runs through possible parents of n} i: position; {runs down the list of ps children} begin for p:=1 to maxnodes do begin i := T.header[p]; while i <> 0 do { see if n is among children of p} if cellspace[i].node = n then return(p)

9/29/2012

CS 09 303 Data Structures - Module 3

59

else i:= cellspace[i].next; end; return(0); end;{PARENT} {returns null node if parent not found}

9/29/2012

CS 09 303 Data Structures - Module 3

60

Shortcoming Inability to create large trees from smaller trees using CREATEi operator.

9/29/2012

CS 09 303 Data Structures - Module 3

61

Leftmost-Child, Right-Sibling Representation of a Tree


Definition Of Node Space Var nodespace :array[1..maxnodes] of record label:labeltype; header: integer; {cursor to cellspace} end;

9/29/2012

CS 09 303 Data Structures - Module 3

62

Definition Of Cell Space Var cellspace :array[1..maxnodes] of record label:labeltype; leftmost_child :integer; right_sibling: integer; end;

9/29/2012

CS 09 303 Data Structures - Module 3

63

9/29/2012

CS 09 303 Data Structures - Module 3

64

9/29/2012

CS 09 303 Data Structures - Module 3

65

Binary Trees
Definition A binary tree is a tree data structure in which each node has at most two child nodes, usually distinguished as "left child" and "right child". A binary tree can be defined as : (1) either an empty tree, or (2) a tree in which every node has either no children, a left child, a right child, or both a left and a right child.
9/29/2012 CS 09 303 Data Structures - Module 3 66

9/29/2012

CS 09 303 Data Structures - Module 3

67

Two Distinct Binary Trees


9/29/2012 CS 09 303 Data Structures - Module 3

An Ordinary Tree
68

Left-Skewed: If a binary tree has only left subtree, it is called left-skewed. Right-Skewed: If a binary tree has only right subtree, it is called right-skewed.

9/29/2012

CS 09 303 Data Structures - Module 3

69

9/29/2012

CS 09 303 Data Structures - Module 3

70

In a binary tree a degree of every node is maximum two. Binary tree with n nodes has exactly (n-1) edges. A full binary tree is a tree in which every node other than the leaves has two children. A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.

9/29/2012

CS 09 303 Data Structures - Module 3

71

Complete Binary Tree

9/29/2012

CS 09 303 Data Structures - Module 3

72

Full Binary Tree

A full binary tree of depth k has 2k -1 nodes, k>=0.

9/29/2012

CS 09 303 Data Structures - Module 3

73

Binary Tree Representations


Array Representation Array stores nodes Nodes accessed sequentially Root node index starts with 1. SIZE of array=(2^d)-1 where d=depth of tree

9/29/2012

CS 09 303 Data Structures - Module 3

74

9/29/2012

CS 09 303 Data Structures - Module 3

75

9/29/2012

CS 09 303 Data Structures - Module 3

76

9/29/2012

CS 09 303 Data Structures - Module 3

77

Binary Tree Representations


Pointer Based Implementation Binary Tree linked list declaration as type node = record leftchild : node; rightchild: node; parent : node; end;
9/29/2012 CS 09 303 Data Structures - Module 3 78

Binary Trees: Pointer Based Implementation


CREATION Algorithm Function create (lefttree, righttree: node): node; Var root : node; begin new ( root); root .leftchild := lefttree; root .rightchild := righttree; root .parent:=0; lefttree .parent := root; righttree .parent:=root; return (root) end;{create}
9/29/2012 CS 09 303 Data Structures - Module 3 79

INSERTION Algorithm Function insert(root: node,digit:number): node; Var root : node; begin If root = NULL then begin new(root); root .leftchild:=NULL; root .rightchild:=NULL; root .data := digit; count := count+1; end;{if}
9/29/2012 CS 09 303 Data Structures - Module 3 80

else if count %2 = 0 then begin root .leftchild :=insert(root .leftchild, digit); else root .rightchild := insert(root .rightchild, digit); end;{elseif} return(root); end;{insert}

9/29/2012

CS 09 303 Data Structures - Module 3

81

Binary Trees Operations


TRAVERSAL Pre-order(Node-left-right) In-Order(Left-Node-Right) Post-Order(Left-Right-Node)

9/29/2012

CS 09 303 Data Structures - Module 3

82

Recursive TRAVERSAL Pre-order Algorithm Steps: (1) Visit the root node (2) Traverse the left subtree in pre-order (3) Traverse the right subtree in pre-order

9/29/2012

CS 09 303 Data Structures - Module 3

83

Recursive TRAVERSAL Pre-order Algorithm procedure preorder (root : node) begin if root <> NULL begin write (root .data); preorder(root .lchild); preorder(root .rchild); end{if} end;{preorder}

9/29/2012

CS 09 303 Data Structures - Module 3

84

Recursive TRAVERSAL In-order Algorithm Steps: (1) Traverse the left subtree in inorder (2) Visit the root node (3) Traverse the right subtree in inorder

9/29/2012

CS 09 303 Data Structures - Module 3

85

Recursive TRAVERSAL Inorder Algorithm procedure inorder(root : node) begin if root <> NULL begin inorder(root .lchild); write (root .data); inorder(root .rchild); end{if} end;{inorder}

9/29/2012

CS 09 303 Data Structures - Module 3

86

Recursive TRAVERSAL Postorder Algorithm Steps: (1) Traverse the left subtree in postorder (2) Traverse the right subtree in postorder (3) Visit the root node

9/29/2012

CS 09 303 Data Structures - Module 3

87

Recursive TRAVERSAL
Postorder Algorithm procedure postorder(root : node) begin if root <> NULL begin postorder(root .lchild); postorder(root .rchild); write (root .data); end{if} end;{preorder}

9/29/2012

CS 09 303 Data Structures - Module 3

88

Binary Tree Vs General Tree


Binary Tree : May be empty Tree : cannot be empty Binary Tree : Exactly 2 subtrees Tree : Any number of subtrees Binary Tree : Ordered Tree : Unordered
9/29/2012 CS 09 303 Data Structures - Module 3 89

Binary Search Tree

9/29/2012

CS 09 303 Data Structures - Module 3

90

9/29/2012

CS 09 303 Data Structures - Module 3

91

Binary Search Tree Operations


Inserting a node
Searching a node Deleting a node

9/29/2012

CS 09 303 Data Structures - Module 3

92

BST INSERTION
procedure INSERT (x: elementtype, var A: Set)); {add x to set A} Begin if A = NIL then begin new (A); A . element := x; A . leftchild := NIL; A . rightchild := NIL; end
9/29/2012 CS 09 303 Data Structures - Module 3 93

else if x < A . element then INSERT (x, A .leftchild); else if x > A . element then INSERT (x, A .rightchild); {if x = A . element then, do nothing; x is already in the set} End; {INSERT}

9/29/2012

CS 09 303 Data Structures - Module 3

94

9/29/2012

CS 09 303 Data Structures - Module 3

95

BST SEARCH
function MEMBER (x: elementtype, var A : SET) : boolean; {returns true if x is in A, false otherwise} Begin if A = NIL then return (false) else if x = A .element then return (true) else if x < A .element then return (MEMBER (x, A .leftchild)) else {x > A .element} return (MEMBER (x, A .rightchild)) End; {MEMBER}
9/29/2012 CS 09 303 Data Structures - Module 3 96

BST DELETION
(1) Deleting leaf node - Just delete the leaf node (2) Deleting a node with a single child (either a left child or a right child) - Replace the node with its left (or right) child. (3) Deleting a node with both left and right child - Replace the node to be deleted with its inorder successor (with smallest value in its right subtree) and then delete the node.
9/29/2012 CS 09 303 Data Structures - Module 3 97

BST DELETION
Procedure DELETE (x: elementtype, var A : SET); {remove x from set A} Begin if A <> NIL then if x < A .element then DELETE (x, A .leftchild) else if x > A .element then DELETE(x, A .rightchild)) {if we reach here, x is at the node pointed to by A}
9/29/2012 CS 09 303 Data Structures - Module 3 98

else if A .leftchild=NIL and A .rightchild=NIL then A := NIL; {delete the leaf holding x} else if A .leftchild=NIL then {A has only right child} A := A .rightchild; else if A .rightchild=NIL then {A has only left child} A := A .leftchild; else {both children are present} A .element := DELETEMIN (A .rightchild); End; {DELETE}

9/29/2012

CS 09 303 Data Structures - Module 3

99

Function DELETEMIN (var A : SET) : elementtype; {returns and removes the smallest element from set A} Begin if A .leftchild = NIL then begin {A points to the smallest element} DELETEMIN := A .element; A := A .rightchild); {replace the node pointed to by A by its right child} end else {the node pointed to by A has a left child} DELETEMIN := DELETEMIN (A .leftchild); End; {DELETEMIN}
9/29/2012 CS 09 303 Data Structures - Module 3 100

(1)

Deleting leaf node with value 13

9/29/2012

CS 09 303 Data Structures - Module 3

101

(2)

Deleting node with single (right)child (with value 16)

Replace the node with its right child (with value 20).

9/29/2012

CS 09 303 Data Structures - Module 3

102

(3)

Deleting node with left and right child (with value 5)

Replace the node with its inorder successor and delete the node.

9/29/2012

CS 09 303 Data Structures - Module 3

103

BST Traversal

Inorder : 3 5 6 7 10 12 13 15 16 18 20 23 (sorted in ascending order) Preorder : 15 5 3 12 10 6 7 13 16 20 18 23 Postorder : 3 7 6 10 13 12 5 18 23 20 16 15


9/29/2012 CS 09 303 Data Structures - Module 3 104

Deleting a node with two children from a BST


Rule 1: Find the largest node of left subtree (inorder predecessor). Rule 2: Find the smallest node of right subtree(inorder successor). A node's in-order successor is the left-most child of its right subtree, and a node's in-order predecessor is the right-most child of its left subtree.

9/29/2012

CS 09 303 Data Structures - Module 3

105

Node to be deleted : node with value 7. The triangles represent subtrees of arbitrary size. Rule 1: Find the largest node of left subtree.

Rule 2: Find the smallest node of right subtree.


9/29/2012 CS 09 303 Data Structures - Module 3 106

Binary Search trees Vs Arrays


Advantages: Complexity of searching: O (log2N) Better insertion time: O (log2N) Vs O(N) Better deletion time Disadvantage: BST requires more memory space to store the two pointer references to left and right child for each data element.

9/29/2012

CS 09 303 Data Structures - Module 3

107

Application of BST
Sorting: We can sort the data by reading it, item by item, and constructing a BST as we go. The inorder traversal of a BST gives the elements in ascending order. A sorted array can be produced from a BST by traversing the tree in inorder and inserting each element sequentially into the array as it is visited. Time Complexity -?????
9/29/2012 CS 09 303 Data Structures - Module 3 108

Threaded Trees
Binary trees have a lot of wasted space: each of the leaf nodes has 2 null pointers. We can use these pointers to help us in inorder traversals. We have the pointers that reference the next node in an inorder traversal; called threads. To know whether a pointer is an actual link or a thread, a boolean variable can be maintained for each pointer.
9/29/2012 CS 09 303 Data Structures - Module 3 109

Threaded Tree Example


6 3 1 5 7 9 8 11 13

9/29/2012

CS 09 303 Data Structures - Module 3

110

Threaded Binary Trees


A binary tree is threaded according to a particular traversal order. A binary tree is threaded by making all right child pointers that would normally be null point to the inorder successor of the node, and all left child pointers that would normally be null point to the inorder predecessor of the node.

9/29/2012

CS 09 303 Data Structures - Module 3

111

Types: Single Threaded: each node is threaded towards either the inorder predecessor or successor. Double threaded: each node is threaded towards both the inorder predecessor and successor.

9/29/2012

CS 09 303 Data Structures - Module 3

112

Threads are references to the predecessors and successors of the node according to an inorder traversal. Inorder of the threaded tree is ABCDEFGHI.

9/29/2012

CS 09 303 Data Structures - Module 3

113

9/29/2012

CS 09 303 Data Structures - Module 3

114

Inorder: DBAEC

9/29/2012

CS 09 303 Data Structures - Module 3

115

Threaded Tree Traversal


We start at the leftmost node in the tree, print it, and follow its right thread. If we follow a thread to the right, we output the node and continue to its right. If we follow a link to the right, we go to the leftmost node, print it, and continue.

9/29/2012

CS 09 303 Data Structures - Module 3

116

Threaded Tree Traversal


6 3 1 5 7 9
Start at leftmost node, print it
9/29/2012 CS 09 303 Data Structures - Module 3

Output 1

8 11 13

117

Threaded Tree Traversal


6 3 1 5 7 9
Follow thread to right, print node
9/29/2012 CS 09 303 Data Structures - Module 3

Output 1 3

8 11 13

118

Threaded Tree Traversal


6 3 1 5 7 9
Follow link to right, go to leftmost node and print
9/29/2012 CS 09 303 Data Structures - Module 3

8 11 13

Output 1 3 5

119

Threaded Tree Traversal


6 3 1 5 7 9
Follow thread to right, print node
9/29/2012 CS 09 303 Data Structures - Module 3

8 11 13

Output 1 3 5 6

120

Threaded Tree Traversal


6 3 1 5 7 9
Follow link to right, go to leftmost node and print
9/29/2012 CS 09 303 Data Structures - Module 3

8 11 13

Output 1 3 5 6 7

121

Threaded Tree Traversal


6 3 1 5 7 9
Follow thread to right, print node
9/29/2012 CS 09 303 Data Structures - Module 3

8 11 13

Output 1 3 5 6 7 8

122

Threaded Tree Traversal


6 3 1 5 7 9
Follow link to right, go to leftmost node and print
9/29/2012 CS 09 303 Data Structures - Module 3

8 11 13

Output 1 3 5 6 7 8 9

123

Threaded Tree Traversal


6 3 1 5 7 9
Follow thread to right, print node
9/29/2012 CS 09 303 Data Structures - Module 3

8 11 13

Output 1 3 5 6 7 8 9 11

124

Threaded Tree Traversal


6 3 1 5 7 9
Follow link to right, go to leftmost node and print
9/29/2012 CS 09 303 Data Structures - Module 3

8 11 13

Output 1 3 5 6 7 8 9 11 13

125

9/29/2012

CS 09 303 Data Structures - Module 3

126

Advantages
The traversal operation is faster than that of its unthreaded version, because with threaded binary tree non-recursive implementation is possible which can run faster. We can efficiently determine the predecessor and successor nodes starting from any node (no stack required). Any node is accessible from any other node. Threads are usually upward whereas links are downward. Thus, in a threaded tree, one can move in either direction and nodes are in fact circularly linked.
9/29/2012 CS 09 303 Data Structures - Module 3 127

Limitations of BSTs
BSTs can become highly unbalanced (In worst case, time complexity for BST operations become O(n)).
root A C F M Z
9/29/2012 CS 09 303 Data Structures - Module 3 128

Height-balanced Binary Search Trees


A self-balancing (or height-balanced) binary search tree is any binary search tree that automatically keeps its height (number of levels below the root) small after arbitrary item insertions and deletions. A balanced tree is a BST whose every node above the last level has non-empty left and right subtree. Number of nodes in a complete binary tree of height h is 2h+1 1. Hence, a binary tree of n elements is balanced if: 2h 1 < n <= 2h+1 - 1
9/29/2012 CS 09 303 Data Structures - Module 3 129

AVL Trees
Named after Russian Mathematicians: G.M. AdelsonVelskii and E.M. Landis who discovered them in 1962. An AVL tree is a binary search tree which has the following properties: The sub-trees of every node differ in height by at most one. Every sub-tree is an AVL tree.

9/29/2012

CS 09 303 Data Structures - Module 3

130

9/29/2012

CS 09 303 Data Structures - Module 3

131

Implementations of AVL tree insertion rely on adding an extra attribute, the balance factor to each node. Balance factor (bf) = HL - HR An empty binary tree is an AVL tree. A non-empty binary tree T is an AVL tree iff : | HL - HR | <= 1 For an AVL tree, the balance factor, HL - HR , of a node can be either 0, 1 or -1.
9/29/2012 CS 09 303 Data Structures - Module 3 132

The balance factor indicates whether the tree is: left-heavy (the height of the left sub-tree is 1 greater than the right sub-tree), balanced (both sub-trees are of the same height) or right-heavy (the height of the right sub-tree is 1 greater than the left sub-tree).

9/29/2012

CS 09 303 Data Structures - Module 3

133

+1

0 0

+1

-1

0 AVL Tree 2

0 AVL Tree 0

-1

0 0

+1 Not an AVL Tree

9/29/2012

CS 09 303 Data Structures - Module 3

134

9/29/2012

CS 09 303 Data Structures - Module 3

135

9/29/2012

CS 09 303 Data Structures - Module 3

136

AVL Tree Insertion


Insertion is similar to that of a BST. After inserting a node, it is necessary to check each of the node's ancestors for consistency with the rules of AVL. If after inserting an element, the balance of any tree is destroyed then a rotation is performed to restore the balance.

9/29/2012

CS 09 303 Data Structures - Module 3

137

For each node checked, if the balance factor remains 1, 0, or +1 then no rotations are necessary. However, if balance factor becomes less than -1 or greater than +1, the subtree rooted at this node is unbalanced.

9/29/2012

CS 09 303 Data Structures - Module 3

138

Theorem: When an AVL tree becomes unbalanced after an insertion, exactly one single or double rotation is required to balance the tree. Let A be the root of the unbalanced subtree. There are four cases which need to be considered. Left-Left (LL) rotation Right-Right (RR) rotation Left-Right (LR) rotation Right-Left (RL) rotation
9/29/2012 CS 09 303 Data Structures - Module 3 139

LL Rotation: Inserted node is in the left subtree of left subtree of node A. RR Rotation: Inserted node is in the right subtree of right subtree of node A. LR Rotation: Inserted node is in the right subtree of left subtree of node A. RL Rotation: Inserted node is in the left subtree of right subtree of node A.

9/29/2012

CS 09 303 Data Structures - Module 3

140

LL Rotation: New element 2 is inserted in the left subtree of left subtree of A , whose bf becomes +2 after insertion.
+1 B 4 0 AR 0 BL BR BL 2 BR A 6 +1 4 AR B +2 A 6

To rebalance the tree, it is rotated so as to allow B to be the root with BL and A to be its left subtree and right child respectively, and BR and AR to be the left and right subtrees of A.
9/29/2012 CS 09 303 Data Structures - Module 3 141

B 0 4 0 BL 2 A 0 6

BR

AR

A.Leftchild = B.rightchild B.Rightchild = A

9/29/2012

CS 09 303 Data Structures - Module 3

142

RR Rotation: New element 10 is inserted in the right subtree of right subtree of A , whose bf becomes -2 after insertion.
-2 -1 A 6 B AL BL 0 8 BL BR BR 10 AL 8 A 6 B -1

To rebalance the tree, it is rotated so as to allow B to be the root with A as its left child and BR as its right subtree, and AL and BL as the left and right subtrees of A respectively.
9/29/2012 CS 09 303 Data Structures - Module 3 143

B 0 8 0 A 6 0 10

AL

BL

BR

A.Rightchild = B.leftchild B.leftchild = A

9/29/2012

CS 09 303 Data Structures - Module 3

144

Left-Left case and Left-Right case: If the balance factor of P is 2, then the left subtree outweighs the right subtree of the given node, and the balance factor of the left child L must be checked. The right rotation with P as the root is necessary. If the balance factor of L is +1, a single right rotation (with P as the root) is needed (Left-Left case). If the balance factor of L is -1, two different rotations are needed. The first rotation is a left rotation with L as the root. The second is a right rotation with P as the root (Left-Right case).
9/29/2012 CS 09 303 Data Structures - Module 3 145

9/29/2012

CS 09 303 Data Structures - Module 3

146

9/29/2012

CS 09 303 Data Structures - Module 3

147

Right-Right case and Right-Left case: If the balance factor of P is -2 then the right subtree outweighs the left subtree of the given node, and the balance factor of the right child (R) must be checked. The left rotation with P as the root is necessary. If the balance factor of R is -1, a single left rotation (with P as the root) is needed (Right-Right case). If the balance factor of R is +1, two different rotations are needed. The first rotation is a right rotation with R as the root. The second is a left rotation with P as the root (Right-Left case).
9/29/2012 CS 09 303 Data Structures - Module 3 148

9/29/2012

CS 09 303 Data Structures - Module 3

149

9/29/2012

CS 09 303 Data Structures - Module 3

150

9/29/2012

CS 09 303 Data Structures - Module 3

151

AVL Tree Deletion


If the node is a leaf or has only one child, remove it. Otherwise, replace it with either the largest in its left sub tree (in order predecessor) or the smallest in its right sub tree (in order successor), and remove that node. The node that was found as a replacement has at most one sub tree. After deletion, retrace the path back up the tree (parent of the replacement) to the root, adjusting the balance factors as needed.
9/29/2012 CS 09 303 Data Structures - Module 3 152

The retracing can stop if the balance factor becomes 1 or +1 indicating that the height of that subtree has remained unchanged. If the balance factor becomes 0 then the height of the subtree has decreased by one and the retracing needs to continue. If the balance factor becomes 2 or +2 then the subtree is unbalanced and needs to be rotated to fix it.

9/29/2012

CS 09 303 Data Structures - Module 3

153

Reference
Sanjay Pahuja, A Practical Approach to Data Structures and Algorithms, First Ed. 2007. For Height Balanced Trees: AVL, B-Trees, refer: Pg. 292 296, 301 315

9/29/2012

CS 09 303 Data Structures - Module 3

154

B-Trees
B-tree is a tree data structure that is a generalization of a binary search tree.ie; A node can have more than two children. It is also called Balanced M-way tree or balanced sort tree. A node of the tree may contain many records or keys and pointers to children. Used in external sorting. It is not a binary tree.
9/29/2012 CS 09 303 Data Structures - Module 3 155

Properties of B-tree of order M


(1) Each node(except the root and leaf) has maximum of M children and a minimum of ceil(M/2) children and for root, any number from 2 to maximum. (2) Each node has one fewer key than children with a maximum of M-1 keys.

9/29/2012

CS 09 303 Data Structures - Module 3

156

(3) Keys are arranged in a defined order within the node. All keys in the subtree to the left of a key are predecessors of the key and those to the right are successors of the key. (4) All leaves are on the same level. ie. There is no empty subtree above the level of the leaves.

9/29/2012

CS 09 303 Data Structures - Module 3

157

B-Tree Insertion
(1) (2) (3) Search and find the position for insertion. Add the key to the node if the node can accommodate it. If not, ie. if a new key is to be inserted into a full node, the node is split into two and the key with median value is inserted in parent node. Continue splitting upward, if required, until the root is reached. If parent node is the root node, and has to be split, a new root is created and the tree grows taller by one level.
9/29/2012 CS 09 303 Data Structures - Module 3 158

B-Tree Deletion
(1) Search and find the key to be deleted. (2) If the key is in a terminal node, the key along with appropriate pointer is deleted. (3) If the key is not in a terminal node, it is replaced by a copy of its successor(key with next higher value).

9/29/2012

CS 09 303 Data Structures - Module 3

159

(4) If on deleting the key, the new node size is lower than the minimum, an underflow occurs. (a) If either of adjacent siblings contains more than minimum number of keys, the central key is chosen from the collection:
contents of node with less than minimum number of keys, more than minimum number of keys, and the separating key from parent node.

This key is written back to parent; the left and right halves are written back to siblings.
9/29/2012 CS 09 303 Data Structures - Module 3 160

(b) If none of the adjacent siblings contains more than minimum number of keys, concatenation is used. The node is merged with its adjacent sibling and the separating key from its parent.

9/29/2012

CS 09 303 Data Structures - Module 3

161

B-Tree of Order 5 Example


All internal nodes have at least ceil(5 / 2) = ceil(2.5) = 3 children (and hence at least 2 keys), other than the root node. The maximum number of children that a node can have is 5 (so that 4 is the maximum number of keys). All nodes other than the root must have a minimum of 2 keys.

9/29/2012

CS 09 303 Data Structures - Module 3

162

B-Tree Order 5 Insertion Insert C N G A H E K Q M F W L T Z DPRXYS


Originally we have an empty B-tree of order 5. The first 4 letters get inserted into the same node

9/29/2012

CS 09 303 Data Structures - Module 3

163

CN GAHEK Q M FWLTZ DPRXYS


When we try to insert the H, we find no room in this node, so we split it into 2 nodes, moving the median item G up into a new root node.

9/29/2012

CS 09 303 Data Structures - Module 3

164

CN GAHEK Q M FWLTZ DPRXYS


Inserting E, K, and Q proceeds without requiring any splits.

9/29/2012

CS 09 303 Data Structures - Module 3

165

CN GAHEK Q M FWLTZ DPRXYS


Inserting M requires a split.

9/29/2012

CS 09 303 Data Structures - Module 3

166

CN GAHEK Q M FWLTZ DPRXYS


The letters F, W, L, and T are then added without any split.

9/29/2012

CS 09 303 Data Structures - Module 3

167

CN GAHEK Q M FWLTZ DPRXYS


When Z is added, the rightmost leaf must be split. The median item T is moved up into the parent node.

9/29/2012

CS 09 303 Data Structures - Module 3

168

CN GAHEK Q M FWLTZ DPRXYS


The insertion of D causes the leftmost leaf to be split. D happens to be the median key and so it is moved up into the parent node. The letters P, R, X, and Y are then added without any split.

9/29/2012

CS 09 303 Data Structures - Module 3

169

CN GAHEK Q M FWLTZ DPRXYS


Finally, when S is added, the node with N, P, Q, and R splits, sending the median Q up to the parent. The parent node is full, so it splits, sending the median M up to form a new root node.

9/29/2012

CS 09 303 Data Structures - Module 3

170

B-Tree Order 5 Deletion


Initial B-Tree

9/29/2012

CS 09 303 Data Structures - Module 3

171

Delete H
Since H is in a leaf and the leaf has more than minimum number of keys, we just remove it.

9/29/2012

CS 09 303 Data Structures - Module 3

172

Delete T
Since T is not in a leaf, we find its successor (the next item in ascending order), which happens to be W. Move W up to replace the T. ie; What we really have to do is to delete W from the leaf .

9/29/2012

CS 09 303 Data Structures - Module 3

173

B+ Trees
Variant of the original B-tree in which all records are stored in the leaves and all leaves are linked sequentially. The B+ tree is used as an indexing method in relational database management systems. All keys are duplicated in the leaves. This has the advantage that all the leaves are linked together sequentially, and hence the entire tree may be scanned without visiting the higher nodes at all.

9/29/2012

CS 09 303 Data Structures - Module 3

174

9/29/2012

CS 09 303 Data Structures - Module 3

175

The B + Tree consists of two types of nodes: (1) internal nodes and (2) leaf nodes Internal nodes point to other nodes in the tree. Leaf nodes point to data in the database using data pointers. Leaf nodes also contain an additional pointer, called the sibling pointer, which is used to improve the efficiency of certain types of search.

9/29/2012

CS 09 303 Data Structures - Module 3

176

The B + -Tree is a balanced tree because every path from the root node to a leaf node is the same length. A balanced tree is one in which all searches for individual values require the same number of nodes to be read from the disc. Order of a B + Tree The order of a B + Tree is the number of keys and pointers that an internal node can contain. An order size of m means that an internal node can contain m-1 keys and m pointers.
9/29/2012 CS 09 303 Data Structures - Module 3 177

Insertion in B+ Tree
Insert sequence : 5, 8, 1, 7, 3, 12, 9, 6 Order: 3 Empty Tree The B+Tree starts as a single leaf node. A leaf node consists of one or more data pointers and a pointer to its right sibling. This leaf node is empty.

9/29/2012

CS 09 303 Data Structures - Module 3

178

Inserting Key Value 5 To insert a key, search for the location where the key has to be inserted. Here, the B+Tree consists of a single leaf node, L1, which is empty. Hence, the key value 5 must be placed in leaf node L1.

9/29/2012

CS 09 303 Data Structures - Module 3

179

Inserting Key Value 8 Again, search for the location where key value 8 is to be inserted. This is in leaf node L1. There is room in L1; so insert the new key.

9/29/2012

CS 09 303 Data Structures - Module 3

180

Inserting Key Value 1 Searching for where the key value 1 should appear also results in L1 but L1 is now full as it contains the maximum two records.

9/29/2012

CS 09 303 Data Structures - Module 3

181

L1 must be split into two nodes. The first node will contain the first half of the keys and the second node will contain the second half of the keys.

9/29/2012

CS 09 303 Data Structures - Module 3

182

We now require a new root node to point to each of these nodes. We create a new root node and promote the rightmost key from node L1.

9/29/2012

CS 09 303 Data Structures - Module 3

183

Insert Key Value 7 Search for the location where key 7 is to be inserted, that is, L2. Insert key 7 into L2.

9/29/2012

CS 09 303 Data Structures - Module 3

184

Insert Key Value 3 Search for the location where key 3 is to be inserted. That is L1. But, L1 is full and must be split.

9/29/2012

CS 09 303 Data Structures - Module 3

185

The rightmost key in L1, i.e. 3, must now be promoted up the tree.

L1 was pointing to key 5 in B1. Therefore, all the key values in B1 to the right of and including key 5 are moved one position to the right.
9/29/2012 CS 09 303 Data Structures - Module 3 186

Insert Key Value 12 Search for the location where key 12 is to be inserted, L2. Try to insert 12 into L2 but L2 is full and it must be split.

As before, we must promote the rightmost value of L2 but B1 is full and so it must be split.
9/29/2012 CS 09 303 Data Structures - Module 3 187

Now the tree requires a new root node, so we promote the rightmost value of B1 into a new node.

9/29/2012

CS 09 303 Data Structures - Module 3

188

9/29/2012

CS 09 303 Data Structures - Module 3

189

Insert Key Value 9 Search for the location where key value 9 is to be inserted, L4. Insert key 9 into L4.

9/29/2012

CS 09 303 Data Structures - Module 3

190

Insert Key Value 6 Key value 6 should be inserted into L2 but it is full. Therefore, split it and promote the appropriate key value.

9/29/2012

CS 09 303 Data Structures - Module 3

191

Deletion in B+ Tree
Deletion sequence: 9, 8, 12.

9/29/2012

CS 09 303 Data Structures - Module 3

192

Delete Key Value 9 First, search for the location of key value 9, L4. Delete 9 from L4. L4 is not less than half full and the tree is correct.

9/29/2012

CS 09 303 Data Structures - Module 3

193

Delete Key Value 8 Search for key value 8, L5. Deleting 8 from L5 causes L5 to underflow, that is, it becomes less than half full.

9/29/2012

CS 09 303 Data Structures - Module 3

194

Redistribute some of the values from L2. This is possible because L2 is full and half its contents can be placed in L5.

As some entries have been removed from L2, its parent B2 must be adjusted to reflect the change.
9/29/2012 CS 09 303 Data Structures - Module 3 195

Deleting Key Value 12 Deleting key value 12 from L4 causes L4 to underflow. However, because L5 is already half full we cannot redistribute keys between the nodes. L4 must be deleted from the index and B2 adjusted to reflect the change.

9/29/2012

CS 09 303 Data Structures - Module 3

196

B+ Trees
Reference: http://www.mec.ac.in/resources/notes/notes/ds/bplus .htm

9/29/2012

CS 09 303 Data Structures - Module 3

197

GRAPH
Graph G = (V,E) is a collection of vertices and edges. (1) Set of vertices - V (2) Set of Edges - E

where V ={ V1,V2,V3..} E ={ e1,e2,e3..}


9/29/2012 CS 09 303 Data Structures - Module 3 198

9/29/2012

CS 09 303 Data Structures - Module 3

199

GRAPH TERMINOLOGIES
Directed Graph (Digraph) :A graph

where nodes are

connected with directed edges.


Arrow head is at vertex called head and other end is

called tail.

9/29/2012

CS 09 303 Data Structures - Module 3

200

LOOP: If an edge is incident from and into the same

vertex.
ADJACENT VERTICES :Two vertices are adjacent if

they are joined by an edge.


ISOLATED VERTEX : If there is no edge incident with

the vertex.

9/29/2012

CS 09 303 Data Structures - Module 3

201

ISOMORPHIC: Two graphs are said to be isomorphic if equal

number of vertices , edges and also corresponding images exist.


SUBGRAPH :Let G = (V,E) be a graph. Then, G1 = (V1,E1)

is a subgraph, if V1 is a subset of V and E1 is a subset of E.


SPANNING

/ INDUCED SUBGRAPH : A subgraph that

contains all vertices of G.


9/29/2012 CS 09 303 Data Structures - Module 3 202

DEGREE: The number of edges incident on a vertex. WEIGHTED

GRAPH :A graph in which every edge is assigned some weight or value.

PATH: A sequence of vertices. SIMPLE PATH : Path in which first and last vertices

are

distinct.
CYCLE : Length of path must be minimum 1 and begins and

ends at same vertex.


9/29/2012 CS 09 303 Data Structures - Module 3 203

LENGTH OF PATH: Number of edge arcs on the path. LABELED

DIGRAPH :A graph in which each arc or vertex has an associated label.

CONNECTED GRAPH : Graph in which there exists a

path from any vertex to any other vertex. Otherwise, it is said to be disconnected. {Disconnected graph contains components.}

9/29/2012

CS 09 303 Data Structures - Module 3

204

REPRESENTATION OF GRAPH
1) Sequential Representation

Adjacency matrix representation Incident matrix representation


2) Linked list representation

9/29/2012

CS 09 303 Data Structures - Module 3

205

ADJACENCY MATRIX REPRESENTATION


Order of Adjacency matrix = Number of vertices * number of vertices. For a directed and undirected graph, adjacency matrix conditions are: Aij = 1 { if there is an edge from Vi to Vj } = 0 { if there is no edge from Vi to Vj } For a weighted graph adjacency matrix conditions are: Aij = Wij { if there is an edge from Vi to Vj, where Wij is the weight. } = -1 { if there is no edge from Vi to Vj }
9/29/2012 CS 09 303 Data Structures - Module 3 206

9/29/2012

CS 09 303 Data Structures - Module 3

207

9/29/2012

CS 09 303 Data Structures - Module 3

208

INCIDENT MATRIX REPRESENTATION


Order of Incident matrix = Number of vertices * number of edges For an undirected graph, incident matrix conditions are: Aij = 1 { if an edge ej is incident on vertex Vi} = 0 {otherwise } For a directed graph, incident matrix conditions are: Aij = 1 {if an edge ej is going outward from vertex Vi} = -1{if an edge ej is incident on vertex Vi} = 0 {otherwise}
9/29/2012 CS 09 303 Data Structures - Module 3 209

LINKED LIST REPRESENTATION


For a directed and undirected graph, store all the vertices of graph in a list and each adjacent vertex as linked list node. For a weighted graph, linked list node will contain an extra field - weight.
9/29/2012 CS 09 303 Data Structures - Module 3 210

9/29/2012

CS 09 303 Data Structures - Module 3

211

Example
Suppose the adjacency matrix for a graph is:
1 1 1 0 0 2 1 0 1 1 3 1 0 0 1 4 1 0 1 0 1 2 3 4

The corresponding adjacency list representation is: 1 -> 1 -> 2 -> 3 -> 4 2 -> 1 3 -> 2 -> 4 4 -> 2 -> 3
9/29/2012 CS 09 303 Data Structures - Module 3 212

Operations on Graphs
FIRST (v) : returns the index for the first vertex adjacent to v. NEXT(v,i ) : returns the index after index i for the vertices adjacent to v. VERTEX ( v, i ) : returns the vertex with index i among the vertices adjacent to v.

9/29/2012

CS 09 303 Data Structures - Module 3

213

(1) FIRST (V) : Var A : array [1..n,1..n] of boolean Function FIRST (v: integer ):integer; Var i : integer ; Begin for i:= 1 to n do if A [v, i] then return (i); return (0); { if we reach here v has no adjacent vertex} End; {FIRST }
9/29/2012 CS 09 303 Data Structures - Module 3 214

(2) NEXT(v,i ) : Function NEXT (v: integer , i : integer ):integer; Var j : integer ; Begin for j:= i+1 to n do if A [v, j] then return (j); return (0); End; {NEXT } (3) VERTEX ( v, i ) : returns the vertex with index i

among the vertices adjacent to v.


9/29/2012 CS 09 303 Data Structures - Module 3 215

(4)TRAVERSAL OF ADJACENT VERTICES OF V i :=FIRST(v); while i <> NULL do begin w : = VERTEX (v,i); { some action on w } i:=NEXT(v,i); end; { while }

9/29/2012

CS 09 303 Data Structures - Module 3

216

(5)CREATING A GRAPH Steps (1) Input total number of vertices in a graph, say n. (2) Allocate memory dynamically for the vertices to store in list array. (3) Input the first vertex and vertices through which it has edge by linking node from list array through nodes. (4) Repeat the process incrementing the list array to add other vertices and edges. (5) Exit
9/29/2012 CS 09 303 Data Structures - Module 3 217

(6)SEARCHING & DELETING FROM A GRAPH Steps (1) Input an edge to be searched. (2) Search for an initial vertex of edge in list arrays by incrementing the array index. (3) Once it is found, search through linked list for the terminal vertex of the edge. (4) If found, display The edge is present in the graph. (5) Then delete the node where the terminal vertex is found and rearrange the linked list. (6) Exit.
9/29/2012 CS 09 303 Data Structures - Module 3 218

(7)TRAVERSING A GRAPH Breadth First Search (BFS ) (1) Input the vertices of the graph and its edges G=(V,E ) (2) Input the source vertex and mark it as visited. (3) Add the source vertex to queue . (4) Repeat step 5 and 6 until the queue is empty. ( i.e, front >rear ) (5) Pop the front element of queue and display it. (6) Add the vertices, which is neighbor to just popped element, if it is not in the queue . (7) Exit.
9/29/2012 CS 09 303 Data Structures - Module 3 219

Breadth First Search (BFS ) ALGORITHM Procedure bfs (V) {bfs visits all vertices adjacent to V using BFS} Var Q : QUEUE of vertex ; x,y : vertex; begin mark[v] := visited; ENQUEUE(V,Q);

9/29/2012

CS 09 303 Data Structures - Module 3

220


9/29/2012

while not EMPTY (Q) do begin x:= FRONT (Q); DEQUEUE(Q); for each vertex y adjacent to x do if mark[y] = unvisited then begin mark[y] := visited; ENQUEUE(y,Q); end {if} end;{while } End; {bfs}
CS 09 303 Data Structures - Module 3 221

Example

9/29/2012

CS 09 303 Data Structures - Module 3

222

Depth First Search (DFS ) ALGORITHM STEPS (1) Input the vertices and edges of graph G=(V,E). (2) Input the source vertex and assign it to the variable S. (3) Push the source vertex to the stack. (4) Repeat steps 5 and 6 until the stack is empty. (5) Pop the top element of stack & display it. (6) Push the vertices adjacent to just popped element if it is not in the stack & is displayed (i.e. not visited). (7) exit

9/29/2012

CS 09 303 Data Structures - Module 3

223

Depth First Search (DFS ) ALGORITHM ALGORITHM Procedure DFS(V: Vertex ); Var W : vertex; begin mark(V) := visited; for each vertex W on L(V) do if mark[w]=unvisited then DFS(W); end; {DFS} L(V) is the adjacency list.
CS 09 303 Data Structures - Module 3 224

(1) (2) (3) (4)

9/29/2012

Tree Searches
A B C

A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions, depending on the problem)

9/29/2012

CS 09 303 Data Structures - Module 3

225

Depth First Search


A B C

A depth-first search (DFS) explores a path all the way to a leaf before backtracking and exploring another path. For example, after searching A, then B, then D, the search backtracks and tries another path from B. Node are explored in the order ABDEHLMNIOPCFG JKQ N will be found before J
226

9/29/2012

CS 09 303 Data Structures - Module 3

Breadth First Search


A

A breadth-first search (BFS) explores nodes nearest the root before exploring nodes further away. For example, after searching A, then B, then C, the search proceeds with D, E, F, G. Node are explored in the order ABCDEFGHIJKLMN OPQ J will be found before N

9/29/2012

CS 09 303 Data Structures - Module 3

227

How to do DFS?
Put the root node on a stack; while (stack is not empty) do begin remove a node from the stack; if (node is a goal node) return success; put all children of node onto the stack; end return failure; At each step, the stack contains a path of nodes from the root. The stack must be large enough to hold the longest possible path, that is, the maximum depth of search.
9/29/2012 CS 09 303 Data Structures - Module 3 228

How to do BFS?
Put the root node on a queue; while (queue is not empty) do begin remove a node from the queue; if (node is a goal node) return success; put all children of node onto the queue; end return failure; Just before starting to explore level n, the queue holds all the nodes at level n-1. In a typical tree, the number of nodes at each level increases exponentially with the depth.
9/29/2012 CS 09 303 Data Structures - Module 3 229

Greedy Algorithms

9/29/2012

CS 09 303 Data Structures - Module 3

230

Minimum Spanning Tree/Minimum Cost Spanning Tree (MST)


Spanning Tree for a graph G = (V,E) is a subgraph G 1= (V 1 ,E1 ) of G that contains all the vertices of G.

The vertex set V1 is same as that of graph G. The edge set E1 is a subset of G. There is no cycle. A graph can have many different spanning trees.

9/29/2012

CS 09 303 Data Structures - Module 3

231

A weighted tree is one in which each edge is assigned a weight. Weight or cost of a spanning tree is the sum of weights of its edges. A Minimum Spanning Tree or Minimum-Weight Spanning Tree is a spanning tree with weight less than or equal to the weight of every other spanning tree.

9/29/2012

CS 09 303 Data Structures - Module 3

232

MST Algorithms
Kruskals Algorithm Finds an MST for a connected weighted undirected graph. If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component). Prims Algorithm Finds an MST for a connected weighted undirected graph. Prim's algorithm requires the graph to be connected.

9/29/2012

CS 09 303 Data Structures - Module 3

233

KRUSKALs ALGORITHM
Kruskals algorithm works by growing the minimum spanning tree one edge at a time, adding the lowest cost edge that does not create a cycle.

It starts with each vertex as a separate tree and merges these trees together by repeatedly adding the lowest cost edge that merges two distinct subtrees (i.e. does not create a cycle).

9/29/2012

CS 09 303 Data Structures - Module 3

234

KRUSKALs ALGORITHM
- It builds the MST in forest (A forest is a disjoint union of trees.). Initially, each vertex is in its own tree in forest. Then, the algorithm considers each edge in order by increasing weight. If an edge (u, v) connects two different trees, then (u, v) is added to the set of edges of the MST, and the two trees connected by the edge (u, v) are merged into a single tree. On the other hand, if the edge (u, v) connects two vertices in the same tree, then edge (u, v) is discarded.
9/29/2012 CS 09 303 Data Structures - Module 3 235

Kruskal(G) - Informal Algorithm Sort the edges in order of increasing weight count = 0 while (count < n-1) do get next edge (u,v) if (component (u) <> component(v)) add edge to T component(u) = component(v) end end end
9/29/2012 CS 09 303 Data Structures - Module 3 236

KRUSKALs ALGORITHM:OPERATIONS REQUIRED

DELETEMIN : deletes edge of minimum cost from a PRIORITY QUEUE . MERGE (A,B,C ): merge components A and B in C and call the result A or B arbitrarily. FIND (v, C) : returns the name of the component of C of which vertex v is a member. This operation will be used to determine whether the two vertices of an edge are in the same or in different components. INITIAL (A, v, C ) : makes A the name of the component in C containing only one vertex, namely v.
CS 09 303 Data Structures - Module 3 237

9/29/2012

Procedure Kruskal (V : Set of vertex; E : Set of edge ) :Var T : Set of edge ) ;

Var
ncomp : integer ; { current number of components } edges : PRIORITYQUEUE ;{ the set of edges } components :MFSET ; {the set V grouped into a MERGE- FIND set of components} u,v : vertex; e : edge ; nextcomp : integer ; {name for new component } ucomp, vcomp :integer ;{component names }

9/29/2012

CS 09 303 Data Structures - Module 3

238

Begin MAKENULL(T); MAKENULL(edges); nextcomp := 0; ncomp := number of members of V; for v in V do begin { initialize a component to contain one vertex of V } nextcomp:= nextcomp+1; INITIAL(nextcomp, v, components ); end; {for} for e in E do {initialize priority queue of edges } INSERT(e, edges);
9/29/2012 CS 09 303 Data Structures - Module 3 239

while ncomp > 1 do begin e:= DELETEMIN(edges); let e = (u,v); ucomp := FIND(u, components); vcomp:= FIND(v, components); if ucomp <> vcomp then begin {e connects two different components} MERGE (ucomp , vcomp, components ); ncomp := ncomp -1; INSERT (e ,T); end ;{if ] end; {while } End; {kuruskal }
9/29/2012 CS 09 303 Data Structures - Module 3 240

9/29/2012

CS 09 303 Data Structures - Module 3

241

9/29/2012

CS 09 303 Data Structures - Module 3

242

9/29/2012

CS 09 303 Data Structures - Module 3

243

9/29/2012

CS 09 303 Data Structures - Module 3

244

9/29/2012

CS 09 303 Data Structures - Module 3

245

PRIMs ALGORITHM for MST


Prim's algorithm starts with an arbitrary vertex v and ``grows'' a tree from it, repeatedly finding the lowest-cost edge that will link some new vertex into this tree.

9/29/2012

CS 09 303 Data Structures - Module 3

246

Algorithm (Informal)
Start with a tree which contains only one node. Identify a node (outside the tree) which is closest to the tree and add the minimum weight edge from that node to some node in the tree and incorporate the additional node as a part of the tree. If there are less than n 1 edges in the tree, go to 2.

9/29/2012

CS 09 303 Data Structures - Module 3

247

PRIMS ALGORITHM
Procedure Prim (G: graph; var T: set of edges); {Prim constructs a minimum-cost spanning tree T for G}. Var U: set of vertices; u, v : vertex; Begin T:= ; U:= {1};

9/29/2012

CS 09 303 Data Structures - Module 3

248

while U<>V do begin let (u,v) be a lowest cost edge such that u is in U and v is in V-U;

end End; {Prim}

9/29/2012

CS 09 303 Data Structures - Module 3

249

9/29/2012

CS 09 303 Data Structures - Module 3

250

9/29/2012

CS 09 303 Data Structures - Module 3

251

9/29/2012

CS 09 303 Data Structures - Module 3

252

PRIMs ALGORITHM FOR MST


Procedure Prim (C :array [1..n ,1..n] of real ); {Prim prints the edges of MST for a graph with vertices {1,2,3.n} and cost matrix C on edges } Var LOWCOST :array[1..n] of real; CLOSEST :array[1..n] of integer; i, j, k, min :integer; {i and j are indices .During a scan of the LOWCOST array, k is the index of the CLOSEST vetex found so far and min := LOWCOST[k] }
9/29/2012 CS 09 303 Data Structures - Module 3 253

begin for i:= 2 to n do begin {initialize with only vertex 1 in the set U } LOWCOST[i] :=C [1,i] ; CLOSEST[i]:=1; end;{for }

9/29/2012

CS 09 303 Data Structures - Module 3

254

For i:= 2 to n do begin {find the CLOSEST vertex k outside of U to some vertex in U} Min:= LOWCOST[2]; k:= 2; For j:=3 to n do If LOWCOST[i] < min then begin Min := LOWCOST[j]; k := j; end; Writeln(k, CLOSEST[k]); {print edge } LOWCOST[k] := infinity; {k is added to U }
9/29/2012 CS 09 303 Data Structures - Module 3 255

For j:=2 to n do { adjust costs to U } If (C[k,j] < LOWCOST[j] ) and (LOWCOST[j] < infinity ) then begin LOWCOST[j] := C[k,j]; CLOSEST[j]:= k; End; {if } End {for } End ;{Prim} Where CLOSEST[i] Gives vertex in U that is currently closest to vertex i in V-U. LOWCOST[i] Cost of the edge (i,CLOSEST[i])

9/29/2012

CS 09 303 Data Structures - Module 3

256

9/29/2012

CS 09 303 Data Structures - Module 3

257

Start with the tree < { v1 }, { } >. Vertex v4 is closest to tree . . .

9/29/2012

CS 09 303 Data Structures - Module 3

258

v3 is closest to tree . . .

9/29/2012

CS 09 303 Data Structures - Module 3

259

v2 and v5 are closest to tree, pick v5, say . .

9/29/2012

CS 09 303 Data Structures - Module 3

260

v6 is closest to tree . . .

9/29/2012

CS 09 303 Data Structures - Module 3

261

v2 is closest (and only remaining) vertex . . .

9/29/2012

CS 09 303 Data Structures - Module 3

262

9/29/2012

CS 09 303 Data Structures - Module 3

263

This figure shows there may be more than one minimum spanning tree in a graph. In the figure, the two trees below the graph are two possibilities of minimum spanning tree of the given graph.
9/29/2012 CS 09 303 Data Structures - Module 3 264

SHORTEST PATH Algorithms for Directed Graphs


Single Source Shortest Paths Algorithm to determine the cost of the shortest path from the source to every other vertex in V.
Dijkstras Algorithm

All-Pairs Shortest Paths Algorithm to find for each ordered pair of vertices (v,w), the shortest path from v to w.
Floyds Algorithm

9/29/2012

CS 09 303 Data Structures - Module 3

265

Dijkstras Algorithm
The algorithm works by maintaining a set S of vertices whose shortest distance from the source is already known. Initially, S contains only the source vertex. At each step, we add to S a remaining vertex v whose distance from the source is as short as possible. Assuming that all arcs have nonnegative costs, we can always find a shortest path from the source to v that passes only through vertices in S.
9/29/2012 CS 09 303 Data Structures - Module 3 266

At each step of the algorithm, we use an array D to record the length of the shortest path to each vertex. Once S includes all vertices, all paths are shortest paths, so D will hold the shortest distance from the source to each vertex.

9/29/2012

CS 09 303 Data Structures - Module 3

267

Procedure Dijikstra {Dijikstra computes the cost of shortest paths from vertex 1 to every vertex of a directed graph} Begin S := {1}; For i :=2 to n do D[i] :=C[1,i]; {Initialize D } For i:=1 to n-1 do begin Choose a vertex w in V-S such that D[w] is minimum ; add w to S; For each vertex v in V-S do D[v]:= min( D[v], D[w] + C[w,v]); end;{for} End;{Dijikstra}
9/29/2012 CS 09 303 Data Structures - Module 3 268

9/29/2012

CS 09 303 Data Structures - Module 3

269

9/29/2012

CS 09 303 Data Structures - Module 3

270

FLOYDS ALGORITHM
Procedure Floyd(Var A :array[1..n ,1..n] of real; C :array[1..n,1..n] of real ); {Floyd computes shortest path matrix A given arc cost matrix C} Var i, j, k : integer; Begin For i := 1 to n do For j := 1 to n do A[i, j] := C[i, j]; For i:=1 to n do A[i, i] := 0;

9/29/2012

CS 09 303 Data Structures - Module 3

271

For k := 1 to n do For i:=1 to n do For j:=1 to n do If A[i, k] + A[k, j] < A[i,j] then A[i,j] := A[i, k] + A[k, j]; End; {Floyd }

9/29/2012

CS 09 303 Data Structures - Module 3

272

9/29/2012

CS 09 303 Data Structures - Module 3

273

9/29/2012

CS 09 303 Data Structures - Module 3

274

Thank You

9/29/2012

CS 09 303 Data Structures - Module 3

275

Potrebbero piacerti anche