JS数据结构4:集合

概念

  • 集合是由一组无序且唯一(即不能重复)的项组成的
  • 该数据结构使用了与有限集合相同的数学概念,但应用在计算机科学的数据结构中
  • 集合的存储是值 和 值对应

集合方法

  • add(element):向集合添加一个新元素。
  • delete(element):从集合移除一个元素。
  • has(element):如果元素在集合中,返回true,否则返回false。
  • clear():移除集合中的所有元素。
  • size():返回集合所包含元素的数量。它与数组的length 属性类似。
  • values():返回一个包含集合中所有值(元素)的数组。

实现

构造存储结构:对象{value: value} 值 和 值

class set {
    constructor() {
        this.items = {};
    }
}

判断元素唯一性的方法

has(ele) {
    return Object.prototype.hasOwnProperty.call(this.items, ele);
}

各种方法的实现

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

    delete(ele) {
        if (this.has(ele)) {
            delete this.items[ele];
            return true;
        }
        return false;
    }
    clear() {
        this.items = {};
    }
    size() {
        // return Object.keys(this.items).length    // es6写法
        let cnt = 0;
        for (let key in this.items) {
            if (this.has(key)) {
                cnt++;
            }
        }
        return cnt;
    }
    values() {
        let values = [];
        for (let key in this.items) {
            if (this.has(key)) {
                values.push(this.items[key]);
            }
        }
        return values;
    }

交集,并集,差集,子集

  • 并集:对于给定的两个集合,返回一个包含两个集合中所有元素的新集合。
  • 交集:对于给定的两个集合,返回一个包含两个集合中共有元素的新集合。
  • 差集:对于给定的两个集合,返回一个包含所有存在于第一个集合且不存在于第二个集合的元素的新集合。
  • 子集:验证一个给定集合是否是另一集合的子集。

并集运算

    union(otherSet) {
        let unionSet = new MySet();   // 存储最终的并集集合数据
        this.values().forEach(value => {
            unionSet.add(value);
        });
        otherSet.values().forEach(value => {
            unionSet.add(value);
        });
        return unionSet;
    }

交集运算

    intersection(otherSet) {
        let intersection = new MySet();
        let values = this.values(); // 预存当前集合值数组
        // 判断哪个大哪个小
        let otherValues = otherSet.values();
        let bigSet = undefined;
        let smallSet = undefined;
        if (values.length > otherValues.length) {
            bigSet = values;
            smallSet = otherValues;
        } else {
            bigSet = otherValues;
            smallSet = values;
        }
        for (let i = 0; i < smallSet.length; i++) {
            // 直接判断可能导致性能损耗,应该判断小的那个集合
            if (bigSet.has(smallSet[i])) {
                intersection.add(smallSet[i]);
            }
        }
    }

差集运算

在这里插入图片描述

    difference(otherSet) {
        // 需要深拷贝
        let differenceSet = this;
        let intersection = this.intersection(otherSet);
        // 删除交集元素
        intersection.values().forEach(value => {
            differenceSet.delete(value);
        });
        return differenceSet;
    }

子集运算

在这里插入图片描述

    isSubsetOf(otherSet) {
        if (this.size() > otherSet.size()) {
            return false;
        }
        let isSubset = true;
        this.values().every(value => {
            if (!otherSet.has(value)) {
                isSubset = false;
                return false;
            }
            return true;
        });
        return isSubset;
    }

完整代码

class MySet {
    constructor() {
        this.items = {};
    }
    // 判断元素的唯一性
    has(ele) {
        return Object.prototype.hasOwnProperty.call(this.items, ele);
    }

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

    delete(ele) {
        if (this.has(ele)) {
            delete this.items[ele];
            return true;
        }
        return false;
    }
    clear() {
        this.items = {};
    }
    size() {
        // return Object.keys(this.items).length    // es6写法
        let cnt = 0;
        for (let key in this.items) {
            if (this.has(key)) {
                cnt++;
            }
        }
        return cnt;
    }
    values() {
        let values = [];
        for (let key in this.items) {
            if (this.has(key)) {
                values.push(this.items[key]);
            }
        }
        return values;
    }

    union(otherSet) {
        let unionSet = new MySet();   // 存储最终的并集集合数据
        this.values().forEach(value => {
            unionSet.add(value);
        });
        otherSet.values().forEach(value => {
            unionSet.add(value);
        });
        return unionSet;
    }

    intersection(otherSet) {
        let intersection = new MySet();
        let values = this.values(); // 预存当前集合值数组
        // 判断哪个大哪个小
        let otherValues = otherSet.values();
        let bigSet = undefined;
        let smallSet = undefined;
        if (values.length > otherValues.length) {
            bigSet = values;
            smallSet = otherValues;
        } else {
            bigSet = otherValues;
            smallSet = values;
        }
        for (let i = 0; i < smallSet.length; i++) {
            // 直接判断可能导致性能损耗,应该判断小的那个集合
            if (bigSet.has(smallSet[i])) {
                intersection.add(smallSet[i]);
            }
        }
    }

    difference(otherSet) {
        // 需要深拷贝
        let differenceSet = this;
        let intersection = this.intersection(otherSet);
        // 删除交集元素
        intersection.values().forEach(value => {
            differenceSet.delete(value);
        });
        return differenceSet;
    }

    isSubsetOf(otherSet) {
        if (this.size() > otherSet.size()) {
            return false;
        }
        let isSubset = true;
        this.values().every(value => {
            if (!otherSet.has(value)) {
                isSubset = false;
                return false;
            }
            return true;
        });
        return isSubset;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值