Computer Science for Engineers

Computer Science for Engineers Lecture 10 Data structures – part 4 Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing. Dan Gutu 22nd of January 2010 ...
Author: Oswin Oliver
1 downloads 1 Views 1MB Size
Computer Science for Engineers Lecture 10 Data structures – part 4

Prof. Dr. Dr.-Ing. Jivka Ovtcharova Dipl. Wi.-Ing. Dan Gutu

22nd of January 2010

Binary Search Trees: Deleting, Case a (1)

Delete the vertex with the data element object o with the content „C“. 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. L P

A 4.4 Trees

4. Data Structures

B D C

R

N E

O

Z

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

Binary Search Trees: Deleting, case a (2)

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

:BNode

:BNode

left, right

left, right

data

data

zero

:BNode left, right

:Object

:Object

data

D

D

:Object

L

4.4 Trees

4. Data Structures

C

B A

P D

C

R

N E

O

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

Binary Search Trees: Deleting, case b (1)

Delete the vertex with the data element object o with the content „N“. 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).

4.4 Trees

4. Data Structures

L B

P

A

D C

R

N E

O

Z

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

Binary Search Trees: Deleting, case b (2)

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

:BNode

:BNode

left, right

left, right

data

data :BNode

:BNode

:Object

left, right

:Object

P

data

P

left, right

4.4 Trees

4. Data Structures

data :BNode

:Object

:Object

left, right

O

N

data

L

:Object

O

B A

P D

C

R

N E

O

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

Binary Search Trees: Deleting, case c (1)

Delete the vertex with the data element object o with the content „L“. 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)

4.4 Trees

4. Data Structures

L B

P

A

D C

R

N E

O

Z

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

Binary Search Trees: Deleting, case c (2)

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

:BNode

left, right

left, right

data

data

:BNode

:Object

:BNode

:Object

left, right

L

left, right

E

data

data :BNode

:Object

4.4 Trees

4. Data Structures

D

left, right

zero

:Object

data

D

:Object

L

E

B A

P D

C

R

N E

O

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

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

4.4 Trees

4. Data Structures

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

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

4.4 Trees

4. Data Structures

Scene graph

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

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





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

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 Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 11

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

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

Outline Lecture Content 1.

Preface

2.

Basics

3.

Object orientation

4.

Data Structures 5.1. Introduction 4.2. Graphs 4.3. Use of Graphs 4.4. Trees 4.5. Use of Trees 4.6. Linked Lists 4.7. Queues and Stacks

5.

Algorithms

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

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 4.5 Use of trees

4. Data structures

- Documentation

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

CATIA : Hierarchical Organization of Manufacturing

In practice, application has established a hierarchical organization for manufacturing : Product part 1 Assembly group 1

part 3

Assembly group 2

part 4

4.5 Use of trees

4. Data structures

Assembly group 3

part 5 part 6

part 2

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

CATIA – Example for an Assembly Group „Hydraulic Pump“ Group (CATProduct)

Single Parts (CATPart)

4.5 Use of trees

4. Data structures

Constraints

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

Networked CATIA Product Data Structures:

4.5 Use of trees

4. 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.

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

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.

4.5 Use of trees

4. Data structures

Product 1

part 1

Assembly group 1

part 2

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

4.5 Use of trees

4. Data structures

Use of Graphs in CATIA (2)

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

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.

4.5 Use of trees

4. Data structures

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

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

4.5 Use of trees

4. Data structures

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

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

Outline Lecture Content 1.

Preface

2.

Basics

3.

Object orientation

4.

Data Structures 5.1. Introduction 4.2. Graphs 4.3. Use of Graphs 4.4. Trees 4.5. Use of Trees 4.6. Linked Lists 4.7. Queues and Stacks

5.

Algorithms

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

From a Tree to a Simple Linked List

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.

4.6 Linked lists

4. Data structures



Binary tree

Simple linked list

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

Simple Linked List

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. Characteristics: • There is exactly one list element without a predecessor (first list element)

4.6 Linked lists

4. Data structures



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

example:

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

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: A

B

C

:List

:Node

:Node

:Node

head

next

next

next

data

data

data

:Object

:Object

:Object

A

B

C

4.6 Linked lists

4. Data structures

Implementation:

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

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 List

Recursive Association of the„Node“ class

4.6 Linked lists

4. Data structures

Node

Object

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

Operations for Simple Linked Lists

insert •

At the start :

void insertAtHead(Object o)



At position i :

void insertAt(int i, Object o)

4.6 Linked lists

4. Data structures

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)

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

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

:

search of the insertion position

:

insert the vertex k

loop:

4.6 Linked lists

4. Data structures

as long as j < i - 1 j=j+1 c = c.next Insert operation: k.next = c.next c.next = k

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

Simple Linked Lists: Inserting - Example (1)

Insert the vertex vk at position i. Initialization: c = head, j = 0

head

4.6 Linked lists

4. Data structures

v0

v1

vi-1

vi

vn

c j=0

k

vk

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

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 head

v2

4.6 Linked lists

4. Data structures

v1

vi-1

vi

vn

c j = index - 1

k

vk

(current state after running the loop) Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 30

Simple Linked Lists: Inserting - Example (3)

Insert the vertex vk at position i. Insert operation: k.next = c.next c.next = k

head

c.next v2

4.6 Linked lists

4. Data structures

v1

vi-1

vi

c j = index - 1

k

vn

k.next vk

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

Simple Linked Lists: Inserting - Example (4)

Insert the vertex vk at position i. Insert operation: k.next = c.next c.next = k

head

v2

vi-1

vi

vn

c.next

4.6 Linked lists

4. Data structures

v1

c j = index - 1

k

k.next vk

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

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 vk can be inserted at no further expense: head = k k.next = zero

4.6 Linked lists

4. Data structures

head

zero head

k

vk

zero

vk

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

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

:

search for the position to be deleted

:

delete the vertex

loop: as long as j < i - 1

4.6 Linked lists

4. Data structures

j=j+1 c = c.next Delete operation: k = c.next c.next = k.next delete k Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 34

Simple Linked List: Deleting - Example (1)

Delete the vertex vi at position i. Initialization: c = head, j = 0

head

4.6 Linked lists

4. Data structures

v0

v1

vi-1

vi

vi+1

vn

c j=0

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

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 head

v1

4.6 Linked lists

4. Data structures

v0

vi-1

vi

vi+1

vn

c j = index - 1 (current state after running the loop) Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 36

Simple Linked List: Deleting - Example (3)

Delete the vertex vi at position i. Delete operation: k = c.next c.next = k.next delete k head

c.next v1

4.6 Linked lists

4. Data structures

v0

vi-1

vi

c

k

vi+1

vn

j = index - 1

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

Simple Linked List: Deleting - Example (4)

Delete the vertex vi at position i. Delete operation: k = c.next c.next = k.next delete k

c.next head

v1

4.6 Linked lists

4. Data structures

v0

vi-1

vi k.next

c

vi+1

vn

k

j = index - 1

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

Simple Linked List: Deleting - Example (5)

Delete the vertex vi at position i. Delete operation: k = c.next c.next = k.next delete k

c.next head

v1

4.6 Linked lists

4. Data structures

v0

vi-1

c

vi

vi+1

vn

k.next

k

j = index - 1

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

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

4.6 Linked lists

4. Data structures

head

vi

head

head.next

v1

vn

vi

v1

vn

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

Other Types of Lists: Double Linked Lists

Double linked lists The simple linked list is supplemented with the following references: •

Each vertex contains a reference to his predecessor.

• tail references the end of the list. head

4.6 Linked lists

4. Data structures

v0

tail

v1

v2

vn-1

vn

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.

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

Outline Lecture Content 1.

Preface

2.

Basics

3.

Object orientation

4.

Data Structures 5.1. Introduction 4.2. Graphs 4.3. Use of Graphs 4.4. Trees 4.5. Use of Trees 4.6. Linked Lists 4.7. Queues and Stacks

5.

Algorithms

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

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

4.7 Lines and sequence

4. Data structures



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

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

Queues

Operations: •

The first element is „served“



A new element is added behind

4.7 Lines and sequence

4. Data structures

Added behind

Processed up front

Application: example:

Administration of print orders or processes

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

Queues: Operations

Minimum Operations: • write insert (add) elements at the end

void enqueue(Object o)

• delete remove an element from the beginning

void dequeue()

• Read and search

4.7 Lines and sequence

4. Data structures

returns the beginning element, without removal

Object front()

Optional Operations: • Number of elements

int size()

• Queue empty?

boolean empty()

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

Queues - Example

4.7 Lines and sequence

4. Data structures

Operation

Content of Queue

output

new Queue()

()

-

enqueue (1)

(1)

-

enqueue (3)

( 1, 3 )

-

front ()

( 1, 3 )

1

enqueue (7)

( 1, 3, 7 )

-

dequeue ()

( 3, 7 )

-

front ()

( 3, 7 )

3

dequeue ()

(7)

-

front ()

(7)

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

Stacks

4.7 Lines and sequence

4. Data structures

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 Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 47

Stacks: Operations

Minimum Operations: • write inserts an element at the end

void push(Object o)

• delete removes an element from the end

void pop()

• Read and search

4.7 Lines and sequence

4. Data structures

returns the last element without deleting

Object peek()

Optional Operations: • Number of elements

int size()

• Stack empty?

boolean empty()

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

Stacks: Example

4.7 Lines and sequence

4. Data structures

Operation

Content of Stack

output

new Stack()

()

-

push (1)

(1)

-

push (3)

( 1, 3 )

-

peek ()

( 1, 3 )

3

push (7)

( 1, 3, 7 )

-

pop ()

( 1, 3 )

-

peek ()

( 1, 3 )

3

pop ()

(1)

-

peek ()

(1)

1

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

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 Prof. Dr. Dr.-Ing. Jivka Ovtcharova – CSE-Lecture – Ch. 4 - WS 09/10 - Slide 50

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

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