Data Structures Homework 9 Solution By Carlos Q

Data Structures Homework #9 Solution By Carlos

Q 1 exercise 18. 12 Pseudocode for add, remove and get. Item

The pseudocode -- add Add(item: Item. Type) : void throw hashtable. Full. Exception 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 origin. I as i add 1 to i
![The pseudocode -- add while(table[i] is occupied and i not equals origin. I){ if(table[i] The pseudocode -- add while(table[i] is occupied and i not equals origin. I){ if(table[i]](http://slidetodoc.com/presentation_image_h2/7fe4c7c52f437b5a6acab980280f5cce/image-4.jpg)
The pseudocode -- add while(table[i] is occupied and i not equals origin. I){ 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 hashtable. Full. Exception }

The pseudocode – remove(1) remove(search. Key: key. Type) : boolean i = the array index that the address calculator gives you for an item whose search key equals search. Key if(table[i]. get. Key() == searchkey){ remove the item from table[i] return true } else { set origin. I as i add 1 to i
![The pseudocode – remove(2) while(table[i] is occupied and i not equals origin. I){ if(table[i]. The pseudocode – remove(2) while(table[i] is occupied and i not equals origin. I){ if(table[i].](http://slidetodoc.com/presentation_image_h2/7fe4c7c52f437b5a6acab980280f5cce/image-6.jpg)
The pseudocode – remove(2) while(table[i] is occupied and i not equals origin. I){ if(table[i]. get. Key() == 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 – get. Item(1) get. Item(search. Key: key. Type) : Item. Type throw Not. Found. Exception i = the array index that the address calculator gives you for an item whose search key equals search. Key if(table[i]. get. Key() == searchkey) return table[i] else { set origin. I as i add 1 to i
![The pseudocode – get. Item(2) while(table[i] is occupied and i not equals origin. I){ The pseudocode – get. Item(2) while(table[i] is occupied and i not equals origin. I){](http://slidetodoc.com/presentation_image_h2/7fe4c7c52f437b5a6acab980280f5cce/image-8.jpg)
The pseudocode – get. Item(2) while(table[i] is occupied and i not equals origin. I){ if(table[i]. get. Key() == searchkey) return table[i] add 1 to i if(i reaches the size of table) set i as 0 } return Not. Found. Exception }

Q 2 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.

Q 3 exercise 19. 1 Draw a 2 -3 tree

30 10 10 10 30 100 80 30 10 50 10 80 100

50 80 30 50 100 50 30 100 60 80 50 30 40 80 60 70 80 100 30 60 70 100

50 30 40 70 60 50 100 30 40 70 60 90 100 50 70 30 20 40 60 90 100

50 20 40 60 50 20 40 70 90 100 90 60 100

Q 4 exercise 19. 3 Range query of 2 -3 tree

The pseudocode(1) range. Query(23 tree: Two. Tree, lb: Item. Type, ub: Item. Type) 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 range. Query(leftsubtree or r, lb, ub) range. Query(rightsubtree or r, lb, ub) if(item is larger than ub) range. Query(leftsubtree or r, lb, ub) if(item is smaller than lb) range. Query(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) range. Query(leftsubtree or r, lb, ub) visit the left item range. Query(middlesubtree or r, lb, ub) if(the left item is smaller than lb and the right on is bwtewwn lb and ub) range. Query(middlesubtree or r, lb, ub) visit the right item range. Query(rightsubtree or r, lb, ub) }

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

Q 5 exercise 19. 10 2 -3 -4 tree represented by red-black tree

37 30 10 20 39 35 32 33 34 50 36 38 70 40 45 60 65 80 85 87 89 90 100

The end~
- Slides: 24