Data Structures Chapter 4

Ace your homework & exams now with Quizwiz!

Queue is ______ in _______ out

First in, First out

Which item does the queue allow access?

First item that was inserted

Peek() Method

Simply returns the value at top, without changing the stack

True or False: The contents of a queue can wrap around, while those of a stack cannot?

True

Suppose you based a priority queue class on the OrdArray class in the orderedArray.java program (Listing 2.4) in Chapter 2, "Arrays." This will buy you binary search capability. If you wanted the best performance for your priority queue, would you need to modify the OrdArray class?

Yes, you would need a method to find the minimum value

Suppose you insert 15, 25, 35, and 45 into a queue. Then you remove three items. Which one is left?

45

True or False: A stack or a queue often serves as the underlying mechanism on which an ADT array is based?

False; Its the other way around

True or False: Pushing and popping items on a stack and inserting and removing items in a queue all take O(N) time?

False; They take O(1) time

Priority Queue Efficiency

Insertion takes O(n) Deletion takes O(1)

Deleting in a Priority Queue

Is always at the top

Stack Size

Typically small

Error Handling

user class: if( !theStack.isFull() ) insert(item); else System.out.print("Can't insert, stack is full");

Most important priority queue operations?

- Inserting an item in sorted order - Removing the item with the smallest key

A priority queue might be used to hold what?

Passengers to be picked up by a taxi from different parts of the city

Which item does the priority queue allow access?

Smallest (or sometimes the largest) item

Which data structures are usually used to simplify certain programming operations?

Stacks, queues, and priority queues

A stack allows access to only one data item:

The last item inserted

The minimum key item is always at the:

Top (highest index)

True or False: At least one of the methods in the priorityQ.java program (Listing 4.6) uses a linear search?

True

Prefix

When the operator comes before the operands (+AB)

Algorithms Parsing Steps

1. Transform the arithmetic expression into a different format, called postfix notation 2. Evaluate the postfix expression

Most important queue operations?

- Inserting an item at the rear of the queue - Removing the item from the front of queue

Most important stack operations?

- Pushing (inserting) an item onto the top of the stack - Popping (removing) the item that's on top

Inserting into a Priority Queue

- Type new item into the Number Field - Insert somewhere in the middle of values already in queue

Methods of a Dequeue?

- insertLeft() - insertRight() - removeLeft() - removeRight()

A queue might be used to hold...?

...keystrokes made by a computer user writing a letter.

Assume an array is numbered with index 0 on the left. A queue representing a line of movie-goers, with the first to arrive numbered 1, has the ticket window on the right. Then...

...the array index numbers and the movie-goer numbers increase in opposite left-right directions.

Largest item is always at index ___?

0

Suppose you push 10, 20, 30, and 40 onto the stack. Then you pop three items. Which one is left on the stack?

10

What is the circular queue based on?

Based on an array in which the indices wrap around from the end of the array to the beginning

What is a Dequeue?

A double-ended queue. You can insert items at either end and delete them from either end

Infix Examples

A+B-C A*B/C A*(B+C) A+B*(C-D/(E+F))

Postfix Examples

AB+C- AB*C/ ABC+* ABCDEF+/-*+

Non Restricted Access

Any item can be accessed, either immediately—if its index number is known—or by searching through a sequence of cells until it's found

With what can these data structures be implemented?

Arrays or other mechanisms such as Linked Lists

Circular Queue

Front and Rear arrows wrap around to the beginning of the array. - Insert enough items to bring the Rear arrow to the top of the array - Remove some items from the front of the array - Now insert another item - You'll see the Rear arrow wrap around from index 9 to index 0; the new item will be inserted there - Insert a few more items. The Rear arrow moves upward as you'd expect. Notice that after Rear has wrapped around, it's now below Front, the reverse of the original arrangement - Delete enough items so that the Front arrow also wraps around. Now you're back to the original arrangement, with Front below Rear

Priority Queues

Has a front and a rear, and items are removed from the front. However, in a priority queue, items are ordered by key value so that the item with the lowest key (or in some implementations the highest key) is always at the front. Items are inserted in the proper position to maintain the order.

Push() Method

Increments top so it points to the space just above the previous top and stores a data item there. Notice again that top is incremented before the item is inserted.

As other items are inserted and removed, does a particular item in a queue move along the array from lower to higher indices, or higher to lower?

It doesn't move at all

Stack is ______ in _______ out

Last in, First out

Which item does the stack allow access?

Last item inserted

What do LIFO and FIFO mean?

Last-In-First-Out First-In-First-Out

Queue efficiency?

O(1)

Stack efficiency?

O(1) time. That is, the time is not dependent on how many items are in the stack and is therefore very quick. No comparisons or moves are necessary

Inserting an item into a typical priority queue takes what big O time?

O(N)

How many data items can be accessed in these data structures?

One (1)

Restricted Access

Only one item can be read or removed at a given time (unless you cheat). The interface of these structures is designed to enforce this restricted access. Access to other items is (in theory) not allowed

How to Evaluate Postfix Expression

Operand - Push onto the stack Operator - Pop the top two operands from the stack and apply the operator to them. Push the result Done? - Pop the stack to obtain answer

Pop() Method

Removes the item from the stack; it's inaccessible, although the value remains in the array (until another item is pushed into the cell).

One difference between a priority queue and an ordered array is that?

The array must be ordered while the priority queue need not be

What are delimiters?

The braces { and }, brackets [ and ], and parentheses ( and ). Each opening or left delimiter should be matched by a closing or right delimiter; that is, every { should be followed by a matching } and so on

If you remove the last item in a stack, what will you access next?

The next-to-last item inserted, and so on

The term priority in a priority queue means that?

The underlying array is sorted by the priority of the items

How are stacks, queues, and priority queues primarily defined by their what?

Their interface: the permissible operations that can be carried out on them. The underlying mechanism used to implement them is typically not visible to their user


Related study sets

Life And Health Insurance State Exam

View Set

bipedalism and traits of early hominins

View Set

Commercial Underwriting Principles Final Exam

View Set

Macroeconomics Chapter 12 questions/review #2

View Set

MGMT 4389 exam 1 (dif sets according to quizzes for exam 1 pt 1)

View Set

Concept Quiz: The One-Sample t-Test

View Set