COMP2230 Introduction To Algorithmics: Lecture Overview
COMP2230 Introduction To Algorithmics: Lecture Overview
Lecture Overview
Data Structures, revision Text, Chapter 3, Sections 3.1-3.4 Next Lecture:
Data Structures, revision Text, Chapter 3, Sections 3.5-3.6 Searching, Text, Chapter 4
Arrays
An one-dimensional array is a sequence of n items each having an index between 0 and n-1 associated with it. All data items are of the same data type (e.g. integers). In an array, an element (data item) can be accessed directly by specifying its index; thus, arrays provide constant time access to elements. However, inserting or deleting an element in an array, takes (n).
a[0] a[1] a[2] ... a[n-1]
Linked Lists
Unlike an array, a linked list provides a constant time insertion and deletion anywhere in the list, but in the worst case it takes (n) time to access an element.
Linked Lists
A linked list can be implemented as a list of nodes, where each node has a field data and a field next , which references the next node in the list. A list also contains a variable start that references the first node in the list. The next field in the last node in null.
start
13
-4
87
Linked Lists
Inserting in a linked list :
start 13 -4 87
20
Stacks
A stack is an abstract data type (ADT) that has the following functions:
stack_init(): initialize stack (make it empty) empty(): return true if the stack is empty and return false if the stack is not empty push(val): add the item val to the stack pop(): remove the item most recently added to the stack top(): return the item most recently added to the stack but do not remove it
Example
Starting from empty stack:
s.push(16) s.push(5) s.push(32) s.pop()
t
16 5 33 16
16 5 33
Queues
A queue is very similar to a stack; the only difference is that when an item is deleted from a queue, then the least recently added item is deleted, not the most recently added as in a stack First In First Out - FIFO.
Queues
The functions on a queue are: queue_init(): initialize the queue (make it empty) empty(): return true if the queue is empty and return false if the queue is not empty enqueue(val): add the item val to the queue dequeue(): remove the item least recently added to the queue front(): return the item least recently added to the queue but do not remove it
Queues
Queue can also can be implemented using arrays.
Variables r and f are used to denote the rear and the front of a queue.
For an empty queue, r=f=-1. When an item is added to an empty queue, both r and f are set to 0 and the item is put in the cell at index 0. When an item is added to non empty queue, r is incremented and the item is put in the cell at index r. If r is the index of the last cell in the array, it is set to 0.
Queues
When an item is deleted from the front of a queue, f gets incremented. If f is the index of the last cell in the array, it is set to 0. When an item is deleted from a queue and queue becomes empty, both r and f are set to 1.
Queues
Starting from an empty queue:
7 55 13 2
rf
28
r
7 247 13
f
2
1. q.enqueue(28) 2. q.enqueue(55) 3. q.enqueue(13) 4. q.dequeue() 5. q.enqueue(2) 6. q.enqueue(7) 7. q.dequeue() 8. q.dequeue() 9. q.enqueue(247) 10. q.dequeue() 11. q.dequeue() 12. q.dequeue()
rf
28 55 13
f
2
247 13
f
28 55
r
13
f
7
r
247 13 2
r
28 55 13
f
7 55 13
f2 r
2
rf
7 247 13 2
rf
3 4
The Algorithm runs in time (n+m), where n is the number of vertices and m number of edges in the graph.
The Algorithm runs in time (n2), where n is the number of vertices in the graph.
Binary Trees
A binary tree is a rooted tree where each node has either:
No children One child Two children.
A child can be either a left child or a right child. To traverse a binary tree means to visit each node in the tree in some prescribed order.
Binary Trees
Example:
1
4 6
Binary Trees
Preorder traversal of a binary rooted tree: If root is empty stop. Visit root Execute preorder on the binary tree rooted at the left child of the root Execute preorder on the binary tree rooted at the right child of the root 1
2 3
4 6
This algorithm returns the number of nodes in the binary tree with root root. Input Parameter: root Output Parameters: None count_nodes(root) if (root == null ) return 0 count = 1 // count root count = count + count_nodes(root. left ) // add in nodes in left subtree count = count + count_nodes(root. right) // add in nodes in right subtree return count }
Inorder traversal of a binary rooted tree: If root is empty stop. Execute inorder on the binary tree rooted at the left child of the root Visit root Execute inorder on the binary tree rooted at the right child of the root Postorder traversal of a binary rooted tree: If root is empty stop. Execute postorder on the binary tree rooted at the left child of the root Execute postorder on the binary tree rooted at the right child of the root Visit root
This algorithm inserts the value val into a binary search tree with root root. If the tree is empty, root = null. The algorithm returns the root of the tree containing the added item. We assume that new node creates a new node with data field data and reference fields left and right. Input Parameter: root ,val Output Parameters: None BSTinsert (root,val) { // set up node to be added to tree temp = new node temp. data = val temp. left = temp. right = null if (root == null) // special case: empty tree return temp BSTinsert_recurs( root ,temp) return root } ...
... BSTinsert_recurs( root ,temp) { if (temp. data root.data) if (root. left == null ) root. left == temp else BSTinsert_recurs( root .left,temp) else if (root. right == null) root. right == temp else BSTinsert_recurs( root .right,temp) }
Input Parameter: root ,ref Output Parameters: None BSTreplace(root,ref) { // set child to refs child, or null, if no child if (ref.left == null) child = ref.right else child = ref.left if (ref == root) { if (child != null) child .parent = null return child } if (ref.parent.left == ref) // is ref left child? ref.parent.left = child else ref.parent.right = child if (child != null) child .parent = ref.parent return root }