JavaScript data structure - Collection

introduce

A collection is made up of a set of unordered and unique (that is, non repeatable) items. For example, a set of integers greater than or equal to 0: N={0,1,2,3,4,5,6,...}.

Another concept is called empty set. Denoted by '{}'.

Create collection

We use objects to represent collections.

1 function Set() { 
2     let items = {}; 
3 }

Common methods

add(value): adds a new entry to the collection.
delete(value): removes a value from the collection.
has(value): returns true if the value is in the collection, false otherwise.  clear(): removes all items from the collection.
size(): returns the number of elements contained in the collection. Similar to the length property of an array.
values(): returns an array containing all the values in the collection.

has method

1 this.has = function(value){ 
2     return value in items; 
3 };
4 
5 ----------------perhaps-----------------
6 
7 this.has = function(value){ 
8  return items.hasOwnProperty(value); 
9 };

add method

1 this.add = function (value) {
2     if (!this.has(value)) {
3         items[value] = value; 
4         return true;
5     }
6     return false;
7 };

remove method

 1 this.remove = function (value) {
 2     if (this.has(value)) {
 3         delete items[value];
 4         return true;
 5     }
 6     return false;
 7 };
 8 
 9 this.clear = function(){ 
10     items = {};
11 };

size method

1 this.size = function(){ 
2     return Object.keys(items).length;
3 };

values method

The values method also applies the same logic to extract all the attributes of the items object and return them as an array

1 this.values = function () {
2     let values = [];
3     for (let i = 0, keys = Object.keys(items); i < keys.length; i++) {
4         values.push(items[keys[i]]);
5     }
6     return values;
7 };

Using the set class

 1 let set = new Set(); 
 2 set.add(1); 
 3 console.log(set.values()); //output["1"] 
 4 console.log(set.has(1)); //output true 
 5 console.log(set.size()); //Output 1 
 6 set.add(2); 
 7 console.log(set.values()); //output["1", "2"] 
 8 console.log(set.has(2)); //true 
 9 console.log(set.size()); //2 
10 set.remove(1); 
11 console.log(set.values()); //output["2"] 
12 set.remove(2); 
13 console.log(set.values()); //output[]

Some operations of collection

It's also in high school math textbooks.

Union: for a given two sets, returns a new set containing all elements in both sets.
Intersection: for a given two sets, returns a new set containing elements in both sets.
Difference set: for a given two sets, returns a new set containing all elements that exist in the first set and do not exist in the second set.
Subset: verifies that a given set is a subset of another set.

Union

The mathematical concept of union is the union of set A and set B, which is expressed as A ∪ B. the definition of set is as follows:

A∪B = { x | x ∈ A∨x ∈ B }

 

The union method of code implementing Set class

 1 this.union = function (otherSet) {
 2     let unionSet = new Set(); //Define the union of two sets 
 3     let values = this.values();
 4     for (let i = 0; i < values.length; i++) { //Get the sets of the first collection and add them to the merge set
 5         unionSet.add(values[i]);
 6     }
 7     values = otherSet.values(); 
 8     for (let i = 0; i < values.length; i++) { //Get the sets of the second set and add them to the merge set
 9         unionSet.add(values[i]);
10     }
11     return unionSet;
12 };

Test code

 1 let setA = new Set(); 
 2 setA.add(1); 
 3 setA.add(2); 
 4 setA.add(3); 
 5 let setB = new Set(); 
 6 setB.add(3); 
 7 setB.add(4); 
 8 setB.add(5); 
 9 setB.add(6); 
10 let unionAB = setA.union(setB); 
11 console.log(unionAB.values());
12 
13 //result
14 ["1", "2", "3", "4", "5", "6"]

intersection

The mathematical concept of intersection is the intersection of set A and set B, which is expressed as A ∩ B. the definition of set is as follows:

A∩B = { x | x ∈ A∧x ∈ B }

           

 

The intersection method of code implementation Set class

 1 this.intersection = function (otherSet) {
 2     let intersectionSet = new Set(); //Create intersection to return common elements 
 3     let values = this.values();
 4     for (let i = 0; i < values.length; i++) { //Facilitate the current Set Class ownership
 5         if (otherSet.has(values[i])) { //Verify existence otherSet in
 6             intersectionSet.add(values[i]); //Add if existing
 7         }
 8     }
 9     return intersectionSet;
10 }

Test code

 1 let setA = new Set(); 
 2 setA.add(1); 
 3 setA.add(2); 
 4 setA.add(3); 
 5 let setB = new Set(); 
 6 setB.add(2); 
 7 setB.add(3); 
 8 setB.add(4); 
 9 let intersectionAB = setA.intersection(setB); 
10 console.log(intersectionAB.values());
11 
12 //result
13 ["2", "3"] 

Difference set

The mathematical concept of difference set is the difference set of set a and set B, expressed as: A-B, the definition of set is as follows:

A-B = { x | x ∈ A ∧ x B }

 

           

 

The difference method of code implementation Set class

 1 this.difference = function (otherSet) {
 2     let differenceSet = new Set(); //Create a set of subtractions 
 3     let values = this.values();
 4     for (let i = 0; i < values.length; i++) { //Traverse current Set Class all values
 5         if (!otherSet.has(values[i])) { //existence A But there is no relationship between B Value of
 6             differenceSet.add(values[i]); //Add to collection
 7         }
 8     }
 9     return differenceSet;
10 };

Test code

 1 let setA = new Set(); 
 2 setA.add(1); 
 3 setA.add(2); 
 4 setA.add(3); 
 5 let setB = new Set(); 
 6 setB.add(2); 
 7 setB.add(3); 
 8 setB.add(4); 
 9 let differenceAB = setA.difference(setB); 
10 console.log(differenceAB.values());
11 
12 //result
13 ["1"]

subset

The mathematical concept of subset is that set A is A subset of set B, which is expressed as A ⊆ B. the definition of set is as follows:

∀x { x ∈ A → x ∈ B }

 

           

 

Code implementation of subset method of Set class

 1 this.subset = function (otherSet) {
 2     if (this.size() > otherSet.size()) { //current set The size of the class must be greater than otherSet Instance size
 3         return false;
 4     } else {
 5         let values = this.values();
 6         for (let i = 0; i < values.length; i++) { //ergodic Set Class all elements
 7             if (!otherSet.has(values[i])) { //Verify that the element page exists with otherSet in
 8                 return false; //There is no such thing as false
 9             }
10         }
11         return true; //If all exist, then true
12     }
13 };

Test code

 1 let setA = new Set(); 
 2 setA.add(1);
 3 setA.add(2); 
 4 let setB = new Set(); 
 5 setB.add(1); 
 6 setB.add(2); 
 7 setB.add(3); 
 8 let setC = new Set(); 
 9 setC.add(2); 
10 setC.add(3); 
11 setC.add(4); 
12 console.log(setA.subset(setB)); 
13 console.log(setA.subset(setC));
14 
15 / / results
 16 setA is a subset of setB (so the output is true), but setA is not a subset of setC (setC only contains 2 in setA, not 1), so the output is false.

 

Source: JavaScript data structure and algorithm

Summary: along

 

Tags: Javascript

Posted on Mon, 11 May 2020 14:53:33 -0400 by biggus