Algorithms and Data Structures

Similar documents
MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

TREES. Trees - Introduction

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

CS301 - Data Structures Glossary By

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

3. Fundamental Data Structures

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Data Abstractions. National Chiao Tung University Chun-Jen Tsai 05/23/2012

12 Abstract Data Types

Data Structure. IBPS SO (IT- Officer) Exam 2017

Postfix (and prefix) notation

Stacks, Queues and Hierarchical Collections

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial


Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

Data Structure - Binary Tree 1 -

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

Recursive Data Structures and Grammars

DEEPIKA KAMBOJ UNIT 2. What is Stack?

CE 221 Data Structures and Algorithms

LECTURE 11 TREE TRAVERSALS

An Introduction to Trees

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Linear Data Structure

Summer Final Exam Review Session August 5, 2009

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

CSE 230 Intermediate Programming in C and C++

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

Fundamentals of Data Structure

Department of Computer Science and Technology

Data Structures Question Bank Multiple Choice

[ DATA STRUCTURES ] Fig. (1) : A Tree

CS8391-DATA STRUCTURES

CS 171: Introduction to Computer Science II. Binary Search Trees

V Advanced Data Structures

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

Stack Abstract Data Type

infix expressions (review)

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Algorithms and Data Structures

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

V Advanced Data Structures

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Binary Trees, Binary Search Trees

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

DATA STRUCTURE AND ALGORITHM USING PYTHON

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Chapter 8: Data Abstractions

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

CS 8391 DATA STRUCTURES

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Formal Languages and Automata Theory, SS Project (due Week 14)

Trees. (Trees) Data Structures and Programming Spring / 28

Name CPTR246 Spring '17 (100 total points) Exam 3

CS-301 Data Structure. Tariq Hanif

Binary Search Tree (2A) Young Won Lim 5/17/18

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Problem with Scanning an Infix Expression

CISC

Linked List Implementation of Queues

COMP : Trees. COMP20012 Trees 219

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

3137 Data Structures and Algorithms in C++

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Basic Data Structures (Version 7) Name:

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Operations on Heap Tree The major operations required to be performed on a heap tree are Insertion, Deletion, and Merging.

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Draw a diagram of an empty circular queue and describe it to the reader.

Data Structures and Algorithms

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Lecture Notes 16 - Trees CSS 501 Data Structures and Object-Oriented Programming Professor Clark F. Olson

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

INF2220: algorithms and data structures Series 1

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

COMP 250 Fall binary trees Oct. 27, 2017

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Prefix/Infix/Postfix Notation

1. Stack overflow & underflow 2. Implementation: partially filled array & linked list 3. Applications: reverse string, backtracking

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

Binary Trees. Height 1

DATA STRUCTURES AND ALGORITHMS

The Stack and Queue Types

CSI33 Data Structures

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

PA3 Design Specification

Transcription:

Algorithms and Data Structures PD Dr. rer. nat. habil. Ralf Peter Mundani Computation in Engineering / BGU Scientific Computing in Computer Science / INF Summer Term 2018

Part 2: Data Structures PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 2

overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 3

Prelude 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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 4

overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 5

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 k 1 n k linked list with nodes n i, 1 i k PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 6

Linked Lists fundamentals (cont d) each node contains one container for storing data item s 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 k 1 n k NIL linked list with nodes n i, 1 i k PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 7

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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 8

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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 9

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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 10

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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 11

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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 12

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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 13

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) PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 14

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 k th item has a complexity of (n) as all nodes have to be processed successively until the k th 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 k 1 n k NIL doubly linked list with nodes n i, 1 i k TAIL PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 15

overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 16

Stacks fundamentals for many applications rather hard restrictions w.r.t. 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 flexibility 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 end (top) of stack source: wordpress.com pop: removing one element from end (top) of stack and returning its value PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 17

Stacks fundamentals (cont d) stacks are, for instance, used for evaluating arithmetic expressions (e.g. within pocket calculators) 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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 18

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 used element in a stack last 3 9 7 size n 8 hence, operations push and pop ready to be implemented 4 1 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 19

Stacks implementation of stacks (cont d) push (here with 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 note: there s no error in case stack is full PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 20

Stacks implementation of stacks (cont d) pop (here with variables n and last with global visibility ) procedure POP (S) tmp false if last 0 then tmp S(last) last last 1 fi return (tmp) end where S indicates a stack PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 21

Stacks example evaluation of an arithmetic expression A B C (D E) using a stack in postfix notation: B, C, D, E,,, stack S push (S, B) push (S, C) push (S, D) push (S, E) E push (S, pop (S) pop (S)) D push (S, pop (S) pop (S)) C C push (S, pop (S) pop (S)) B B B A E D C A B note: operators and have to switch operands due to LIFO ordering PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 22

overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 23

Queues fundamentals other representatives of restricted source: callcentrehelper.com 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 end of queue remove: removing one element from queue s front 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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 24

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 number of already used cells indices first and last indicate the first and the last used element in a queue ( circular array: wraparound back to 0 at end of array) used 5 array index last 1 first 7 0 1 2 3 4 5 6 7 8 9 15 18 4 7 11 size n 10 hence, operations insert and remove ready to be implemented PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 25

Queues implementation of queues (cont d) insert (here with 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 note: there s no error in case queue is full PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 26

Queues implementation of queues (cont d) remove (here with variables n, used, first, and last with global visibility ) procedure REMOVE (Q) tmp false if used 0 then tmp Q(first) used used 1 first first 1 if first n then first 0 fi fi return (tmp) end where Q indicates a queue PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 27

overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 28

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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 29

Trees fundamentals (cont d) further definitions 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 non terminal nodes parent children internal nodes leaves PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 30

Trees fundamentals (cont d) further definitions 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 level 2 siblings level 3 height 3 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 31

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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 32

Trees representation of trees (cont d) example A B E G C D F H I J HEAD A B E G C D F H I J i.e. link pointing to NIL PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 33

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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 34

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 max. 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) (i.e. 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 node s parent due to missing upward link PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 35

Trees binary trees (cont d) example A B F HEAD C G A D E B F C G D E i.e. link pointing to NIL PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 36

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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 37

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) PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 38

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 PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 39

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 parent s link to child of the node to be deleted node to be deleted PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 40

Trees binary trees (cont d) case c: deleting an internal node with two children 1 1) find the in order successor (IOS) of the node to be deleted 2) copy IOS to the node to be deleted 3) i. if IOS has no children, simply delete it ii. if IOS has a right child ( ), set IOS s parent ( ) to IOS s right child node to be deleted in order successor IOS 1 as suggested by T. HIBBARD in 1962, guarantying that the heights of the subject subtrees are changed by at most one PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 41

overview prelude linked lists stacks queues trees PD Dr. Ralf Peter Mundani Algorithms and Data Structures Summer Term 2018 42