Introduction to Data Structures and Algorithms



Similar documents
\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Introduction to Data Structures and Algorithms

Data Structure [Question Bank]

Algorithms Chapter 12 Binary Search Trees

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Sequential Data Structures

Algorithms and Data Structures

DATA STRUCTURES USING C

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Data Structures and Algorithms Stacks and Queues

Analysis of a Search Algorithm

Common Data Structures

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

PES Institute of Technology-BSC QUESTION BANK

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Binary Search Trees CMPSC 122

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

Linked Lists Linked Lists, Queues, and Stacks

Data Structures. Level 6 C Module Descriptor

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Converting a Number from Decimal to Binary

Chapter 3: Restricted Structures Page 1

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Data Structures Fibonacci Heaps, Amortized Analysis

Big O and Limits Abstract Data Types Data Structure Grand Tour.

10CS35: Data Structures Using C

Cpt S 223. School of EECS, WSU

Sample Questions Csci 1112 A. Bellaachia

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Analysis of Algorithms I: Binary Search Trees

System Software Prof. Dr. H. Mössenböck

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Universidad Carlos III de Madrid

Data Structures UNIT III. Model Question Answer

Binary Heap Algorithms

St S a t ck a ck nd Qu Q eue 1

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Data Structures. Chapter 8

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Symbol Tables. Introduction

Module 2 Stacks and Queues: Abstract Data Types

Data Structures and Data Manipulation

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Lecture Notes on Binary Search Trees

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Introduction to Data Structures

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Algorithms and Data Structures

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Data Structures and Algorithms

Binary Search Tree Intro to Algorithms Recitation 03 February 9, 2011

6 March Array Implementation of Binary Trees

Exam study sheet for CS2711. List of topics

Lecture 2: Data Structures Steven Skiena. skiena

Analysis of Algorithms, I

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Rotation Operation for Binary Search Trees Idea:

Algorithms and Data S tructures Structures Stack, Queues, and Applications Applications Ulf Leser

Ordered Lists and Binary Trees

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Lecture Notes on Binary Search Trees

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

CSE 326: Data Structures B-Trees and B+ Trees

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

TREE BASIC TERMINOLOGIES

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

CompSci-61B, Data Structures Final Exam

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Computer Programming using C

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

STACKS,QUEUES, AND LINKED LISTS

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Java Software Structures

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

DATA STRUCTURE - QUEUE

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Transcription:

Introduction to Data Structures and Algorithms Chapter: Elementary Data Structures(1) Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German) Martensstraße 3, 91058 Erlangen

Overview on simple data structures for representing dynamic sets of data records Main operations on these data structures are Insertion and deletion of an element searching for an element finding the minimum or maximum element finding the successor or the predecessor of an element And similar operations These data structures are often implemented using dynamically allocated objects and pointers Data Structures and Algorithms (132)

Typical Examples of Elementary Data Structures Array Stack Queue Linked List Tree Data Structures and Algorithms (133)

Stack A stack implements the LIFO (last-in, first-out) policy like a stack of plates, where you can either place an extra plate at the top or remove the topmost plate For a stack, the insert operation is called Push and the delete operation is called Pop Data Structures and Algorithms (134)

Where are Stacks used? A call stack that is used for the proper execution of a computer program with subroutine or function calls Analysis of context free languages (e.g. properly nested brackets) Properly nested: (()(()())), Wrongly nested: (()((()) Reversed Polish notation of terms Compute 2 + 3*5 2 Push 3 Push 5 * + Data Structures and Algorithms (135)

Properties of a Stack Stacks can be defined by axioms based on the stack operations, i.e. a certain data structure is a stack if the respective axioms hold For illustration some examples for such axioms - the typical axioms are (where S is a Stack which can hold elements x of some set X) If not full(s): Pop(S) o (Push(S,x)) = x for all x X If not empty(s): Push(S, Pop(S)) = S Data Structures and Algorithms (136)

Typical Implementation of a Stack A typical implementation of a stack of size n is based on an array S[1 n] so it can hold at most n elements top(s) is the index of the most recently inserted element Push Pop The stack consists of elements S[1 top(s)], where S[1] is the element at the bottom of the stack, and S[top(S)] is the element at the top. The unused elements S[top(S)+1 n] are not in the stack Top S 4 3 2 1 Data Structures and Algorithms (137)

Stack If top(s) = 0 the stack is empty no element can be popped If top(s) = n the stack is full no further element can be pushed Data Structures and Algorithms (138)

Example (Stack Manipulation) S Start with stack given, denote changes of stack state 7 6 Push(S, 17) Pop(S), Pop(S), Pop(S), Push(S, 5) 5 Pop(S), Pop(S) Pop(S) 4 3 3 top(s) 2 23 1 3 Data Structures and Algorithms (139)

pop (S) 17 pop (S) 3 push(s,17) pop (S) 23 pop (S) 5 push(s,5) pop (S) 3 S: S: S: S: pop (S) 7 6 7 6 7 6 7 6 Error: underflow Top[S]=3 5 4 Top=4 17 5 4 3 23 3 3 2 1 3 23 3 3 2 1 Top=2 5 3 5 4 3 2 1 5 4 3 2 1 Top=0 Data Structures and Algorithms (140)

Pseudo Code for Stack Operations Number of elements NumElements (S) return top[s] Data Structures and Algorithms (141)

Pseudo Code for Stack Operations Test for emptiness Stack_Empty(S) if top[s]=0 then return true else return false Test for stack full Stack_Full (S) if top[s]=n then return true else return false Data Structures and Algorithms (142)

Pseudo Code for Stack Operations Pushing and Popping Push(S,x) if Stack_Full(S) then error "overflow" else top[s] := top[s]+1 S[top[S]] := x Pop(S) if Stack_Empty(S) then error "underflow" else top[s] := top[s]-1 return S[top[S]+1] This pseudo code contains error handling functionality Data Structures and Algorithms (143)

Pseudo Code for Stack Operations (Asymptotic) Runtime NumElements: number of operations independent of size n of stack constant O(1) Stack_Empty and Stack_Full: number of operations independent of size n of stack constant O(1) Push and Pop: number of operations independent of size n of stack constant O(1) Data Structures and Algorithms (144)

Queue A queue implements the FIFO (first-in, first-out) policy Like a line of people at the post office or in a shop enqueue dequeue For a queue, tail head the insert operation is called Enqueue (=> place at the tail of the queue) and the delete operation is called Dequeue (=> take from the head of the queue) Data Structures and Algorithms (145)

Where are Queues used? In multi-tasking systems (communication, synchronization) In communication systems (store-and-forward networks) In servicing systems (queue in front of the servicing unit) Queuing networks (performance evaluation of computer and communication networks) Data Structures and Algorithms (146)

Typical Implementation of a Queue A typical implementation of a queue consisting of at most n-1 elements is based on an array Q[1 n] Its attribute head(q) points to the head of the queue. Its attribute tail(q) points to the position where a new element will be inserted into the queue (i.e. one position behind the last element of the queue). The elements in the queue are in positions head(q), head(q)+1,, tail(q)-1, where we wrap around the array boundary in the sense that Q[1] immediately follows Q[n] Data Structures and Algorithms (147)

Example (1) Q 1 2 3 4 5 6 7 8 9 10 1. 2. 3. (n = length (Q) = 10) Insert a new element (4.) head(q) tail(q) Q 1 2 3 4 5 6 7 8 9 10 1. 2. 3. 4. head(q) tail(q) Data Structures and Algorithms (148)

Example (2) Q 1 2 3 4 5 6 7 8 9 10 1. 2. 3. 4. Insert one more element (5.) head(q) tail(q) Q 1 2 3 4 5 6 7 8 9 10 1. 2. 3. 4. 5. tail(q) head(q) And again: Insert one more element (6.) Q 1 2 3 4 5 6 7 8 9 10 6. 1. 2. 3. 4. 5. tail(q) head(q) Data Structures and Algorithms (149)

Typical Implementation of a Queue Number of elements in queue If tail > head: NumElements(Q) = tail - head If tail < head: NumElements(Q) = tail head + n If tail = head: NumElements(Q) = 0 Initially: head[q] = tail[q] = 1 Position of elements in queue The x. element of a queue Q (1 x NumElements(Q) is mapped to array position head(q) + (x - 1) if x n head +1 (no wrap around) head(q) + (x - 1) - n if x > n head +1 (wrap around) Data Structures and Algorithms (150)

Typical Implementation of a Queue Remark: A queue implemented by a n-element array can hold at most n-1 elements otherwise we could not distinguish between an empty and a full queue A queue Q is empty: ( NumElements(Q) = 0) if head(q) = tail(q) A queue Q is full: ( NumElements(Q) = n-1) if head(q) = (tail(q) + 1) (head(q) > tail(q)) if head(q) = (tail(q) - n + 1) (head(q) < tail(q)) Data Structures and Algorithms (151)

Example (Queue Manipulation) 1 2 3 4 5 6 7 8 9 10 Q 4 12 4 head(q) tail(q) Start with queue given, denote changes of queue state Enqueue(Q, 2), Enqueue(Q, 3), Enqueue(Q, 7) Dequeue(Q) Data Structures and Algorithms (152)

Queue Operations Enqueue and Dequeue This pseudo code does not contain error handling functionality (see stack push and pop) Enqueue(Q,x) Q[tail[Q]] := x Precondition: queue not full if tail[q]=length[q] then tail[q] := 1 else tail[q] := tail[q]+1 Dequeue(Q) Precondition: queue not empty x := Q[head[Q]] if head[q]=length[q] then head[q] := 1 else head[q] := head[q]+1 return x Data Structures and Algorithms (153)

Pseudo Code for Queue Operations (Asymptotic) Runtime Enqueue and Dequeue: number of operations independent of size n of queue constant O(1) Data Structures and Algorithms (154)

Introduction to Data Structures and Algorithms Chapter: Elementary Data Structures(2) Lehrstuhl Informatik 7 (Prof. Dr.-Ing. Reinhard German) Martensstraße 3, 91058 Erlangen

Typical Examples of Elementary Data Structures Array Stack Queue Linked List Tree Data Structures and Algorithms (156)

Linked List In a linked list, the elements are arranged in a linear order, i.e. each element (except the first one) has a predecessor and each element (except the last one) has a successor. Unlike an array, elements are not addressed by an index, but by a pointer (a reference). There are singly linked lists and doubly linked lists. A list may be sorted or unsorted. A list may be circular (i.e. a ring of elements). Here we consider mainly unsorted, doubly linked lists Data Structures and Algorithms (157)

Linked List Each element x of a (doubly) linked list has three fields A pointer prev to the previous element A pointer next to the next element A field that contains a key (value of a certain type) Possibly a field that contains satellite data (ignored in the following) One element x : (consist of 3 fields) prev key next Pointer fields that contain no pointer pointing to another element contain the special pointer NIL ( ) The pointer head[l] points to the first element of the linked list If head[l] = NIL the list L is an empty list Data Structures and Algorithms (158)

Linked List In a linked list, the insert operation is called List_Insert, and the delete operation is called List_Delete. In a linked list we may search for an element with a certain key k by calling List_Search. Linked List Example: dynamic set {11, 2,7, 13} prev key next head[l] 11 7 13 2 Notice: prev[head] = NIL and next[tail] = NIL Data Structures and Algorithms (159)

Some Examples for the Use of Linked Lists Lists of passengers of a plane or a hotel Card games (sorting cards corresponding to a certain order, inserting new cards into or removing cards out of the sequence) To-do lists (containing entries for actions to be done) Hash Lists ( Hashing, dealt later in this lecture) Data Structures and Algorithms (160)

Searching a Linked List The procedure List_search (L, k) finds the first element with key k in list L and returns a pointer to that element. If no element with key k is found, the special pointer NIL is returned. List_Search(L,k) x := head[l] while x!=nil and key[x]!=k do x := next[x] return x It takes at most Θ(n) time to search a list of n objects (linear search) Data Structures and Algorithms (161)

Inserting into a Linked List The procedure List_insert(L,x) inserts a new element x as the new head of list L List_Insert(L,x) next[x] := head[l] if head[l]!=nil then prev[head[l]] := x head[l] := x prev[x] := NIL x key(x) head[l] The runtime for List_Insert on a list of length n is constant (O(1)) Data Structures and Algorithms (162)

Deleting from a Linked List The procedure List_Delete (L, x) removes an element x from the linked list L, where the element is given by a pointer to x. If you want to delete an element given by its key k, you have to compute a pointer to this element (e.g. by using List_search(L, k)) List_Delete(L,x) if prev[x]!=nil x not the first element then next[prev[x]] := next[x] else head[l] := next[x] if next[x]!=nil x not the last element then prev[next[x]] := prev[x] Data Structures and Algorithms (163)

Deleting from a Linked List a) a) b) head[l] List_Delete(L,x) if prev[x]!=nil then next[prev[x]] := next[x] else head[l] := next[x] if next[x]!=nil then prev[next[x]] := prev[x] x b) head[l] x Data Structures and Algorithms (164)

Deleting from a Linked List The runtime for List_Delete on a list of length n is constant (O(1)) If you want to delete an element with a certain key, you must first find that element by executing List_Search, which takes Θ(n) time in the worst case Data Structures and Algorithms (165)

Inserting and deleting : head[l] 7 13 2 11 List_insert (L,x) with key[x] = 25 head[l] 25 7 13 2 11 List_Delete (L,x) where x points to element with key[x] = 2 head[l] 25 7 13 11 Data Structures and Algorithms (166)

Tree Any data structure consisting of elements of the same type can be represented with the help of pointers (in a similar way as we implemented lists). Very important examples of such data structures are trees. Trees are graphs that contain no cycle: every non-trivial path through a tree starting at a node and ending in the same node, does traverse at least one edge at least twice. There exist many kinds of trees. Examples are: Binary trees Trees with unbounded branching Binary search trees Red-black trees Data Structures and Algorithms (167)

Some Examples for the Use of Trees Systematically exploring various ways of proceeding (e.g. in chess or planning games) Morse trees (coding trees) Heaps ( heap sort) Search trees Data Structures and Algorithms (168)

Tree A binary tree consists of nodes with the following fields A key field Possibly some satellite data (ignored in the following) Three pointers p, left and right pointing to the parent node, left child node and right child node Be x an element (or node) of a tree If p[x] = NIL x represents the root node If both left[x] = NIL and right[x] = NIL x represents a leaf node For each tree T there is a pointer root[t] that points to the root of T If root[t] = NIL, the tree T is empty Data Structures and Algorithms (169)

Binary Tree (Example) p key (+ s.d.) left right Data Structures and Algorithms (170)

P-nary Trees The above scheme can be extended to any class of trees where the number of children is bounded by some constant k : child1,, child k a bit of memory space may be wasted for pointers which are not actually used k Trees with unbounded branching A tree with unbounded branching (if no upper bound on the number of a node's children is known a priori) can be implemented by the following scheme: Each node has a key field (and possibly some satellite data), and three pointers p, left_child and right_sibling In a leaf node, left_child=nil If a node is the rightmost child of its parent, then right_sibling=nil Data Structures and Algorithms (171)

Unbounded Tree (Example) p key (+ s.d.) l_child r_sibl Data Structures and Algorithms (172)

Unbounded Tree (Example) leaves Data Structures and Algorithms (173)