Lecture 4

Stacks

Stack ADT (operations)

Alt text

Array-base stack

Algorithm size() return t + 1 Algorithm pop() if isEmpty() then throw NoSuchElementException else t <- t - 1 return S[t+1]

Performance

Other implementations

Queues

Queue operations

Alt text

Array-Base queue

Alt text

Operations

The following are enqueue and dequeue operations

Algorithm enqueue(item) if size() = N then throw IllegalStateException else r <- (f + sz) mod N Q[r] <- item sz <- (sz + 1) Algorithm deqeue() if isEmpty() then return null else item <- Q[r] f <- (f + 1) mod N sz <- (sz - 1) return item

Programming: Call stacks

Programming languages use stacks to keep track of the current execution

Alt text

def reverse(inlist): stack = [] for item in inlist: stack.append(item) i = 0 while len(stack) > 0: inlist[i] = stack.pop() i += 1 return inlist reverse([1,2,3,4,5,6]) [6, 5, 4, 3, 2, 1]

Application of Queues: Round Robin Scheduler

Iterators

Iterable interface

mylist = [“a”, “b”, “c”, “d”, “e”] for element in mylist: print(element) a b … x = iter(mylist) # <list_iterator object at 0x000…> print(x) <list_iterator object at 0x000…> m = next(x) print (m) a m = next(x) print(m) b

Implementing iterators

Class Counter: def __init__(self, low, high): self.current = low - 1 self.high = high def __iter__(self): return self def __next__(self): self.current += 1 if self.current < self.high: return self.current raise StopIteration for c in Counter(3, 9): print(c)

Trees

Tree basics

Arthemtic Expression tree

Alt text

Tree terminology

Terminoplogy Meaning
Root (root node) has no parents
Internal Node has at least one child, cannot be an external node
External node a.k.a. leaf
Edge line between nodes
Path set of edges that have to be connected
Ancestors of a node anything connected the node and is 'above' or parent to it
Descendants of a node anything connected and 'below'
Siblings share the same parent
Degree number of outlinks, i.e. links going downwards
level defined as the root being at level 0, and every child node being an additional level down
Depth of a node/tree Count the number of edges from node to the root, Depth of k=3k=3
Height of a node/tree Number of edges on the longest path to a leaf
Subtree A tree that you root at some arbitrary node (e.g. c below)
kk-ary tree At most kk children for a given node
2-ary Tree at most 3 children for a given node
binary Tree k-ary tree
Alt text Alt text

Tree properties

General Tree Structure

Alt text

Tree ADT

Accessor methods:

Query methods

Tree CDT

C is 'concrete'. Common update methods

Preorder Traversal

Alt text

Algorithm preOrder(p) visit(p) for each child c of p preorder(c)

You would get the following:

Postorder traversal

Alt text

Algorithm postOrder(p) for each child c of p postOrder(c) visit(p)

Binary Trees

Properties of complete binary trees

Properties of proper binary trees

Notation:

Binary tree ADT

Additional methods:

name description
left(p)
right(p)
sibling(p) Do I have a sibling, if so, give me a reference to it

Traversal - Inorder traversal

Alt text

((2 * (a − 1)) + (3 * b))

Euler Tour Traversal

Alt text

Linked Structure for Binary Trees

Alt text

Array-Based Representation of Binary Trees

Breadth-first travsersal

Alt text