Professional Documents
Culture Documents
Winter 2022/2023
Anja Rey
Questions:
36/69 Advanced Programming and Algorithms I – Correctness & Code Quality I Anja Rey
Data Structures
Data Structures Intro
0
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection
0
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection
kehr Key
Data: key's
µ
Now: data structure that contains multiple elements kein
Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)
0
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection
assume a
Data:
key complete
Key a relation
Now: data structure that contains multiple elements yay
Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)
g
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection
assume a
Data:
key complete
Key a relation
Now: data structure that contains multiple elements yay
Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)
g
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection
assume a
Data:
key complete
Key a relation
Now: data structure that contains multiple elements yay
Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)
g
initialise: create a new collection
search: find an element with key x
insert: add an element to the collection
delete: delete a given element from the collection
assume a
Data:
45 key complete
order wrt
key Key a relation
Now: data structure that contains multiple elements yay
Examples: arrays, lists, trees, graphs, . . .
Types for multiple elements (objects) in Python: tuple (immutable); list, set, dict (mutable)
0 1 2 4 d
length n
z
affixed
index length n
z
affixed
index length n
z
WE affixed
Key Ali
index length n
z idk affixed
su HTkeyATi
E1
initialise: array of length n: A Ω new array A[0..n ≠ 1]
search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten
index length n
z idk affixed
su HTkeyATi
E1
initialise: array of length n: A Ω new array A[0..n ≠ 1]
search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten
index length n
z idk affixed
su HTkeyATi
E1
initialise: array of length n: A Ω new array A[0..n ≠ 1]
search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten
index length n
z idk affixed
su HTkeyATi
E1
initialise: array of length n: A Ω new array A[0..n ≠ 1]
search: find element (index) with key x in O(n)
insert: set the value at an index i to A[i] = x in O(1)
delete: element remains in the array unless overwritten
variants: singly vs. doubly linked, sorted vs. unsorted, distinguished elements (head, tail), . . .
one element:
variants: singly vs. doubly linked, sorted vs. unsorted, distinguished elements (head, tail), . . .
one element:
variants: singly vs. doubly linked, sorted vs. unsorted, distinguished elements (head, tail), . . .
one element:
variants: singly vs. doubly linked, sorted vs. unsorted, distinguished elements (head, tail), . . .
one element:
variants: singly vs. doubly linked, sorted vs. unsorted, distinguished elements (head, tail), . . .
one element:
Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure
HEAD
X
insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L
Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure
HEAD
insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L
Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure
HEAD
X
insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L
Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure
HEAD
X D
insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L
Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure
HEAD
I I I
insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L
Insert Key
input: data structure D, key x
output: data structure that contains the same data and structure as D
together with an element with key x following the structure
HEAD
I I I
insert_simple_list(L, x ):
1 e Ω new element
2 e.key Ω x
3 e.next Ω L.head
4 L.head Ω e
5 return L
HEAD
4 3 77 74 72 1
Find Key
input: data structure D, key x
output: An element with key x
HEAD
4 3 77 74 72
search_simple_list(L, x ):
1 e Ω L.head
2 while e ”= nil and e.key ”= x do
3 e Ω e.next
4 return e
Find Key
input: data structure D, key x
output: An element with key x
HEAD
3 77 74 72
search_simple_list(L, x ):
1 e Ω L.head
2 while e ”= nil and e.key ”= x do
3 e Ω e.next
4 return e
Find Key
input: data structure D, key x
output: An element with key x
HEAD
3 77 74 72
4
search_simple_list(L, x ):
1 e Ω L.head
2 while e ”= nil and e.key ”= x do
3 e Ω e.next
4 return e
Find Key
input: data structure D, key x
output: An element with key x
HEAD
3 77 74 72
search_simple_list(L, x ):
4
1 e Ω L.head
2 while e ”= nil and e.key ”= x do
3 e Ω e.next
4 return e
Find Key
input: data structure D, key x
output: An element with key x
IET 7 34 72
search_simple_list(L, x ):
4
1 e Ω L.head
2 while e ”= nil and e.key ”= x do
m
3 e Ω e.next
4 return e
Find Key
input: data structure D, key x
output: An element with key x
IET 7 34 72
search_simple_list(L, x ):
4
1 e Ω L.head
2 while e ”= nil and e.key ”= x do
Un
3 e Ω e.next
return e
4
an
41/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Linked List – Delete
Delete Element
input: data structure D, element e
output: the same data structure as D without element e
HEAD
77 74 72
need to remember the previous element
Delete Element
input: data structure D, element e
output: the same data structure as D without element e
HEAD
It 2
need to remember the previous element
Delete Element
input: data structure D, element e
output: the same data structure as D without element e
HEAD
3 77 74 72
need to remember the previous element
Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x
Ä P
7 4 72 -
7
search_including_predecessor_simple_list(L, x ):
1 if L.head.key = x then
2 return nil, L.head
3 p Ω L.head
4 e Ω p.next
5 while e ”= nil and e.key ”= x do
6 pΩe
7 e Ω e.next
8 return e, p
42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Linked List – Delete
Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x
HEAD
P
37 4 72
search_including_predecessor_simple_list(L, x ):
1 if L.head.key = x then
2 return nil, L.head
3 p Ω L.head
4 e Ω p.next
5 while e ”= nil and e.key ”= x do
6 pΩe
7 e Ω e.next
8 return e, p
42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Linked List – Delete
Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x
HEAD
77 74 72
search_including_predecessor_simple_list(L, x ):
1 if L.head.key = x then
2 return nil, L.head
3 p Ω L.head
4 e Ω p.next
5 while e ”= nil and e.key ”= x do
6 pΩe
7 e Ω e.next
8 return e, p
42/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Linked List – Delete
Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x
HEAD
P
3 77 4 72
delete_key_simple_list(L, x ):
1 e, p Ω search_including_predecessor_simple_list(L, x )
2 if p ”= nil then
3 p.next Ω e.next
4 else
5 L.head Ω e.next
6 return L
Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x
HEAD
77 2 7
delete_key_simple_list(L, x ):
1 e, p Ω search_including_predecessor_simple_list(L, x )
2 if p ”= nil then
3 p.next Ω e.next
4 else
5 L.head Ω e.next
6 return L
Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x
HEAD
77 2 7
delete_key_simple_list(L, x ):
1 e, p Ω search_including_predecessor_simple_list(L, x )
2 if p ”= nil then
3 p.next Ω e.next
4 else
5 L.head Ω e.next
6 return L
Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x
4
HEAD
3 77 2
delete_key_simple_list(L, x ):
1 e, p Ω search_including_predecessor_simple_list(L, x )
2 if p ”= nil then
3 p.next Ω e.next
4 else
5 L.head Ω e.next
6 return L
Delete Key
input: data structure D, key x
output: the same data structure as D without an element with key x
4
HEAD
3 77 2 I
delete_key_simple_list(L, x ):
1 e, p Ω search_including_predecessor_simple_list(L, x ) Ola
2 if p ”= nil then
3 p.next Ω e.next
4 else
5 L.head Ω e.next
6 return L
HEAD
S S S SNIL
one element:
HEAD
Nils
- - Ä SN
one element:
HEAD
NIL S SNIL
c
one element:
prä hext
key
Delete Element
input: data structure D, element e
output: the same data structure as D without element e
HEAD
Nils 3 7 S
4 si 2 SNIL
delete_key_simple_list(L, e):
1 if e.prev ”= nil then
2 e.prev.next Ω e.next
3 else
4 L.head Ω e.next
5 if e.next ”= nil then
6 e.next.prev Ω e.prev
7 return L
44/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Doubly Linked List
Delete Element
input: data structure D, element e
output: the same data structure as D without element e
HEAD
NIL -
3 - -
7 I 2 NIL
delete_key_simple_list(L, e):
1 if e.prev ”= nil then
2 e.prev.next Ω e.next
3 else
4 L.head Ω e.next
5 if e.next ”= nil then
6 e.next.prev Ω e.prev
7 return L
44/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Doubly Linked List
Delete Element
input: data structure D, element e
output: the same data structure as D without element e
HEAD
NIL -
3 - -
7 I 2 NIL
delete_key_simple_list(L, e):
1 if e.prev ”= nil then
2 e.prev.next Ω e.next
3 else
4 L.head Ω e.next
5 if e.next ”= nil then
6 e.next.prev Ω e.prev
7 return L
44/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
In Theory: Doubly Linked List
Delete Element
input: data structure D, element e
output: the same data structure as D without element e
HEAD
NIL -
3 - -
7 i 2 NIL
delete_key_simple_list(L, e):
1 if e.prev ”= nil then
2 e.prev.next Ω e.next
3 else
4 L.head Ω e.next
5 if e.next ”= nil then
6 e.next.prev Ω e.prev
7 return L a
44/69 Advanced Programming and Algorithms I – Data Structures Anja Rey
What’s next?