1 / 24

Data Structures

Data Structures. Homework #9 Solution By Carlos. Q1 exercise 18.12. Pseudocode for add, remove and getItem. The pseudocode -- add. Add( item :ItemType ) : void throw hashtableFullException i = the array index that the address calculator gives you by given item if(table[ i ] is empty){

djosephs
Télécharger la présentation

Data Structures

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. Data Structures Homework #9 Solution By Carlos

  2. Q1 exercise 18.12 Pseudocode for add, remove and getItem

  3. The pseudocode -- add Add(item:ItemType) : void throw hashtableFullException i = the array index that the address calculator gives you by given item if(table[i] is empty){ put item into table[i] return } else { set originIas i add 1 to i

  4. The pseudocode -- add while(table[i] is occupied and i not equals originI){ if(table[i] is empty){ put item into table[i] return } add 1 to i if(i reaches the size of table) set i as 0 } throw hashtableFullException }

  5. The pseudocode – remove(1) remove(searchKey:keyType) : boolean i = the array index that the address calculator gives you for an item whose search key equals searchKey if(table[i].getKey() == searchkey){ remove the item from table[i] return true } else { set originIas i add 1 to i

  6. The pseudocode – remove(2) while(table[i] is occupied and i not equals originI){ if(table[i].getKey() == searchkey){ remove the item from table[i] return true } add 1 to i if(i reaches the size of table) set i as 0 } return false }

  7. The pseudocode – getItem(1) getItem(searchKey:keyType) : ItemType throw NotFoundException i = the array index that the address calculator gives you for an item whose search key equals searchKey if(table[i].getKey() == searchkey) return table[i] else { set originIas i add 1 to i

  8. The pseudocode – getItem(2) while(table[i] is occupied and i not equals originI){ if(table[i].getKey() == searchkey) return table[i] add 1 to i if(i reaches the size of table) set i as 0 } return NotFoundException }

  9. Q2 exercise 18.14 Comments for the hash function

  10. This function is easy to compute but the distribution will not be evenly distribution. The probability of locating around medium is larger to others. If tow keys’ sums of positions in alphabet of its letters are the same, they will be hashed to the same location. Example : same letters but different order(anagram).

  11. This hash function is easy to compute. However, the distribution will have longer tail of its right side than its left side. There is no way to find a pattern which can be located to the same location by this hash function .

  12. Q3 exercise 19.1 Draw a 2-3 tree

  13. 30 10100 10 100 10 3080 30 10 50 100 10 80100

  14. 5080 80 3050 100 100 30 60 5080 5080 100 3040 6070 30 100 6070

  15. 5070 5070 3040 60 100 3040 60 90100 50 70 30 20 60 90100 40

  16. 5070 2040 60 90100 5090 2040 60 100

  17. Q4 exercise 19.3 Range query of 2-3 tree

  18. The pseudocode(1) rangeQuery(23tree:TwoTreeTree, lb:ItemType, ub:ItemType) Let r be the root node of 23 tree if(r equals null) return

  19. The pseudocode(2) if(r has one item){ if(item is between lb and ub) visit item rangeQuery(leftsubtree or r, lb, ub) rangeQuery(rightsubtree or r, lb, ub) if(item is larger than ub) rangeQuery(leftsubtree or r, lb, ub) if(item is smaller than lb) rangeQuery(rightsubtree or r, lb, ub) }

  20. The pseudocode(3) if(r has two items){ if(the left item is between lb and ub and the right on is larger than ub) rangeQuery(leftsubtree or r, lb, ub) visit the left item rangeQuery(middlesubtree or r, lb, ub) if(the left item is smaller than lb and the right on is bwtewwn lb and ub) rangeQuery(middlesubtree or r, lb, ub) visit the right item rangeQuery(rightsubtree or r, lb, ub) }

  21. The pseudocode(4) if(two items are both between the lb and ub) rangeQuery(leftsubtree or r, lb, ub) visit the left item rangeQuery(middlesubtree or r, lb, ub) visit the right item rangeQuery(leftsubtree or r, lb, ub) if(two items are both larger than ub) rangeQuery(leftsubtree or r, lb, ub) if(two items are both smaller than lb) rangeQuery(rightsubtree or r, lb, ub) }

  22. Q5 exercise 19.10 2-3-4 tree represented by red-black tree

  23. 3750 39 30 35 70 8590 87 89 40 45 100 32 3334 60 65 36 80 38 10 20

  24. The end~

More Related