js 实现数据结构 -- 集合(MySet)

原文:

   Javascript 中学习数据结构与算法。

 

概念:

  即数学中的集合,在计算机科学中被应用成数据结构。

  当然,集合中的数据具有不重复的特性。js 集合的原理大致上是 Object 的键值对 key: value 的形式,细微的不同是集合应用的是 value: value 的形式(即 key === value),并且 ES6 中 Set 中的 key 不再被限制(或隐式转换成)字符串。

 

基础集合:

class MySet {
    constructor() {
        this.items = {};
    }
    
    has(value) {
        return this.items.hasOwnProperty(value);
    }

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

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

    get size() {
        return Object.keys(this.items).length;
    }

    get values() {
        return Object.keys(this.items);
    }

    // 并集
    union(otherSet) {
        const unionSet = new MySet();
        this.values.forEach((val, index) => {
            // unionSet.add(val)
            unionSet.add(this.values[i]);
        })
        otherSet.values.forEach((val, index) => {
            // unionSet.add(val)
            unionSet.add(this.values[i]);
        })
        return unionSet;
    }

    // 交集
    intersection(otherSet) {
        const intersectionSet = new MySet();
        this.values.forEach((val, index) => {
            if (otherSet.has(val)) {
                intersectionSet.add(val);
            }
        })
        return intersectionSet;
    }

    // 差集 数学概念:集合A和B的差集,表示为A-B,定义如下:A-B = { x | x∈A ∧ x∉B },意思是x(元素)存在于A中,且不x存在于B中
    difference(otherSet) {
        const differenceSet = new MySet();
        this.values.forEach((val, index) => {
            if (!otherSet.has(val)) {
                differenceSet.add(val);
            }
        })
        return differenceSet;
    }

    // 是否子集
    subset(otherSet) {
        if (this.size > otherSet.size) {
            return false;
        } else {
            return !this.values.some(val => !otherSet.has(val))
        }
    }

}

   

  上例中最后四个方法是拓展,分别求并集、交集、差集、子集。其定义即示例图如下:

并集:对于给定的两个集合,返回一个包含两个集合中所有元素的新集合。

‰交集:对于给定的两个集合,返回一个包含两个集合中Р有元素的新集合。‰

差集:对于给定的两个集合,返回一个包含所有存在于第一个集合且不存在于第二个集合的元素的新集合。‰

子集:求证一个给定集合是否是另一集合的子集。

 

  1.并集:集合A和B的并集,表示为A∪B,定义如下:A∪B = { x | x∈A ∨ x∈B },意思是x(元素)存在于A中,或x存在于B中。如图:

  函数如下:

    union(otherSet) {
        const unionSet = new MySet();
        this.values.forEach((val, index) => {
            // unionSet.add(val)
            unionSet.add(this.values[i]);
        })
        otherSet.values.forEach((val, index) => {
            // unionSet.add(val)
            unionSet.add(this.values[i]);
        })
        return unionSet;
    }

 

  2.交集:集合A和B的交集,表示为A∩B,定义如下:A∩B = { x | x∈A ∧ x∈B },意思是x(元素)存在于A中,且x存在于B中。

  函数如下:

    intersection(otherSet) {
        const intersectionSet = new MySet();
        this.values.forEach((val, index) => {
            if (otherSet.has(val)) {
                intersectionSet.add(val);
            }
        })
        return intersectionSet;
    }

 

  3.差集:集合A和B的差集,表示为A-B,定义如下:A-B = { x | x∈A ∧ x∉B },意思是x(元素)存在于A中,且不x存在于B中。如图:

 

  函数如下:

    difference(otherSet) {
        const differenceSet = new MySet();
        this.values.forEach((val, index) => {
            if (!otherSet.has(val)) {
                differenceSet.add(val);
            }
        })
        return differenceSet;
    }

 

  4.子集:集合A是B的子集,或者说集合B包含了集合A,如图: 

 

  函数如下:

    subset(otherSet) {
        if (this.size > otherSet.size) {
            return false;
        } else {
            return !this.values.some(val => !otherSet.has(val))
        }
    }

 

posted @ 2019-03-26 13:32  shiweiqianju  阅读(652)  评论(0编辑  收藏  举报