Queue and Tree in C Yang Zhengwei CSCI2100B Data Structures Tutorial 5 1
Queue ticket office of the Ocean park? 2
Queue Overview First In First Out (FIFO) Enqueue Dequeue 3
Queue Implementation A queue may be implemented using linked-list or array Implement a queue using array 4
Queue Implementation Implementing a queue using circular array 5
Queue Implementation createQueue 6
Queue Implementation enqueue 7
Queue Implementation dequeue 8
Queue Implementation isEmpty, isFull 9
Queue Implementation front, makeEmpty 10
Binary Tree Overview Binary tree – Degree of tree is 2 11
Trees – traversal (Recursion) Preorder 12
Trees – traversal (Recursion) Inorder 13
Trees – traversal (Recursion) Postorder 14
Trees - traversal Preorder A B D G H E C F I Inorder G D H B E A F I C Postorder G H D E B I F C A 15 A B C D E F I G H
Traversal(non-recursion) INORDER(node) parentStack = empty stack while (not parentStack.isEmpty() or node ≠ null) if (node ≠ null) parenStack.push(node) node = node.left else node = parentStack.pop() visit(node) node = node.right
A B E CD C B A A B E CD B A A B E CD D A A B E CD A A B E CD E A B E CD Output: C Output: Output: C B Output: C B DOutput: C B D A Output: C B D A E
Traversal(non recursion) PREORDER(node) parentStack = empty stack while (not parentStack.isEmpty() or node ≠ null) if (node ≠ null) visit(node) if (node.right ≠ null) parentStack.push(node.right) node = node.left else node = parentStack.pop() Preorder
POSTORDER (node) parentStack = empty stack lastnodevisited = null while (not parentStack.isEmpty() or node ≠ null) if (node ≠ null) parentStack.push(node) node = node.left else peeknode = parentStack.peek() if (peeknode.right ≠ null and lastnodevisited ≠ peeknode.right) /* if right child exists AND traversing node from left child, move right */ node = peeknode.right else visit(peeknode) lastnodevisited = parentStack.pop() Traversal(non recursion) Postorder