Question

In: Computer Science

The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary...

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

Solutions

Expert Solution

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!! :)


Related Solutions

Implement a dictionary application using C++ with the following features: Load a dictionary file. Given a...
Implement a dictionary application using C++ with the following features: Load a dictionary file. Given a prefix string that the user specifies, print the first word or all words in the dictionary with that string as their prefix. Given two strings A and B that the user specifies, replace all occurrences of A in the dictionary file with B. Spawning a new editor (e.g., vim) to allow the user to modify the dictionary file. Save the dictionary file afterwards. You...
Implement a dictionary application using C++ with the following features: Load a dictionary file. Given a...
Implement a dictionary application using C++ with the following features: Load a dictionary file. Given a prefix string that the user specifies, print the first word or all words in the dictionary with that string as their prefix. Given two strings A and B that the user specifies, replace all occurrences of A in the dictionary file with B. Spawning a new editor (e.g., vim) to allow the user to modify the dictionary file. Save the dictionary file afterwards. All...
Data Structures and Algorithms Activity Requirement: Implement a queue using an array as its underline data...
Data Structures and Algorithms Activity Requirement: Implement a queue using an array as its underline data structure. Your queue should fully implemnted the following methods: first, push_back (enqueue), pop_front (dequeue), size, and isEmpty. Make sure to include a driver to test your newly implemented queue
Linked List Course Outcome: CLO3    Build computer programs to implement appropriate data structures for solving computing...
Linked List Course Outcome: CLO3    Build computer programs to implement appropriate data structures for solving computing problems in a time frame given. (P3, PLO3) Task Write a complete program by applying linked list data structure that will perform the following operation on any records: Add record Delete record View record Find record Your program also should be able to display the number of record in the linked list. Put your creativity to produce the program. Your Submission should have the...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
This question is related to graph representation in Data Structure and Algorithm in Javascript. Question 1-...
This question is related to graph representation in Data Structure and Algorithm in Javascript. Question 1- Write a code in javascript in which you have to implement a function that converts adjacency matrix to adjacency list-store and it should have following signature function convertToAdjList( adjMatrix); You also have to include the functions that can demonstrate your implementation work with few inputs. Submit your javascript code with output as well. Question 2 Tell the runtime complexity of the conversion that implemented....
This all has to be in javascript Use the following variables and data structures to answer...
This all has to be in javascript Use the following variables and data structures to answer this question. var queue = new Queue(); var stack = new Stack(); 1. Enqueue the numbers 1, 2, 3, 4, and 5 in the queue. Then print the contents of the queue to the console. Note: queue and stack both have a print() method. 2. Dequeue each number from the queue and push it onto the stack. Then print the contents of the stack...
Pick a favorite JavaScript topic you learned from this course and present a lesson using a...
Pick a favorite JavaScript topic you learned from this course and present a lesson using a page or series of web pages that fully teaches someone who may be interested in learning about that topic, and create a mini website for your tutorial. You must make use of JavaScript code snippets to provide valid examples of proper syntax. Code examples with errors in them will not earn full credit. For your code snippets make use of this JavaScript-based syntax highlighter...
I am using a course that requires me to use excel.I am able to figure everything...
I am using a course that requires me to use excel.I am able to figure everything out except the test statistic A marine biologist claims that the mean length of mature female pink seaperch is different in fall and winter. A sample of 15 mature female pink seaperch collected in fall has a mean length of 108 millimeters and a standard deviation of 15 millimeters. A sample of 8 mature female pink seaperch collected in winter has a mean length...
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting...
I am taking a Data Structures and Program Design class. BUT, I am having trouble getting access to the vectors so that I can compare the guesses with the random numbers. I’ve been working on this for several days and don't feel any closer to a solution and could really use some help as I really don't know how to get this to work. Thank you! The assignment is: Design an ADT for a one-person guessing game that chooses 4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT