Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
Battistella Stefano edited this pageMay 20, 2014 ·1 revision

In computer science, a set is an abstract data structure that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite set. Unlike most other collection types, rather than retrieving a specific element from a set, one typically tests a value for membership in a set.

Wikipedia

varset=newSet();//an empty set

Methods

insert(element)

This method insert the element in the set. The element must be created with the correct declaration as you see in the example.

varset=newSet();vare1=newElement(0);vare2=newElement(1);set.insert(e1);//set contains e1set.insert(e2);//set contains e1 e2

Complexity:O(1)

multiInsert(elements)

This method insert the element in the set. The element must be created with the correct declaration as you see in the example.

varset=newSet();vare1=newElement(0);vare2=newElement(1);set.multiInsert([e1,e2]);//set contains e1 e2

Complexity:O(n)

union(set)

This method returns the set representing the union of the two sets.

varset1=newSet();varset2=newSet();vare1=newElement(0);set1.insert(e1);vare2=newElement(1);set2.insert(e2);varunion=set1.union(set2);//union contains e1 e2

Complexity:O(n)

n: the number of total elements stored in the sets.

intersect(set)

This method returns the set representing the intersection of the two sets.

varset1=newSet();varset2=newSet();vare1=newElement(0);set1.multiInsert([e1,e2]);vare2=newElement(1);set2.insert(e2);varintersect=set1.intersect(set2);//intersection contains e2

Complexity:O(n·m)

n: the number of elements stored in the first set.m: the number of elements stored in the second set.

difference(set)

This method returns the set representing the difference of the first set with the second.

varset1=newSet();varset2=newSet();vare1=newElement(0);set1.multiInsert([e1,e2]);vare2=newElement(1);set2.insert(e2);vardifference=set1.difference(set2);//difference contains e1

Complexity:O(n·m)

n: the number of elements stored in the first set.m: the number of elements stored in the second set.

cartesianProduct(set)

This method returns the set representing the cartesian product of the first set with the second.

varset1=newSet();varset2=newSet();vare1=newElement(0);set1.insert(e1);vare2=newElement(1);set2.insert(e2);varproduct=set1.cartesianProduct(set2);//product contains [e1, e2]

Complexity:O(n·m)

n: the number of elements stored in the first set.m: the number of elements stored in the second set.

getItems()

This method returns the items stored in the set.var set = new Set();var e1 = new Element(0);var e2 = new Element(1);set.multiInsert([e1, e2]);set.getItems(); //0, 1

#### Complexity: _O_(n)### getCardinality()This method returns the size of the set.```JavaScriptvar set = new Set();var e1 = new Element(0);var e2 = new Element(1);set.multiInsert([e1, e2]);set.getCardinality(); //2

Complexity:O(1)

isEmpty()

This method checks if the set is empty or not.

varset=newSet();vare1=newElement(0);vare2=newElement(1);set.isEmpty();//falseset.multiInsert([e1,e2]);set.isEmpty();//true

Complexity:O(1)

clone()

This method returns a clone of the set.

varset=newSet();vare1=newElement(0);vare2=newElement(1);varclone=set.clone();//clone contains e1, e2

Complexity:O(n)

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp