Download presentation

Presentation is loading. Please wait.

Published byAudrey Miller Modified about 1 year ago

1
Queues Queue Q = x 0 x 1 x 2 x 3 … x n-1 n = # elements A queue is a list but the nodes are only accessed first-in-first-out (FIFO). Functions: createEmptyQueue() returns a newly created empty queue front(Q) returns the first node of Q dequeue(Q) returns and removes the first node of Q enqueue(Q, x) returns Q with x added as the last element isEmptyQueue(Q) returns true if Q is empty and false if it is not

2
Homework 3a Describe how to implement a queue using an array (assume it will never have more than 100 elements). Do the five queue functions. Describe how to implement a queue using an set of nodes (this queue will have no number of element limit). Do the five queue functions.

3
Queue – Array NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: int front = the front of the queue int end = the end of the queue NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 in this case front = 1 end = 5

4
createEmptyQueue() declare an array of max size array q[100] int front = 0 int end = 0

5
front(Q) if isEmptyQueue(q) return null else return q[front]

6
dequeue(Q) temp = front front = (front + 1) modulo 100 return q[temp]

7
enqueue(Q, x) q[end] = x end = (end + 1) modulo 100

8
isEmptyQueue(Q) if front = end return true else return false

9
Queue – Linked-List q NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95

10
Queue – Linked-List f NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 e

11
createEmptyQueue() Declare pointers to type node called f and e f null e

12
front(Q) f NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 return node pointed to by f e

13
dequeue(Q) f NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 e

14
dequeue(Q) f NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 temp = f temp e

15
dequeue(Q) f NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 f = f.next temp e

16
dequeue(Q) f NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 temp.next = null return temp temp null e

17
enqueue(Q, x) single pointer q NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null x

18
enqueue(Q, x)- sp q NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null x temp temp = q temp = temp.next until temp.next = null

19
enqueue(Q, x)- sp q NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null x temp

20
enqueue(Q, x)- sp q NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 x temp temp.next = x

21
enqueue(Q, x) f NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null xe

22
enqueue(Q, x) f NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 xe e.next = x

23
enqueue(Q, x) f NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 xe e = x

24
isEmptyQueue(Q) return f == null

25
Linked List Search head NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95 null NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: Grade: 95

26
Speed of List Search O(n) for when implemented as a linked-list O(lg n) possible if implemented as an array –How?

27
Speed of List Search 1

28
2

29
3

30
4

31
5

32
Speed of List Search -- Array It took 5 compares There were 29 elements lg(16) < lg(29) < lg(32) 4 < lg(29) < 5 What if there were 1,000,000 elements? How many compares would it take?

33
Speed of List Search -- Array lg( ) = 20 In 20 compares we can search a list of 1,000,000 elements. But we can’t always use an array to hold the data because we may not know the size limit of the database. How can we dynamically represent the data in such a way that we can still get searches in lg(n)?

34
Trees

35
Root Nodes Edges Leaves Height of a Tree Depth of a Node Parent / Child

36
Binary Tree

37
Binary Search Tree

38
Binary Search Tree Tree T is a binary search tree made up of n elements: x 0 x 1 x 2 x 3 … x n-1 Functions: createEmptyTree() returns a newly created empty binary tree delete(T, p) removes the node pointed to by p from the tree T insert(T, p) returns T with the node pointed to by p added in the proper location search(T, key) returns a pointer to the node in T that has a key that matched key returns null if key is not found traverse(T) prints the contents of T in order isEmptyTree(T) returns true if T is empty and false if it is not

39
Homework 4 Describe how to implement a binary search tree using a set of nodes (this tree will have no number of element limit). Do the six BST functions. Can you determine how to implement a binary search tree using an array (assume it will never have more than 100 elements)? Consider the six BST functions.

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google