๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿค– data structures & algorithms

Data Structures & Algorithms with Javascript : Sets

 

[์ด์ „ ๊ธ€]2024.07.23 - [๐Ÿค– data structures & algorithms] - Data Structures & Algorithms with Javascript : Hashing

 

Data Structures & Algorithms with Javascript : Hashing

[์ด์ „ ๊ธ€] 2024.07.22 - [๐Ÿค– data structures & algorithms] - Data Structures & Algorithms with Javascript : DictionariesHashing์€ ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€์™€ ๋ฐ˜ํ™˜์„ ๋น ๋ฅด๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์ €์žฅ ๊ธฐ์ˆ ์ด๋‹ค.Hash table์ด๋ผ๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ

pyotato-dev.tistory.com

set์€ ์ค‘๋ณต๋˜๋Š” ์›์†Œ(member)๊ฐ€ ์—†๊ณ  ์›์†Œ ๊ฐ„์— ์ˆœ์„œ๊ฐ€ ์—†๋Š” ์ง‘ํ•ฉ์ด๋‹ค. 
๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป์˜ค๋Š˜์€ ๋‚ด์žฅ๋œ Set()์„ ํ•œ๋ฒˆ ์ง์ ‘ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•ด ๋ณด์ž.

Set์˜ ์ •์˜

  • ์›์†Œ๊ฐ€ ์—†๋Š” set์€ empty set(๊ณต์ง‘ํ•ฉ)์ด๋ผ๊ณ  ํ•œ๋‹ค. universe(์ „์ฒด ์ง‘ํ•ฉ)๋Š” ๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ์›์†Œ set์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  • ๋‘ set์ด ๋™์ผํ•œ ์›์†Œ๋ฅผ ์ง€๋‹Œ๋‹ค๋ฉด ์„œ๋กœ ๊ฐ™๋‹ค.
  • ํ•˜๋‚˜์˜ set B๊ฐ€ ๋‹ค๋ฅธ set A์˜ ์›์†Œ๋“ค์„ ํฌํ•จํ•œ๋‹ค๋ฉด set A๋Š” set B์˜ subset(๋ถ€๋ถ„ ์ง‘ํ•ฉ)์ด๋‹ค.

Set ์—ฐ์‚ฐ

 

  • Union : ๋‘ set์˜ ๋ชจ๋“  ์›์†Œ๋“ค (ํ•ฉ์นฉํ•ฉ)
  • Intersection : ๋‘ set์˜ ์›์†Œ๋“ค ์ค‘๊ณตํ†ต ์›์†Œ๋“ค (๊ต์ง‘ํ•ฉ)
  • Difference : ๋‘ set ์ค‘ ๊ต์ง‘ํ•ฉ์„ ์ œ์™ธํ•œ ์›์†Œ๋“ค(์ฐจ์ง‘ํ•ฉ)

Set ํด๋ž˜์Šค ๊ตฌํ˜„

โ€ผ ์˜ˆ์ œ๋Š” ์ฑ…์˜ ๊ตฌํ˜„์„ ์ฐธ๊ณ ํ•˜์—ฌ ํด๋ž˜์Šค๋กœ ์žฌ๊ตฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค!!

class Set{
	constructor(){
    	this.dataStore = [];
    }
    
    // ์ด๋ฏธ ์žˆ๋Š” ์›์†Œ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ์›์†Œ ์ถ”๊ฐ€
    add(data){
    	if (this.dataStore.indexOf(data) < 0) {
        	this.dataStore.push(data);
        	return true; 
        } else {
        	return false;
        }
    }
    
    // ์›์†Œ ์ œ๊ฑฐ
    remove(data){
        let pos = this.dataStore.indexOf(data); 
        if (pos > -1) {
       this.dataStore.splice(pos,1);
            return true; 
        } else {
            return false;
        }
    }
    
    // ์›์†Œ ๋ณด๊ธฐ
    show(){
    	return this.dataStore;
    }
    
    // ์›์†Œ๊ฐ€ set์— ์žˆ๋Š” ์ง€ ํ™•์ธํ•˜๋Š” ํ—ฌํผ ํ•จ์ˆ˜
    contains(data) {
        if (this.dataStore.indexOf(data) > -1) {
        	return true; 
        } else {
        	return false;
    	} 
    }
    
   // ํ•ฉ์ง‘ํ•ฉ
    union(){
        let tempSet = new Set();
        for (let i = 0; i < this.dataStore.length; ++i) {
            tempSet.add(this.dataStore[i]); 
        }
        for (let i = 0; i < set.dataStore.length; ++i) { 
            if (!tempSet.contains(set.dataStore[i])) {
                tempSet.dataStore.push(set.dataStore[i]); 
            }
        }    
        return tempSet; 
    }
    
    //๊ต์ง‘ํ•ฉ
    intersect(set){
        let tempSet = new Set();
        for(let i=0; i< this.dataStore.length; ++i){
            if(set.contains(this.dataStore[i])){
                tempSet.add(this.dataStore[i]);
            }
        }
        return tempSet;
    }

    // set์˜ ํฌ๊ธฐ
    size() {
        return this.dataStore.length;
    }
        
    //๋ถ€๋ถ„์ง‘ํ•ฉ
     subset(set){
        if (this.size() > set.size()) {
            return false; 
        } else {
            return this.dataStore.every(member=>
                set.contains(member)
            )
        }
        
    }

    // ์ฐจ์ง‘ํ•ฉ
    difference(set){
        let tempSet = new Set();
        for(let i = 0; i < this.dataStore.length; ++i){
            if(!set.contains(this.dataStore[i])){
                tempSet.add(this.dataStore[i]);
            }
        }
        return tempSet;
    
    }
}

const print = item => console.log(item);

const set1 = new Set();
set1.add("Mike"); 
set1.add("Clayton"); 
set1.add("Jennifer"); 
set1.add("Raymond");

print(set1.show());


const set2 = new Set();

set2.add("Raymond");
set2.add("Cynthia");
set2.add("Bryan");

print(set2.show()); // ['Raymond', 'Cynthia', 'Bryan']


const intersection = set1.intersect(set2);
print(`intersection: ${intersection.show()}`); // intersection: Raymond

const setA = new Set();

setA.add("Cynthia"); setA.add("Clayton"); setA.add("Jennifer"); setA.add("Danny"); setA.add("Jonathan"); setA.add("Terrill"); setA.add("Raymond"); setA.add("Mike");

print(set1.show()); // ['Mike', 'Clayton', 'Jennifer', 'Raymond']
const setB = new Set();

setB.add("Cynthia"); 
setB.add("Raymond"); 
setB.add("Jonathan");

if (setB.subset(setA)) {
   print("setB is a subset of setA.");
} else {
    print("setB is not a subset of setA.");
}
// setB is a subset of setA.

setB.add("Shirley"); // add Shirley to setB
print("add Shirley to setB"); // setB is not a subset of setA.

if (setB.subset(setA)) {
   print("setB is a subset of setA.");
} else {
    print("setB is not a subset of setA.");
}

const difference = setA.difference(setB);
print(`setA[${setA.show()}] difference setB[${setB.show()}] -> ${difference.show()}`);


// setA[Cynthia,Clayton,Jennifer,Danny,Jonathan,Terrill,Raymond,Mike] difference setB[Cynthia,Raymond,Jonathan,Shirley] -> Clayton,Jennifer,Danny,Terrill,Mike

๐Ÿ“š Reference

- Data Structures and Algorithms Using Javaโ€ Script by Michael McMillian (O’Reilly). Copyright 2014 Michael McMillan, 978-1-449-36493-9