Data Structures Flashcards
Week 2.10, 2.11 (26 cards)
describe arrays
- stored contiguously in computer memory
- same data type
- insertion at the end of the array = O(1)
- insertion at a new element of index i = O(n)
describe linked lists
- node = data + one or more links to other nodes
- last node - next field set to null
- memory NOT allocated contiguously
- size of data field for nodes can be different
big O to locate an item in a linked list
n
big O to insert an item in a linked list
1
big O to delete an item in a linked list
1
big O to insert an item into an array
last element - 1
element at i - 1
big O to delete an item into an array
last element - 1
element at i - 1
big O to locate an item into an array
unsorted - n
sorted - log n
describe ADT stack
- LIFO
- push = insert
- pop = delete
uses of stacks
- reversing data
- parsing
- backtracking
describe ADT queue
- front & rear
- FIFO
- enqueue = inset
- dequeue = delete
uses of queues
- scheduling
- simulation
define priority queues
- as an item enters the queue it is given a priority
- this determines the order in which the queue is output/read
- higher priorities processed before lower ones even if the entered the queue after them
array implementation of stacks
limited size
linked list implementation of stacks
‘unlimited’ size
- but extra-memory references
applications of priority queues
- a subroutine in more complex algorithms
- heapsort algorithms
describe heap implementation
- uses a partially ordered binary tree
- allows insertion & deletion with O(log n)
- if operations repeated it is more efficient than linked lists and arrays
describe trees
each node in a tree, except the root, has one parent
- parent-child relationship
- nodes without child-nodes = leaves
2 conditions of a complete binary tree
- all internal nodes have 2 children
- all leaves have the same depth
define heap
a complete binary tree down to a depth of d - 1 and the nodes with depth d are as far left as possible
2 properties of heaps
- the root of the heap always contains the largest element
- depth of heap with n elements is lower bound(log2n)
describe a heap as its array implementation
illustration - ordered binary tree
programs - array
- root node = A[0]
- parent node = A[i]
- 2 child nodes = A[2i + 1] and A[2i + 2]
heap insert
- attach new node with key K after last leaf
- sift k UP to appropriate place in heap
- O(log n)
heap delete
- exchange root’s key with the last K of the heap
- decrease heap size by 1
- sift K down to its appropriate place in the heap
- O(log n)