Algorithms and Data Structures



Similar documents
DATA STRUCTURES USING C

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

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

Data Structure [Question Bank]

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

Module 2 Stacks and Queues: Abstract Data Types

Data Structures UNIT III. Model Question Answer

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Full and Complete Binary Trees

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Data Structures and Algorithms V Otávio Braga

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

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

TREE BASIC TERMINOLOGIES

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 Fibonacci Heaps, Amortized Analysis

Data Structures. Level 6 C Module Descriptor

Symbol Tables. Introduction

10CS35: Data Structures Using C

Common Data Structures

PES Institute of Technology-BSC QUESTION BANK

Chapter 3: Restricted Structures Page 1

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

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.

Ordered Lists and Binary Trees

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

Binary Trees and Huffman Encoding Binary Search Trees

Introduction to Data Structures and Algorithms

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

Binary Heap Algorithms

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

Binary Search Trees (BST)

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

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Data Structure with C

2) Write in detail the issues in the design of code generator.

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

Fast nondeterministic recognition of context-free languages using two queues

Converting a Number from Decimal to Binary

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

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:

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

Introduction to Data Structures

AUTOMATED TEST GENERATION FOR SOFTWARE COMPONENTS

Section IV.1: Recursive Algorithms and Recursion Trees

Data Structures and Algorithms

Binary Search Trees CMPSC 122

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

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

7.1 Our Current Model

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

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

Algorithms Chapter 12 Binary Search Trees

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Introduction to Stacks

Analysis of Algorithms I: Binary Search Trees

Binary Search Trees. Ric Glassey

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

Analysis of a Search Algorithm

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

Any two nodes which are connected by an edge in a graph are called adjacent node.

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

How To Create A Tree From A Tree In Runtime (For A Tree)

AP Computer Science AB Syllabus 1

Data Structures. Chapter 8

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

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

DATABASE DESIGN - 1DL400

Structural Design Patterns Used in Data Structures Implementation

GRAPH THEORY LECTURE 4: TREES

Data Structures and Data Manipulation

6 March Array Implementation of Binary Trees

Chapter 7: Termination Detection

Binary Heaps. CSE 373 Data Structures

Sample Questions Csci 1112 A. Bellaachia

From Last Time: Remove (Delete) Operation

Big Data and Scripting. Part 4: Memory Hierarchies

Data Structures and Algorithms Stacks and Queues

Data Structures and Algorithms Written Examination

Stacks. Data Structures and Data Types. Collections

Optimal Binary Search Trees Meet Object Oriented Programming

PROBLEMS (Cap. 4 - Istruzioni macchina)

Rotation Operation for Binary Search Trees Idea:

Why Use Binary Trees?

STACKS,QUEUES, AND LINKED LISTS

Analysis of Algorithms I: Optimal Binary Search Trees

Chapter 8: Binary Trees

COMPUTER SCIENCE. Paper 1 (THEORY)

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Heaps & Priority Queues in the C++ STL 2-3 Trees

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Intermediate Code. Intermediate Code Generation

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

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

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

Honors Class (Foundations of) Informatics. Tom Verhoeff. Department of Mathematics & Computer Science Software Engineering & Technology

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

Sequential Data Structures

Binary Coded Web Access Pattern Tree in Education Domain

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

Transcription:

Algorithms and Data Structures Part 2: Data Structures PD Dr. rer. nat. habil. Ralf-Peter Mundani Computation in Engineering (CiE) Summer Term 2016

Overview general linked lists stacks queues trees 2 2

General fundamentals data structure way of organising data for processing by some algorithm or more general by some computer program often the choice of proper data structures is the only major decision for implementations for the same information some data structures require more or less space for storing the information for the same operations (on the data) some data structures lead to more or less efficient algorithms hence, the choice of algorithms and data structures is closely intertwined design of algorithms also means design of sufficient data structures 2 3

Overview general linked lists stacks queues trees 2 4

Linked Lists fundamentals elementary data type, but not (yet) defined as primitive in all languages advantages compared to arrays easy growth and shrinkage in size over lifetime, hence, maximum size must not be known in advance high flexibility w.r.t. rearrangement of data items (nevertheless, this comes up with higher expenses for the access of arbitrary items) general description set of sequentially organised items, so called nodes explicit organisation of nodes via link to next node N 1 N 2 N 3 N M 1 N M linked list with nodes N i, 1 i M 2 5

Linked Lists fundamentals (cont d) each node contains one container for storing data item value (integer, float, boolean, ) one container for storing link to next node value i 1 value i value i 1 problem: what to specify as next node in case of last node hence, dummy node NIL to indicate end of list for reasons of convenience also dummy node HEAD at the other end HEAD N 1 N 2 N M 1 N M NIL linked list with nodes N i, 1 i M 2 6

Linked Lists fundamentals (cont d) assume, there s a primitive list with two containers value and link which can be accessed via list value and list link furthermore, a dummy node NIL should also be provided hence, the following program creates an empty list list L L link NIL which only consists of the two dummy nodes HEAD and NIL HEAD NIL for adding nodes to list L some further understanding is necessary 2 7

Linked Lists fundamentals (cont d) linked lists are dynamic structures easy rearrangement of nodes possible (just changing links instead of moving items in case of arrays) example: last node should be moved to the front (3 links to be changed) HEAD 1 2 3 4 5 NIL HEAD 1 2 3 4 5 NIL HEAD 5 1 2 3 4 NIL 2 8

Linked Lists list operations typical operations on linked lists are insertion of new nodes (which makes it grow by one in length each) deletion of old nodes (which makes it shrink by one in length each) for inserting a new node only two links have to be changed 4 HEAD 1 2 3 5 6 NIL 4 HEAD 1 2 3 5 6 NIL 2 9

Linked Lists list operations (cont d) the following procedure appends a new node at the end of list L by recursively processing all nodes starting from HEAD procedure APPEND (L, new_node) if L link NIL then new_node link L link L link new_node else APPEND (L link, new_node) fi end changing the condition allows to insert a new node anywhere in the list 2 10

Linked Lists list operations (cont d) the following procedure inserts a new node before the one with value val procedure INSERT (L, new_node, val) case L link NIL: exit L link value val: new_node link L link L link new_node else INSERT (L link, new_node, val) esac end the first test for NIL is necessary in case there is no node with value val and the recursion reaches the end of list L 2 11

Linked Lists list operations (cont d) when deleting node N i from a list only one link has to be moved (i. e. linking from node N i 1 to node N i 1 ) this does not delete node N i itself (which still points to N i 1 ), but it is no longer accessible by following the links from HEAD HEAD 1 2 3 4 5 NIL HEAD 1 2 3 4 5 NIL 2 12

Linked Lists list operations (cont d) the following procedure deletes a node (with value val) from lists L procedure DELETE (L, next, val) case next NIL: exit next value val: L link next link else DELETE (L link, next link, val) esac end the temporary node next is necessary to link from N i 1 to N i 1 (again, the dummy element HEAD makes everything more convenient) for deleting some node from list L the procedure is called with parameters DELETE (L, L link, val) 2 13

Linked Lists doubly linked lists as shown, linked lists are very efficient for certain operations, but there is no direct access to single nodes hence, finding the kth item has a complexity of (N) as all nodes have to be processed successively until the kth item is reached (compared to (1) for arrays) furthermore, there is no possibility of going backwards in case a node that has already been processed is needed (such as N i 1 for deleting N i ) idea: maintain two links for each node, one to the item before and one to the item after so called doubly linked lists HEAD NIL N 1 N 2 N M 1 N M NIL doubly linked list with nodes N i, 1 i M TAIL 2 14

Overview general linked lists stacks queues trees 2 15

Stacks fundamentals for many applications rather hard restrictions concerning the accessibility of data structures can be made in order to hide (often complicated and, thus, error prone) implementation details such as links or indices allow more flexible implementations with fewer operations famous representatives of such restricted-access data structures are so called stacks or pushdown stacks, i.e. one-dimensional structures with LIFO (last in, first out) ordering that support two types of basic operations push (val): inserting one element with value val at the beginning of the stack pop: removing one element from the beginning of the stack and returning its value 2 16

Stacks fundamentals (cont d) stacks are, for instance, frequently used for evaluating arithmetic expressions (within pocket calculators, e.g.) arithmetic expressions can be written in different notations prefix: operands appear after the operator infix: the customary way of writing arithmetic expressions (but parentheses may be required) postfix or reverse Polish: operands appear before the operator therefore, stacks require arithmetic expressions to appear in postfix notation so the operands can be on the stack when the operator is encountered (as expression are processed from left to right) and, thus, intermediate results can again be pushed to the stack 2 17

Stacks implementation of stacks stacks to be implemented, for instance, as arrays (in case the maximum size can be predicted in advance) linked lists assume, there s a primitive stack (of fixed size N) implemented as array further assumptions and requirements are necessary it s not possible to push an element on a full stack it s not possible to pop an element from an empty stack index last indicates the last element in a stack 1 2 3 4 5 stack of size N 10 with last 4 hence, operations push and pop ready to be implemented 2 18

Stacks implementation of stacks (cont d) push: variables N and last with global visibility procedure PUSH (S, val) if last N 1 then last last 1 S(last) val fi end where S indicates a stack and val the value to be pushed on the stack 2 19

Stacks implementation of stacks (cont d) pop: variables N and last with global visibility procedure POP (S) tmp 0 if last 0 then tmp S(last) last last 1 return (tmp) else return (false) fi end where S indicates a stack 2 20

Stacks example evaluation of an arithmetic expression A B C (D E) using a stack stack S push (S, B) push (S, C) push (S, D) push (S, E) push (S, pop (S) pop (S)) push (S, pop (S) pop (S)) push (S, pop (S) pop (S)) A pop (S) E D C B operators and have to switch operands due to LIFO ordering 2 21

Overview general linked lists stacks queues trees 2 22

Queues fundamentals other representatives of restricted-access data structures are queues one-dimensional structures with FIFO (first in, first out) ordering that support two types of basic operations insert (val): inserting one element with value val at the beginning of the queue remove: removing one element from the end of the queue and returning its value in comparison to stacks, the order of processing reflects the order of arrival of elements (also referred to as first come, first serve) queues to be implemented, for instance, as arrays (in case the maximum size can be predicted in advance) linked lists 2 23

Queues implementation of queues assume, there s a primitive queue (of fixed size N) implemented as array further assumptions and requirements are necessary it s not possible to insert an element into a full queue it s not possible to remove an element from an empty queue variable used indicates the amount of already used cells two indices first and last indicate the first and the last element in a queue, taking into account the wraparound back to 0 when the end of the array is encountered 4 5 1 2 3 queue of size N 10 with used 5, first 7, and last 1 hence, operations insert and remove ready to be implemented 2 24

Queues implementation of queues (cont d) insert: variables N, used, first, and last with global visibility procedure INSERT (Q, val) if used N then last last 1 if last N then last 0 fi Q(last) val used used 1 fi end where Q indicates a queue and val the value to be inserted into the queue 2 25

Queues implementation of queues (cont d) remove: variables N, used, first, and last with global visibility procedure REMOVE (Q) tmp 0 if used 0 then tmp Q(first) used used 1 first first 1 if first N then first 0 fi return (tmp) else return (false) fi end 2 26

Overview general linked lists stacks queues trees 2 27

Trees fundamentals definition: a tree is a finite set of nodes such that 1) there is one specially designated node called root 2) remaining nodes are partitioned into N 0 disjoint sets T 1,, T N where each of these sets is a tree (so called subtree of the root) 3) there exists exactly one path between the root and each of the other nodes in the tree root links between nodes might point downwards (away from the root) or upwards (towards the root) depending upon the application 2 28

Trees fundamentals (cont d) further definitions (1) each node (except the root) has exactly one node above it which is called its parent hence, the nodes directly below a node are called its children nodes with no children are called leaves or terminal nodes hence, nodes with children are called internal or nonterminal nodes parent children internal nodes leaves 2 29

Trees fundamentals (cont d) further definitions (2) children of the same parent are said to be siblings a forest is a set of N 0 disjoint trees (removing the root of a tree leads to a forest) the level of a node is defined by initially letting the root be at level one; hence, if a node is at level K, then its children are at level K 1 the height or depth of a tree is the maximum level of any node in the tree level 1 siblings level 2 level 3 2 30

Trees representation of trees idea: using linked lists where one node in the list corresponds to one node in the tree problem: nodes can have a varying number of links difficult to write algorithms for data structures with varying sizes hence, already known list elements to be extended by one additional link as follows value siblings children 2 31

Trees representation of trees (cont d) example A B E G HEAD A C D F H I J B E G C D F H I J i. e. link pointing to NIL 2 32

Trees binary trees definition: a binary tree is a finite set of nodes which is either empty or consists of a root node and two disjoint binary trees called the left and right subtree hence, any node can have at most two children ( name binary tree ) examples of binary trees skewed binary tree binary tree full binary tree 2 33

Trees binary trees (cont d) properties a tree with N nodes has N 1 edges (i. e. links); this is true for all kind of trees and follows directly from the recursive definition the maximum number of nodes N in a full binary tree of depth K 0 is 2 K 1 the height of a full binary tree with N nodes is about ld N (log 2 N) for representing binary trees, linked lists can be used again (labelling one link as left and one link as right) value left right drawback: determination of a node s parent due to missing upward link 2 34

Trees binary trees (cont d) example HEAD A B F C G A D E B F C G D E i. e. link pointing to NIL 2 35

Trees binary trees (cont d) adding a node to a binary tree is a quite simple task 1) find a corresponding node X with at least one free link left or right 2) link from X to the new node Y, thus, X becomes parent of Y Y X X Y 2 36

Trees binary trees (cont d) deleting a node from a binary tree is a more complex task to be distinguished a) deleting a leaf node b) deleting an internal node with one child c) deleting an internal node with two children cases a) and b) are quite easy to process, but case c) requires further understanding case a) case b) case c) 2 37

Trees binary trees (cont d) case a: deleting a leaf node 1) find the corresponding parent of the leaf node to be deleted 2) set the parent s link to NIL node to be deleted 2 38

Trees binary trees (cont d) case b: deleting an internal node with one child 1) find the corresponding parent of the node to be deleted 2) set the parent s link to the child of the node to be deleted node to be deleted 2 39

Trees binary trees (cont d) case c: deleting an internal node with two children 1) find the corresponding parent of the node to be deleted 2) find the right-most child (X) from the node to be deleted its left child 3) set the parent s link to the left child of the node to be deleted 4) set X s right link to the right child of the node to be deleted node to be deleted right-most child X 2 40