The Set of Learning Data Structure and Algorithms

Posted by Vidya_tr on Wed, 05 Jun 2019 23:40:49 +0200

Brief introduction to collections

I remember that the first lesson of Senior One Mathematics was collecting. Now I'm going to be a senior and see it again with the feeling of meeting old friends. Haha, no more gossip, get to the point.

A set is a data structure consisting of a set of disordered and non-repetitive items. Here the concept of set is similar to that of high school mathematics. There are also concepts of empty set, intersection, Union and subset, but there are no more complicated proof problems here. Then I'll implement the collection in JavaScript.

Implementing collections with JavaScript

The old rule is to get a constructor first.

function Set () {
  // Here we use objects to simulate collections
  var items = {}
}

Collection implements the following methods:

  • add(value): Add a new item to the collection

  • remove(value): Delete an item from a collection

  • has(value): Returns true if the item exists in the collection, or false.

  • clear(): Clears all items in the collection

  • size(): Returns the number of elements contained in the collection

  • values(): Returns an array of all values in the collection

Implementing has

Judge whether the element belongs to a collection based on hasOwnProperty. Note: hasOwnProperty can check whether the attribute belongs to an object, and hasOwnProperty returns false for the attribute on the prototype chain.

this.has = function (value) {
  return items.hasOwnProperty(value)
}

Implementing add

When adding a new element to a collection, the first thing to do is to ensure that the element does not exist in the collection.

this.add = function (value) {
  if (!this.has(value)) {
    items[value] = value
    return true
  }
  return false
}

Implementing remove

Before deleting, we should judge whether the element exists or not.

this.remove = function (value) {
  if (this.has(value)) {
    delete items[value]
    return true
  }
  return false
}

Implementing values

Object.keys returns an array of all enumerable attributes in the object (excluding attributes on the prototype chain). Of course, you can also use for...in.

this.values = function () {
  return Object.keys(items)
}

Implementing clear and size

Simple, just paste the source code directly.

this.clear = function () {
  items = {}
}

this.size = function () {
  return Object.keys(items).length
}

Implementing set operations

The collection has the following operations:

  • Union

  • intersection

  • Difference set

  • subset

Specific explanation is not much, see the code

Implementing Union

Create a new set unionSet to represent the union of two sets, then iterate through the two sets to add to the unionSet, and finally return to the set.

this.union = function (otherSet) {
  var unionSet = new Set()
  
  var values = this.values()
  for (var i = 0; i < values.length; i++) {
    unionSet.add(values[i])
  }
  
  values = otherSet.values()
  for (var i = 0; i < values.length; i++) {
    unionSet.add(values[i])
  }
  return unionSet
}

Achieve intersection

Intersection is a set of attributes that both have. The idea of implementation is similar to that above, as long as the element exists at the same time as the two sets.

this.intersection = function (otherSet) {
  var intersectionSet = new Set()

  var values = this.values()
  for (var i = 0; i < values.length; i++) {
    if (otherSet.has(values[i])) {
      intersectionSet.add(values[i])
    }
  }

  return intersectionSet
}

Implementing difference sets

The difference set A-B means that elements only exist in set A and set B does not exist, so the implementation is very simple.

this.difference = function (otherSet) {
  var differenceSet = new Set()
  
  var values = this.values()
  for (var i = 0; i < values.length; i++) {
    if (!otherSet.has(values[i])) {
      differenceSet.add(values[i])
    }
  }
  
  return differenceSet
}

Implementation subset

In mathematics, A is a subset of B, which means that all elements in A exist in B. According to this idea, the code is as follows.

this.subSet = function (otherSet) {
  if (this.size() > otherSet.size()) {
    return false
  } else {
    var values = this.values()

    for (var i = 0; i < values.length; i++) {
      if (!otherSet.has(values[i])) {
        return false
      }
    }
    return true
  }
}

Put in the source code, you can see if you are interested:

Implementation of Collection - Source Code

Summary

So far, the relatively simple data structure is basically mastered, followed by dictionaries, hash tables, trees, graphs and sorting algorithms, which are difficult to gnaw, but must be gnawed down. Continue refueling

Topics: Javascript Attribute