computer science for engineers - kit · computer science for engineers lecture 10 data ... [boru00]...

51
Computer Science for Engineers Lecture 10 Data structures – part 4 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing. Dan Gutu 22 nd of January 2010

Upload: nguyenthien

Post on 12-Sep-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Computer Science forEngineers

Lecture 10Data structures – part 4

Prof. Dr. Dr.-Ing. Jivka OvtcharovaDipl. Wi.-Ing. Dan Gutu

22nd of January 2010

Page 2: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 2

Binary Search Trees: Deleting, Case a (1)

case a: vertex is a leaf, that means, it has no successor.

make the corresponding child vertex (here: D.left) of the belonging father vertex k.father (here: D) zero.

B

L

P

C

D

E

A N R

ZO

Delete the vertex with the data element object o with the content „C“.

4. D

ata

Stru

ctur

es

4.4

Tre

es

Page 3: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 3

Binary Search Trees: Deleting, case a (2)

B

L

P

CD

E

A N RZO

:BNode

left, right

data

:Object

D

:BNode

left, right

data

:Object

C

:BNode

left, right

data

:Object

D

zero

Delete the vertex with the data element object o with the content „C“.

4. D

ata

Stru

ctur

es

4.4

Tre

es

Page 4: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 4

Binary Search Trees: Deleting, case b (1)

case b: vertex k has exactly 1 successor

place the corresponding child vertex indicator (here: P.left) of the belonging father vertex k.father (here: P) on the non-empty successor of the vertex k to be deleted (here N.right = O).

B

L

P

C

D

E

A N R

ZO

Delete the vertex with the data element object o with the content „N“.

4. D

ata

Stru

ctur

es

4.4

Tre

es

Page 5: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 5

Binary Search Trees: Deleting, case b (2)

B

L

P

CD

E

A N RZO

:BNode

left, right

data

:BNode

left, right

data

:Object

N

:BNode

left, right

data

:Object

O

:BNode

left, right

data

:Object

P

:BNode

left, right

data

:Object

O

:Object

P

Delete the vertex with the data element object o with the content „N“.

4. D

ata

Stru

ctur

es

4.4

Tre

es

Page 6: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 6

Binary Search Trees: Deleting, case c (1)

case c: vertex k has exactly 2 successors

replace the vertex to be deleted k (here: L) with the largest vertex from the left side of the tree (here: E, on the right side)

correct references (here: D.right = zero)

B

L

P

C

D

E

A N R

ZO

Delete the vertex with the data element object o with the content „L“.

4. D

ata

Stru

ctur

es

4.4

Tre

es

Page 7: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 7

Binary Search Trees: Deleting, case c (2)

B

L

P

CD

E

A N R

ZO

:BNode

left, right

data

:BNode

left, right

data

:BNode

left, right

data

:Object

D

:Object

E

zero

:Object

L

:BNode

left, right

data

:Object

D

:BNode

left, right

data

:Object

E

Delete the vertex with the data element object o with the content „L“.

4. D

ata

Stru

ctur

es

4.4

Tre

es

Page 8: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 8

Examples for trees

• Scene graphs

- Representation of 3D-scenes

- Starting from one node, the geometry of all object is represented in a tree structure

- Geometries are subdivided into sub-geometries

- The branches can be manipulated. Ex., positioning, orientation, …

- Each geometry has its position in the scene, an orientation and a behavior

- With a camera object that can be navigated, the entire scene can be made visible

4. D

ata

Stru

ctur

es

4.4

Tre

es

Page 9: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 9

Scene graph4.

Dat

a St

ruct

ures

4.4

Tre

es

Page 10: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 10

Example XML

• Documents are stored in a tree structure

• Tags (like in HTML) can be defined

• A meta-language defines the formal aspects of the tags

• Prescribed: opening tag, closing tag, in between content, attributes.

<?xml version="1.0" encoding="ISO-8859-1"?> <my_band> <Bandname>Nero's Delight</Bandname> <musicians> <musician> <name>Michael</name> <instrument>Trumpet</instrument> <lives_in> <Town>Bonn</Town> </lives_in> </musician> <musician> <name>Heidrun</name> <instrument>Trombone</instrument> <lives_in> <Town>Köln</Town> </lives_in> </musician> <!-- and so on --> </musicians> </my_band>

4. D

ata

Stru

ctur

es

4.4

Tre

es

Page 11: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 11

Literature

[Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur Modellierung multidisziplinärer dynamischer Systeme“, Frontiers in Simulation, SCS European Publishing House, Erlangen, 2000, ISBN: 1-56555-183-4

[FMUP05] Vorlesung „Formale Methoden und Programmierung“,2005

[Schn98] H.-J.Schneider (Hrsg.), „Lexikon der Informatik“, Oldenbourg Verlag, 1998

[Wid02] Ottmann, Widmayer, „Algorithmen und Datenstrukturen“,Spektrum Akademischer Verlag, 2002

[WaWa86] Waldschmidt, Walter, „Grundzüge der Informatik“ – Band 1 & 2,Spektrum Akademischer Verlag, 1998

[Wald87] Waldschmidt, „Einführung in die Informatik für Ingenieure“,Oldenbourg Verlag, 1987

Page 12: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 12

Literature

[Sen05] Sen, S.: „Bondgraphs – a formalism for modeling physical systems“, School of Computer Science, 2005

[Bisw02] Biswas, G.: „Model-based reasoning“ Lecture 13-15, Vanderbilt University, 2002

[Boru06] Borutzky, W.: „Modellierung multidisziplinärer Systeme mit Bondgraphen“, Labor für Simulationstechnik, 2006

[Math06] Mathews, J.: „Intelligent powertrain design“, Mississipi State University, 2006

Page 13: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 13

Outline

Lecture Content

3. Object orientation

1. Preface

2. Basics

4. Data Structures

4.2. Graphs

4.3. Use of Graphs

5. Algorithms

5.1. Introduction

4.4. Trees

4.5. Use of Trees

4.6. Linked Lists

4.7. Queues and Stacks

Page 14: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 14

CATIA – Product Data Structure: Example

The structuring of product data also concerns the organization of manufacturing. The product is thus separated into small chunks:

- Assembly groups

- Sub assembly groups

- Single parts

- Graphs

- Standard parts

- Documentation

4. D

ata

stru

ctur

es

4.5

Use

of t

rees

Page 15: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 15

CATIA : Hierarchical Organization of Manufacturing

In practice, application has established a hierarchical organization for manufacturing :

4. D

ata

stru

ctur

es

4.5

Use

of t

rees

Product

part 1

part 3

part 4

part 2part 6

part 5

Assembly group 1

Assembly group 2

Assembly group 3

Page 16: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 16

Group

(CATProduct)

Single Parts(CATPart)

Constraints

„Hydraulic Pump“

CATIA – Example for an Assembly Group4.

Dat

a st

ruct

ures

4.5

Use

of t

rees

Page 17: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 17

Networked CATIA Product Data Structures:

The product structures in CATIA contain, next to the geometric data, also functional data and physical characteristics, such as volume, mass, surface, version (work-up edition), etc. These are dependent on one another and represent complex networked structures.

4. D

ata

stru

ctur

es

4.5

Use

of t

rees

Page 18: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 18

Use of Graphs in CATIA (1)

• For the illustration of such dependence in the product data structure, graphs are used.

• By means of graphs, the „desktop“ in CATIA shows the coherences between the graphical, functional, and physical characteristics of a product.

Product 1

part 2Assembly group 1

part 1

4. D

ata

stru

ctur

es

4.5

Use

of t

rees

Page 19: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 19

Use of Graphs in CATIA (2)4.

Dat

a st

ruct

ures

4.5

Use

of t

rees

Page 20: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 20

CATIA – construction tree (CSG tree)

The construction of a complex assembly part is created by functional and constructional coherences in a Constructive Solid Geometry (CSG) tree (constructive solid body geometry).

• By this way, the geometry of complex assembly parts can be generated from primitive bodies such as cubes, cylinders, prisms, spheres, or rings - that are linked with operations.

• Usually, the common boolean operations on sets are used for this purpose: union, difference and intersection, as the example shows.

4. D

ata

stru

ctur

es

4.5

Use

of t

rees

Page 21: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 21

Pro/ENGINEER – Example for Choosing a Standard Part from a Parts Library4.

Dat

a st

ruct

ures

4.5

Use

of t

rees

Page 22: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 22

Outline

Lecture Content

3. Object orientation

1. Preface

2. Basics

4. Data Structures

4.2. Graphs

4.3. Use of Graphs

5. Algorithms

5.1. Introduction

4.4. Trees

4.5. Use of Trees

4.6. Linked Lists

4.7. Queues and Stacks

Page 23: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 23

A simple linked list represents a special case of a tree.

• The definition of a simple linked list is easily derived from the definition of a binary tree, since a simple linked list is just a limitation on the tree: each vertex has at the most exactly 1 successor.

From a Tree to a Simple Linked List4.

Dat

a st

ruct

ures

4.6

Link

ed li

sts

Binary tree Simple linked list

Page 24: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 24

Definition 3.10: the binary tree T = (V,E) is a simple linked list when exactly

d(T) = 1

That means, each vertex of T has exactly one child vertex.

Simple Linked List

Characteristics:• There is exactly one list element without a predecessor

(first list element)

• There is exactly one list element without a successor(last list element)

example:

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 25: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 25

Simple Linked List as a Dynamic Data Structure (1)

Simple linked lists are achieved in Java through the following:

• Construction of the list from 2 types of head elements:- The vertex (Node) as a head element knows its successor and a data element.

- a List-Element (List) knows the first vertex.

List illustration:

Implementation:

:Node

next

data

:Node

next

data

:Node

next

data

:Object

A:Object

C

:Object

B

:List

head

A B C

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 26: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 26

Simple Linked List as a Dynamic Data Structure (2)

example: simply linked, with additional indicators at the end (UML)

• classes:

- List – list class, provides methods

- Node – head vertex

- Object – data object

Recursive Association of the„Node“ class

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

List

Node

Object

Page 27: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 27

Operations for Simple Linked Lists

insert• At the start : void insertAtHead(Object o)

• At position i : void insertAt(int i, Object o)

read• Testing of content : boolean contains(Object o)

• Read the first element : Object getFirst()

• Read at position i : Object get(int i)

delete• Delete the first element : void removeAtHead()

• Delete at position i : void removeAt(int i)

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 28: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 28

Simple Linked Lists: Inserting (Pseudocode)

Insert the vertex vk with the data element object o in a non-empty simple linked list at position i.

c : indicates the vertex

j : Integer

Initialization:

c = head, j = 0 : start of the first element

loop:

as long as j < i - 1 : search of the insertion position

j = j + 1

c = c.next

Insert operation:

k.next = c.next : insert the vertex k

c.next = k

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 29: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 29

Simple Linked Lists: Inserting - Example (1)

Insert the vertex vk at position i.

Initialization:

c = head, j = 0

head

v0 v1 vi-1

c

vi vn

vkj = 0k

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 30: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 30

Simple Linked Lists: Inserting - Example (2)

Insert the vertex vk at position i.

loop:

as long as j < i - 1

j = j + 1

c = c.next

(current state after running the loop)

head

v1 v2 vi-1

c

vi vn

j = index - 1vkk

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 31: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 31

Simple Linked Lists: Inserting - Example (3)

Insert the vertex vk at position i.

Insert operation:

k.next = c.next

c.next = k

head

v1 v2 vi-1

c

vi vn

j = index - 1vkk

c.next

k.next

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 32: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 32

Simple Linked Lists: Inserting - Example (4)

Insert the vertex vk at position i.

Insert operation:

k.next = c.next

c.next = k

head

v1 v2 vi-1

c

vi vn

j = index - 1vkk

k.next

c.next

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 33: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 33

Simple Linked Lists: Inserting - Special Case (Pseudocode)

The insertion of the vertex vk in an empty simple linked list is a special case that must be dealt with.

Since the list is empty, the vertex vkcan be inserted at no further expense:

head = k

k.next = zero

head

vk

zero

head vk zero

k

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 34: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 34

Simple Linked Lists: Deleting (Pseudocode)

Delete the vertex vi with the data element object o in a non-empty simple linked list at position i.

c, k : indicates a vertex ( k – temporary indicator)

j : Integer

Initialization:c = head, j = 0 : starts with the first element

loop:as long as j < i - 1 : search for the position to be deleted

j = j + 1

c = c.next

Delete operation:k = c.next : delete the vertex

c.next = k.next

delete k

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 35: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 35

Simple Linked List: Deleting - Example (1)

Delete the vertex vi at position i.

Initialization:

c = head, j = 0

head

v0 v1

c

vn

j = 0

vi-1 vi vi+1

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 36: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 36

Simple Linked List: Deleting - Example (2)

Delete the vertex vi at position i.

loop:

as long as j < i - 1

j = j + 1

c = c.next

(current state after running the loop)

head

v0 v1

cj = index - 1

vnvi-1 vi vi+1

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 37: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 37

Simple Linked List: Deleting - Example (3)

Delete the vertex vi at position i.

Delete operation:

k = c.next

c.next = k.next

delete k

j = index - 1

head

v0 v1

c

vnvi-1 vi vi+1

c.next

k

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 38: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 38

Simple Linked List: Deleting - Example (4)

Delete the vertex vi at position i.

Delete operation:

k = c.next

c.next = k.next

delete k

j = index - 1

head

v0 v1

c

vnvi-1 vi vi+1k.next

k

c.next

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 39: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 39

Simple Linked List: Deleting - Example (5)

Delete the vertex vi at position i.

Delete operation:

k = c.next

c.next = k.next

delete k

j = index - 1

head

v0 v1

c

vnvi-1 vi vi+1

k

k.next

c.next

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 40: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 40

Simple Linked List: Deleting, Special Cases

The special case of the element to be deleted vi, that is the first in the list (i = 0), must be dealt with.

Since the element is the first in the list, the vertex vi can be removed from the list without further expense and later be deleted with:

head = head.next

head

vi v1 vn

head

vi v1 vnhead.next

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 41: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 41

Other Types of Lists: Double Linked Lists

Double linked listsThe simple linked list is supplemented with the following references:• Each vertex contains a reference to his predecessor.• tail references the end of the list.

advantages:• Insertion operations in front and behind are fast to perform, since one

must not run through the list.

• One can run through the list in both directions.

head

v0 v1 vn-1 vn

tail

v2

4. D

ata

stru

ctur

es

4.6

Link

ed li

sts

Page 42: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 42

Outline

Lecture Content

3. Object orientation

1. Preface

2. Basics

4. Data Structures

4.2. Graphs

4.3. Use of Graphs

5. Algorithms

5.1. Introduction

4.4. Trees

4.5. Use of Trees

4.6. Linked Lists

4.7. Queues and Stacks

Page 43: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 43

Queues and Stacks

The characteristics of the double linked lists allow for the implementation of 2 important data structures.

• Queues: FIFO – „First in First out“

Elements are extracted in the order in which they enter the queue.

Everyday examples: Bank counter, supermarket, waiting room at the doctor

• Stacks: LIFO – „Last in First out“

Elements are extracted in reverse order of how they enter the line.

Everyday examples: plate sequence, overcrowded busses, recursive problems

4. D

ata

stru

ctur

es

4.7

Line

s an

d se

quen

ce

Page 44: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 44

Queues

Operations:• The first element is „served“

• A new element is added behind

Application:example: Administration of print orders or processes

Processed up front

Added behind

4. D

ata

stru

ctur

es

4.7

Line

s an

d se

quen

ce

Page 45: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 45

Queues: Operations

Minimum Operations:• write

insert (add) elements at the end void enqueue(Object o)

• deleteremove an element from the beginning void dequeue()

• Read and searchreturns the beginning element, without removal Object front()

Optional Operations:• Number of elements int size()

• Queue empty? boolean empty()

4. D

ata

stru

ctur

es

4.7

Line

s an

d se

quen

ce

Page 46: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 46

Queues - Example

Operation Content of Queue

output

new Queue()

enqueue (1)

enqueue (3)

front ()

enqueue (7)

dequeue ()

front ()

dequeue ()

front ()

()

( 1 )

( 1, 3 )

( 1, 3 )

( 1, 3, 7 )

( 3, 7 )

( 3, 7 )

( 7 )

( 7 )

-

-

-

1

-

-

3

-

7

4. D

ata

stru

ctur

es

4.7

Line

s an

d se

quen

ce

Page 47: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 47

Stacks

Operations:• Adding is only possible at one end (the back). The oldest elements are at

the front.

• Removal at the same end

Applications:Examples:

- Calculations of multi-body systems and simulations (sequence of transformation matrices)

- Saving of local variables at function calls

4. D

ata

stru

ctur

es

4.7

Line

s an

d se

quen

ce

Page 48: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 48

Stacks: Operations

Minimum Operations:

• writeinserts an element at the end void push(Object o)

• deleteremoves an element from the end void pop()

• Read and searchreturns the last element without deleting Object peek()

Optional Operations:

• Number of elements int size()

• Stack empty? boolean empty()

4. D

ata

stru

ctur

es

4.7

Line

s an

d se

quen

ce

Page 49: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 49

Stacks: Example

Operation Content of Stack

output

new Stack()

push (1)

push (3)

peek ()

push (7)

pop ()

peek ()

pop ()

peek ()

()

( 1 )

( 1, 3 )

( 1, 3 )

( 1, 3, 7 )

( 1, 3 )

( 1, 3 )

( 1 )

( 1 )

-

-

-

3

-

-

3

-

1

4. D

ata

stru

ctur

es

4.7

Line

s an

d se

quen

ce

Page 50: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 50

Recommended Literature

[Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur Modellierung multidisziplinärer dynamischer Systeme“, Frontiers in Simulation, SCS European Publishing House, Erlangen, 2000, ISBN: 1-56555-183-4

[FMUP05] Vorlesung „Formale Methoden und Programmierung“,2005

[Schn98] H.-J.Schneider (Hrsg.), „Lexikon der Informatik“, Oldenbourg Verlag, 1998

[Wid02] Ottmann, Widmayer, „Algorithmen und Datenstrukturen“,Spektrum Akademischer Verlag, 2002

[WaWa86] Waldschmidt, Walter, „Grundzüge der Informatik“ – Band 1 & 2,Spektrum Akademischer Verlag, 1998

[Wald87] Waldschmidt, „Einführung in die Informatik für Ingenieure“,Oldenbourg Verlag, 1987

Page 51: Computer Science for Engineers - KIT · Computer Science for Engineers Lecture 10 Data ... [Boru00] Wolfgang Borutzky: „Bondgraphen – Eine Methodologie zur ... with additional

Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 51

Recommended Literature

[Sen05] Sen, S.: „Bondgraphs – a formalism for modeling physical systems“, School of Computer Science, 2005

[Bisw02] Biswas, G.: „Model-based reasoning“ Lecture 13-15, Vanderbilt University, 2002

[Boru06] Borutzky, W.: „Modellierung multidisziplinärer Systeme mit Bondgraphen“, Labor für Simulationstechnik, 2006

[Math06] Mathews, J.: „Intelligent powertrain design“, Mississipi State University, 2006