CS1102 Tut 8 AVL Trees and Hashing - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

CS1102 Tut 8 AVL Trees and Hashing

Description:

h(key) = (sum of positions in alphabet of key's letters) mod 2048 ... h(key) = (position in alphabet of the first letter of key) mod 2048 ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 29
Provided by: soc128
Category:
Tags: avl | alphabet | cs1102 | hashing | letters | of | trees | tut

less

Transcript and Presenter's Notes

Title: CS1102 Tut 8 AVL Trees and Hashing


1
CS1102 Tut 8 AVL Trees and Hashing
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • S15-03-07 Tel65164364http//www.comp.nus.edu.sg
    /tanhuiyi

2
Groups assignment
  • Group 1 - Question 4Group 2 - Question 1Group 3
    - Question 2Group 4 - Question 3

3
First 15 minutes
  • Any questions?
  • Mock PE exam treat it as a practice!
  • Unix Tutorial
  • Pico editor
  • Compiling in Unix
  • Executing in Unix

4
First 15 minutes
  • Hashtable
  • Linear Probing
  • Quadratic Probing
  • Separate Chaining

5
Question 1
  • Write an AVL ADT
  • What are the public and private methods you need
    ?
  • Public operations Add, Delete, Get, Size
  • What do you need to do after you add?
  • Balance
  • When balancing, you need to rotateLeft and
    rotateRight
  • To balance, you need to know the height of the
    left and right subtrees

6
Question 1
  • Algorithm for Balance, consider the cases
  • Case1 Left.height gt right.height

7
Question 1
  • Algorithm for Balance, consider the cases
  • Case2 Right.height gt left.height

8
Question 1
  • So, break it up into two cases and consider them
    seperately
  • Write a rotateLeft and a rotateRight method to
    further simplify the problem!

9
Question 1
  • In this case we simply rotate right about x
  • rotateRight(x)

10
Question 1
  • In this case just rotating right wont solve the
    problem! (why? See next slide)
  • rotateLeft(y)
  • rotateRight(x)

11
Question 1
12
Question 1
  • Simply rotate about x
  • rotateLeft(x)

13
Question 1
  • Again simply rotating about x wont solve this
  • rotateRight(z)
  • rotateLeft(x)

14
Question 1
  • After you break it up its easy!
  • Balance(Treenode T)if(leftchild.height gt
    rightchild.height)
  • if(leftchild.leftHeight gt leftChild.rightHeight)
  • //Case 1a (insert outer)
  • rotateRight(t)
  • else
  • //Case 1b (insert inner)
  • rotateLeft(leftChild)
  • rotateRight(t)

15
Question 1
  • else //if(rightchild.height gt
    leftchild.height)
  • if(rightchild.rightHeight gt rightchild.leftHeigh
    t)
  • //Case 1a (insert outer)
  • rotateLeft(t)
  • else
  • //Case 2a
  • rotateRight(rightChild)
  • rotateLeft(t)

16
Question 1
  • What is the worst case?

17
Question 1
  • Have to balance for each level!
  • Question What is the maximum number of balance
    operations for an ADD operation?

18
Question 2
  • Good Hash function easy to compute
  • Evenly distribute data

19
Question 2
  • (a) The hash table has size 2048. The search keys
    are English words.The hash function is h(key)
    (sum of positions in alphabet of keys letters)
    mod 2048
  • In your lecture notes, Imagine a word is of
    length 10 (average), this hash function will
    generate values between (x 0) (x 260)
    where x is the starting index of the letter a
  • All words will be hashed to that 260 locations,
    and not make use of the remaining 1700 locations!
  • Also words like not and ton hash to the same
    location
  • To improve, add weights to each letter!

20
Question 2
  • (b) The hash table has size 2048. The keys are
    strings that begin with a letter.The function
    is h(key) (position in alphabet of the first
    letter of key) mod 2048
  • Even worst, all keys get hashed to (x0)
    (x26)th location in the array!
  • Lots of collisions!
  • Imagine if we try to hash the IC number!
    (Singaporean IC number starts with S)

21
Question 2
  • (c) The hash table is 10000 entries long. The
    search keys are integers in the range 0 to 9999.
    The hash function is
  • h(key) (key random) truncated to an
    integer, where random is a number generated
    randomly from 0 to 1
  • This hash function wont even work at all. This is
    because the random function will generate a
    different number each time it is called!
  • This means you wont be able to recover the items
    you have inserted into the hashtable!

22
Question 2
  • (d) The hash table is 10000 entries long. The
    search keys are integers in the range 0 through
    9999. The hash function is given by the following
    Java method
  • This hash function needs 1million operations to
    compute!
  • It may be even, but it is too slow!

23
Question 3
  • Inserting 10,22,31,4,15,28,17,88,59 into a table
    using linear probing

10
22
31
4
28
17
88
59
d
24
Question 3
  • Inserting 10,22,31,4,15,28,17,88,59 into a table
    using quadratic probing

10
22
31
4
28
17
88
59
d
15
25
Question 4
  • A perfect hash function is one that has a
    one-to-one mapping between keys and hash values.

26
Question 4
  • Suppose h3(key) (3key mod 11) 1 and the table
    size is 10. The keys are integers from 0 to 9
    inclusive. Is h3 a perfect hash function? Why?
  • No! h(0) h(5)!

27
Question 4
  • Suppose h2(key) (2key mod 11) 1 and the table
    size is 10. The keys are integers from 0 to 9
    inclusive. Is h2 a perfect hash function? Why?
  • Yes, because the hash function generates a unique
    number for each key. Try it out exhaustively

28
Question 4
  • 20 1 h(x) 0
  • 21 2 h(x) 1
  • 22 4 h(x) 3
  • 23 8 h(x) 7
  • 24 16 h(x) 4
  • 25 32 h(x) 9
  • 26 64 h(x) 8
  • 27 128 h(x) 6
  • 28 256 h(x) 2
  • 29 512 h(x) 5
Write a Comment
User Comments (0)
About PowerShow.com