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

原文:

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

 

概念:

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

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

 

基础集合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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中。如图:

  函数如下:

1
2
3
4
5
6
7
8
9
10
11
12
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中。

  函数如下:

1
2
3
4
5
6
7
8
9
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中。如图:

 

  函数如下:

1
2
3
4
5
6
7
8
9
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,如图: 

 

  函数如下:

1
2
3
4
5
6
7
subset(otherSet) {
    if (this.size > otherSet.size) {
        return false;
    } else {
        return !this.values.some(val => !otherSet.has(val))
    }
}

 

posted @   shiweiqianju  阅读(686)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
点击右上角即可分享
微信分享提示