zoukankan      html  css  js  c++  java
  • 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))
            }
        }
    
  • 相关阅读:
    C# XML 文档注释
    大数据知识学习
    现在的人,买个钢铁做的车,每天擦,每周打蜡。可对自已的身体最应该保养的“车”,却从不养护
    Asp.net项目因Session阻塞导致页面打开速度变慢
    AvoidRepeatSubmit通过Javascript避免客户端重复提交请求
    Linux下Attansic L2 网卡驱动安装
    如果知道dll文件是面向32位系统还是面向64位系统的?
    整理C# 二进制,十进制,十六进制 互转
    连接Oracle时出现“System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”错误的问题
    [转]删除hbase表region块脚本
  • 原文地址:https://www.cnblogs.com/cc-freiheit/p/10598974.html
Copyright © 2011-2022 走看看