Download presentation
Presentation is loading. Please wait.
Published byJemimah Bernadette Dickerson Modified over 8 years ago
1
Queue and Tree in C Yang Zhengwei CSCI2100B Data Structures Tutorial 5 1
2
Queue ticket office of the Ocean park? 2
3
Queue Overview First In First Out (FIFO) Enqueue Dequeue 3
4
Queue Implementation A queue may be implemented using linked-list or array Implement a queue using array 4
5
Queue Implementation Implementing a queue using circular array 5
6
Queue Implementation createQueue 6
7
Queue Implementation enqueue 7
8
Queue Implementation dequeue 8
9
Queue Implementation isEmpty, isFull 9
10
Queue Implementation front, makeEmpty 10
11
Binary Tree Overview Binary tree – Degree of tree is 2 11
12
Trees – traversal (Recursion) Preorder 12
13
Trees – traversal (Recursion) Inorder 13
14
Trees – traversal (Recursion) Postorder 14
15
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
16
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
17
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
18
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
19
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.