3. STACK
➤ Definition: ordered collection of items where the addition of new
items and the removal of existing items always takes place at the
same end. This end is commonly referred to as the “top”. The end
opposite the top is known as the “base”.
4. ➤ Order principle: LIFO(last-in, first-out).
➤ The most recently added item is the one that is in position
to be removed first.
➤ Newer items are near the top, while older items are near
the base.
5. ➤ Stack operations:
➤ Stack(): creates a new stack that is empty. It needs no parameters and
returns an empty stack.
➤ push(item): adds a new item to the top of the stack. It needs the item
and returns nothing. O(1).
➤ pop(): removes the top item from the stack. It needs no parameters and
returns the item. The stack is modified. O(1).
➤ peak(): returns the top item from the stack but does not remove it. It
needs no parameters. The stack is not modified. O(1).
➤ isEmpty(): tests to see whether the stack is empty. It needs no
parameters and returns a boolean value. O(1).
➤ size(): returns the number of items on the stack. It needs no parameters
and returns an integer. O(1).
6. ➤ Not a built-in type in python. We need to define it by
ourselves.
7. QUEUE
➤ Definition: ordered collection of items where the addition of
new items happens at one end, called the “rear”, and the
removal of existing items occurs at the other end, commonly
called the “front”.
9. ➤ Queue operations:
➤ Queue(): creates a new queue that is empty. It needs no
parameters and returns an empty queue.
➤ enqueue(item): adds a new item to the rear of the queue. It needs
the item and returns nothing. O(1)
➤ dequeue(): removes the front item from the queue. It needs no
parameters and returns the item. The queue is modified. O(1)
➤ isEmpty(): tests to see whether the queue is empty. It needs no
parameters and returns a boolean value. O(1)
➤ size(): returns the number of items in the queue. It needs no
parameters and returns an integer. O(1)
10. ➤ Not a built-in type in python. We need to define it by
ourselves.
11. DEQUE
➤ Definition: ordered collection of items similar to the queue.
It has two ends, a front and a rear, and the items remain
positioned in the collection.
➤ Difference between queue: new items can be added at
either the front or the rear. Likewise, existing items can
be removed from either end.
➤ Order principle: does not require FIFO or LIFO.
12. ➤ Deque operations:
➤ Deque() creates a new deque that is empty. It needs no parameters and returns an empty
deque.
➤ addFront(item) adds a new item to the front of the deque. It needs the item and returns
nothing. O(1)
➤ addRear(item) adds a new item to the rear of the deque. It needs the item and returns
nothing. O(1)
➤ removeFront() removes the front item from the deque. It needs no parameters and
returns the item. The deque is modified. O(1)
➤ removeRear() removes the rear item from the deque. It needs no parameters and returns
the item. The deque is modified. O(1)
➤ isEmpty() tests to see whether the deque is empty. It needs no parameters and returns a
boolean value.
➤ size() returns the number of items in the deque. It needs no parameters and returns an
integer.
14. HASH TABLE
…….
[0] [1] [2] [3] [4] [700]
1. An array of records
2. Each record has a special field, called key.
In this example, the key is a long integer field
called number
number: 506643548 3. The number may be
a person’s
identification
number, and the rest
of the record has
information about
person
16. Number:
281942902
Number:
233667136
Number:
506643548 …….
Number:
155778322
[0] [1] [2] [3] [4] [700]
- In order to insert a new record, the key must
somehow be converted to an array index.
- The index is called the hash value of the key.
Number: 580625685- A typical way to create a hash value:
Number mod 701
-> 580625685 mod 701 = 3
19. HASH FUNCTION
➤ Good hash function:
➤ Return number 0, …, table size.
➤ Should be efficiently computable: O(1)
➤ Should not waste space.
➤ Should minimize collisions.
➤ The best hash function would distribute keys as evenly as possible in the hash table.
➤ Folding method:
➤ Mid square method:
20. COLLISION
➤ Collision: occurs when two different keys has to the same value.
➤ Cannot store both data records in the same slot in array.
➤ Collision resolution: a systematic method for placing the second
item in the hash table.
➤ Two methods:
➤ Separate Chaining: Use a dictionary data structure (such
as a linked list) to store multiple items that hash to the
same slot
➤ Closed Hashing (or probing/open addressing): search for
empty slots using a second function and store item in first
empty slot that is found. May go circularly to the start.
21. HASHING WITH SEPARATE CHAINING
• Put a little dictionary at
each entry
– common case is
unordered linked list
(chain)
22. CLOSED HASHING/ OPEN ADDRESSING
➤ Disadvantage:
➤ Clustering: If many collisions occur at the same hash value, a number
of surrounding slots will be filled by the linear probing resolution.
This will have impact on other items that are being inserted.
➤ Advance:
➤ Skip slots (rehashing): (pos + skip) % hashtablesize.
➤ Quadratic probing: Instead of using a constant “skip” value, we use a
rehash function that increments the hash value by 1, 3, 5, 7, 9, and so
on. In other words, quadratic probing uses a skip consisting of
successive perfect squares.