In: Computer Science
The course is Data Structures and am using Javascript and Atom... QUESTION
1. Implement the dictionary data structure using the prototype. Run some tests that show that your code works.
2. Implement the hash table data structure using the prototypeRun some tests that show that your code works.
3. The book discusses linear probing, but their approach has a serious problem. What is the issue?
4. Complete the method below that adds all key-value pairs from one dictionary into another. See also the example below.
Dictionary.prototype.merge = function(dict) {
// add all key-value pairs in 'dict' to 'this' };
// Test your code by uncommenting these lines:
//var dict1 = new Dictionary();
//dict1.set("key1", "val1");
//dict1.set("key2", "val2");
//dict1.set("key3", "val3");
//var dict2 = new Dictionary();
//dict2.set("key3", "val3b");
//dict2.set("key4", "val4");
//dict1.merge(dict2);
//dict1.print();
// should contain key1-val1, key2-val2, key3-val3b, key4-val4
1. Implement the dictionary data structure using the prototype:
Note: We can use javascript objects as a dictionary as follows:
Code:
<!DOCTYPE html>
<html>
<body>
<h3>1. Implement the dictionary data structure using the
prototype.</h3>
<p id="demo"></p>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
var D = {"a":"Element1", "b":"Element2",
3:"Element3"};
document.getElementById("demo").innerHTML =
"The value of the key a of dictionary D is: " +
D["a"];
document.getElementById("demo1").innerHTML =
"The value of the key b of dictionary D is: " +
D["b"];
document.getElementById("demo2").innerHTML =
"The value of the key 3 of dictionary D is: " + D[3];
</script>
</body>
</html>
Output Snapshot:
2. Implement the hash table data structure using the prototype:
Note: A Hash table is sometimes called as hash map is a data structure that maps keys to values.
Implementation of Hash table is as follows:
Code:
<!DOCTYPE html>
<html>
<body>
<h3>2. Implement the hash table data structure using the
prototype</h3>
<script>
var hash = new Object(); // or just {}
hash['one'] = 1;
hash['two'] = 2;
hash['three'] = 3;
// show the values stored
for (var i in hash) {
// use hasOwnProperty to filter out keys from the
Object.prototype
if (hash.hasOwnProperty(i)) {
alert('key is: ' + i + ', value is: ' + hash[i]);
}
}
</script>
</body>
</html>
Output:
Alert box will display:
key is: one, value is: 1
key is: two, value is: 2
key is: three, value is: 3
Code
Snapshots:
3. The book discusses linear probing, but their approach has a serious problem. What is the issue?:
Linear Probing is a topic in computer programming for the resolution of collisions purpose occurs in hash tables,for the maintainance of collection of key-value pairs and looking for the values associated to the keys.
In this method the new key is placed in the closest next empty cell. Locality of reference makes the linear probing faster.
It needs a five-way independence in the hash function this might causes a problem in linear probing.
Tendency for clusturing in the table is there because of this more number of collisions occurs at the similar hash value and the slots can be filled by linear probing resolution.
There might be an issue of clusturing in the Book. we can deal with the clusturing by extending the linear probing technique in this way we can skip slots instead of looking for sequential open slot. Therefore an even distribution of items can be done which were causing collisions so tat the clusturing will be reduced.
4. Complete the method below that adds all key-value pairs from one dictionary into another.:
Dictionary.prototype.merge = function(dict) {
var dict1 = new Dictionary();
dict1.set("key1", "val1");
dict1.set("key2", "val2");
dict1.set("key3", "val3");
var dict2 = new Dictionary();
dict2.set("key3", "val3b");
dict2.set("key4", "val4");
dict1.merge(dict2);
dict1.print();
};
I hope this will help, if not please comment below i will help
you!!
Please do not forget to Upvote the answer!!
Happy Learning!! :)