240 likes | 254 Views
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){
E N D
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){ put item into table[i] return } else { set originIas i add 1 to i
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 }
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
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 }
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
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 }
Q2 exercise 18.14 Comments for the hash function
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).
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 .
Q3 exercise 19.1 Draw a 2-3 tree
30 10100 10 100 10 3080 30 10 50 100 10 80100
5080 80 3050 100 100 30 60 5080 5080 100 3040 6070 30 100 6070
5070 5070 3040 60 100 3040 60 90100 50 70 30 20 60 90100 40
5070 2040 60 90100 5090 2040 60 100
Q4 exercise 19.3 Range query of 2-3 tree
The pseudocode(1) rangeQuery(23tree:TwoTreeTree, lb:ItemType, ub:ItemType) Let r be the root node of 23 tree if(r equals null) return
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) }
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) }
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) }
Q5 exercise 19.10 2-3-4 tree represented by red-black tree
3750 39 30 35 70 8590 87 89 40 45 100 32 3334 60 65 36 80 38 10 20