Professional Documents
Culture Documents
Week 6 - Hashing and Collision Resolution
Week 6 - Hashing and Collision Resolution
Week 6
Program Studi Teknik Informatika
Fakultas Teknik – Universitas Surabaya
Review Exercise Week 5
Exercise
3
Infix to Postfix
(2+3)*(4–5)
Iteration-1 Iteration-2
• ( is opening bracket (5th A Rule), • 2 is operand (1st Rule), then
then push to stack put to Postfix
• Postfix = • Postfix = 2
• Stack = ( (tos) • Stack = ( (tos)
4
Infix to Postfix
(2+3)*(4–5)
Iteration-3 Iteration-4
• + is operator and tos < + (3rd A Rule), • 3 is operand (1st Rule),
then push to stack then put to Postfix
• Postfix = 2 • Postfix = 2 3
• Stack = ( + (tos) • Stack = ( + (tos)
5
Infix to Postfix
(2+3)*(4–5)
Iteration-5 Iteration-6
• ) is closing bracket (5th B Rule) • * is operator and stack is
• Postfix = 2 3 + empty (2nd Rule), then push to
• Stack = stack
• Postfix = 2 3 +
• Stack = * (tos)
6
Infix to Postfix
(2+3)*(4–5)
Iteration-7 Iteration-8
• ( is opening bracket (5th A Rule), • 4 is operand (1st Rule),
then push to stack then put to Postfix
• Postfix = 2 3 + • Postfix = 2 3 + 4
• Stack = * ( (tos) • Stack = * ( (tos)
7
Infix to Postfix
(2+3)*(4–5)
Iteration-9 Iteration-10
• - is operator and tos < - (3rd A Rule), • 5 is operand (1st Rule),
then push to stack then put to Postfix
• Postfix = 2 3 + 4 • Postfix = 2 3 + 4 5
• Stack = * ( - (tos) • Stack = * ( - (tos)
8
Infix to Postfix
(2+3)*(4–5)
Postfix = 2 3 + 4 5 - *
9
Calculate postfix
23+45-*
Calculate Postfix
23+45-*
Iteration-1 Iteration-3
• 2 is operand, then Push to Stack • + is operator, then Pop 2
• Stack = 2(tos) operand from Stack
• Calculate : 2 + 3 = 5, Push
the result to Stack
Iteration-2
• Stack = 5(tos)
• 3 is operand, then Push to Stack
• Stack = 2 3(tos)
11
Calculate Postfix
23+45-*
Iteration-4 Iteration-6
• 4 is operand, then Push to Stack • - is operator, then Pop 2
• Stack = 5 4(tos) operand from Stack
• Calculate : 4 - 5 = -1, Push
the result to Stack
Iteration-5
• Stack = 5 -1(tos)
• 5 is operand, then Push to Stack
• Stack = 5 4 5(tos)
12
Calculate Postfix
23+45-*
Iteration-7
• * is operator, then Pop 2 operand from Stack
• Calculate : 5 * -1 = -5, Push the result to Stack
• Stack = -5(tos)
• Use the “circular” array concept to find the actual new index from
quadratic/linear probing (for example: idx from calculation: 102, actual: 2,
as long as index 2 is empty)
Hashing in .Net
• .Net framework has already implemented the concept of hashing and
provides the hash table through the Hashtable class.
• Adding data to the Hashtable, can be performed by Add method, that will
store the key and the item, which can be created from any data type
Example:
• The index that must be used inside this square bracket is the key of the
data.
• Remember, because all the data is stored in class object, then you must
perform explicit casting to the data retrieved.
Example:
string name = (string)students[“110214002”];
Get Data From Hashtable
• Before you get the data from Hashtable, it is advisable to check the
existence of the data by ContainsKey method.
Example:
• You can use this property to enumerate all the items in the array such as:
foreach (string key in students.Keys)
{
listBox1.Items.Add (“key = ” + key + “ Name = ” + students[key]);
}
Note: the order of the items retrieved may not be the same as the order of the
items in addition
QUESTIONS??