js数据结构与算法——集合

    <script>
        function Set(){
            var items = {};//使用对象表示集合,因为js对象不允许一个键指向两个不同的值,保证集合里面的匀速唯一性

            this.add = function(value){
                //向集合添加一个新的项
                if(!this.has(value)){
                    items[value] = value;
                    return true;
                }
                return false;
            }

            this.remove = function(value){
                //从集合移除一个值
                if(this.has(value)){
                    delete items[value];
                    return true;
                }
                return false;
            }

            this.has = function(value){
                //判断值是否在集合中,返回布尔值
                // return value in items;
                //第二种方法
                return items.hasOwnProperty(value);
            }

            this.clear = function(){
                //清空集合
                items = {};
            }

            this.size = function(){
                //返回集合的元素数量
                return Object.keys(items).length;
            }

            this.values = function(){
                //返回一个包含集合中所有值的数组
                return Object.keys(items);
            }

            /*
                title:集合操作
                1.并集;2.交集;3.差集;4.子集
            */
            
            this.union = function(otherSet){
                //并集
                var unionSet = new Set();
                var values = this.values();
                for(let i=0;i<values.length;i++){
                    unionSet.add(values[i]);
                }
                values = otherSet.values();
                for(let i=0;i<values.length;i++){
                    unionSet.add(values[i]);
                }
                return unionSet;
            }

            this.intersection = function(otherSet){
                //交集
                var intersectionSet = new Set();
                var values = this.values();
                for(let i=0;i<values.length;i++){
                    if(otherSet.has(values[i])){
                        intersectionSet.add(values[i]);
                    }
                }
                return intersectionSet;
            }

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

            }

            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;
                }
            }

        }
        //set集合使用

        // var set = new Set();
        // set.add(1);
        // console.log(set.values()); //输出["1"]
        // console.log(set.has(1)); //输出true
        // console.log(set.size()); //输出1
        // set.add(2);
        // console.log(set.values()); //输出["1", "2"]
        // console.log(set.has(2)); //true
        // console.log(set.size()); //2
        // set.remove(1);
        // console.log(set.values()); //输出["2"]
        // set.remove(2);
        // console.log(set.values()); //输出[] 
        var setA = new Set();
        setA.add(1);
        setA.add(2);
        setA.add(3);
        var setB = new Set();
        setB.add(3);
        setB.add(4);
        setB.add(5);
        setB.add(6);
        var unionAB = setA.union(setB);
        console.log(unionAB.values()); 
        var intersectionAB = setA.intersection(setB);
        console.log(intersectionAB.values())
        var differenceAB = setA.difference(setB);
        console.log(differenceAB.values())

    </script>

猜你喜欢

转载自www.cnblogs.com/huangmin1992/p/10415219.html