1 / 39

Queues

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

heidip
Télécharger la présentation

Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Queues Queue Q = x0 x1 x2 x3 … xn-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: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 0 1 2 3 4 5 6 int front = the front of the queue int n = the number of elements in the queue in this case front = 1 n = 4

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

  5. front(Q) return q[front]

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

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

  8. isEmptyQueue(Q) return n == 0

  9. NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 Queue – Linked Nodes q null

  10. NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 Queue – Linked Nodes f e null

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

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

  13. NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 dequeue(Q) f e null

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

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

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

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

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

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

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

  21. NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 enqueue(Q, x) f e x null null

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

  23. NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 enqueue(Q, x) f e x e = x null

  24. isEmptyQueue(Q) return f == null

  25. NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 NameLast: Smart FirstName: Joe StudentNumber: 8 SSN: 123-34-1112 Grade: 95 Linked List Search head null

  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. Speed of List Search 2

  29. Speed of List Search 3

  30. Speed of List Search 4

  31. Speed of List Search 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(1000000) = 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. Trees • Root • Nodes • Edges • Leaves • Parent / Child • Depth of a Node • Height of a Tree

  36. Binary Tree

  37. Binary Search Tree 54 21 72 5 30 60 84 10 25 35 79 86 44 93

  38. Binary Search Tree Tree T is a binary search tree made up of n elements: x0 x1 x2 x3 … xn-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.

More Related