Scala中的set树集

数据结构是编程语言中最重要的部分,因为它是存储程序中使用的变量和数据的基础。对于程序的每个特定需求,Scala都实现了可以执行任务的独特数据结构。

树集是您需要学习的有用数据结构之一,它已经在许多高级软件中找到了用途。

树集

树集是一种特殊类型的集,用于以排序的方式存储唯一元素。可以说是Scala中sortedSet的实现。为了使元素保持排序,treeSet使用一棵红黑树。

Set存储唯一元素,没有重复项,但是存储的元素没有任何特定顺序。因此,为了使这些元素保持排序状态,将使用treeSet。

Scala中的treeSet的示例:

Integer TreeSet : 
	TreeSet(3, 6, 8, 12, 19)

String TreeSet :	TreeSet(Java, Kotlin, Scala, XML)

在Scala中,有两种可用的树集,

他们是,

  1. Mutable TreeSet:scala.collection.mutable.TreeSet

  2. 不可变的TreeSetscala.collection.immutable.TreeSet

创建一个树集

在Scala中创建TreeSet很容易。两种类型的treeSet都使用相同的语法创建。并且使用创建TreeSet所需的import语句进行区分。

语法:

var TreeSet_Name = TreeSet(elements...)

在Scala中创建TreeSet的程序

import scala.collection.mutable.TreeSet

object MyClass {
    def main(args: Array[String]) {
        var MyTreeSet = TreeSet(1, 5, 2, 6, 9, 4)
        println("The elements of the TreeSet are " + MyTreeSet)
    }
}

输出:

The elements of the TreeSet are TreeSet(1, 2, 4, 5, 6, 9)

在上面的代码中,我们创建了一个名为MyTreeSet的简单TreeSet。该TreeSet中包含的整数值,当我们可以看到,在输入我们以随机顺序写入它们的值,但它们在排序TreeSet中

空树集

您也可以创建一个空的TreeSet。您需要为此提供TreeSet的数据类型。

程序:

import scala.collection.immutable.TreeSet

object MyClass {
    def main(args: Array[String]) {
        var emptyTreeSet = TreeSet[Integer]()
        println("The elements of the TreeSet are " + emptyTreeSet)
    }
}

输出:

The elements of the TreeSet are TreeSet()

在TreeSet中添加新元素

我们还可以向TreeSet添加一个新元素。该“+”操作符被分配的任务。

如果我们使用可变的TreeSet,则可以在相同的数据结构中完成更新。否则,对于不可变的TreeSet,我们需要创建一个新对象来进行更新。

语法:

For mutable TreeSet: 
	MyTreeSet = MyTreeSet + element 

For immutable TreeSet : 
	var newTreeSet = oldTreeSet + element

将新元素添加到可变树集的程序

import scala.collection.mutable.TreeSet

object MyClass {
    def main(args: Array[String]) {
        var myTreeSet = TreeSet(4, 7, 100, 54)
        
        println("The elements of the TreeSet are " + myTreeSet)
        
        println("\nAdding new element to the TreeSet\n")
        myTreeSet =  myTreeSet + 43
        
        println("The elements of the TreeSet are " + myTreeSet)
    }
}

输出:

The elements of the TreeSet are TreeSet(4, 7, 54, 100)

Adding new element to the TreeSet

The elements of the TreeSet are TreeSet(4, 7, 43, 54, 100)

向TreeSet添加序列

您可以使用“ ++”运算符将整个元素序列添加到TreeSet中。

语法:

For mutable TreeSet, 
	TreeSet_Name = TreeSet_Name ++Seq_Name

For immutable TreeSet
	var newTreeSet = oldTreeSet ++ Seq_Name

程序向不可变的TreeSet添加序列

import scala.collection.immutable.TreeSet

object MyClass {
    def main(args: Array[String]) {
        var orignalTreeSet = TreeSet(4, 7, 100, 54)
        
        println("The elements of the TreeSet are " + orignalTreeSet)
        
        println("\nAdding a Sequence to the TreeSet\n")
        var newTreeSet =  orignalTreeSet ++ Seq( 1, 5, 6)
        
        println("The elements of the TreeSet are " + newTreeSet)
    }
}

输出:

The elements of the TreeSet are TreeSet(4, 7, 54, 100)

Adding a Sequence to the TreeSet

The elements of the TreeSet are TreeSet(1, 4, 5, 6, 7, 54, 100)

从TreeSet中删除元素

您也可以使用“ - ”运算符从TreeSet中删除元素。

语法:

For mutable TreeSet: 
	MyTreeSet = MyTreeSet - element 

For immutable TreeSet : 
	var newTreeSet = oldTreeSet - element

从不可变的TreeSet中删除元素的程序

import scala.collection.immutable.TreeSet

object MyClass {
    def main(args: Array[String]) {
        var orignalTreeSet = TreeSet(4, 7, 100, 54)
        
        println("The elements of the TreeSet are " + orignalTreeSet)
        
        println("\nRemoving an  element from the TreeSet\n")
        var newTreeSet =  orignalTreeSet - 7
        
        println("The elements of the TreeSet are " + newTreeSet)
    }
}

输出:

The elements of the TreeSet are TreeSet(4, 7, 54, 100)

Removing an  element from the TreeSet

The elements of the TreeSet are TreeSet(4, 54, 100)

从树集中删除序列

与添加新序列相似,我们可以使用“ - ”运算符从TreeSet中删除序列。

语法:

For mutable TreeSet, 
	TreeSet_Name = TreeSet_Name -- Seq_Name

For immutable TreeSet
	var newTreeSet = oldTreeSet -- Seq_Name

程序从可变树集中删除序列

import scala.collection.mutable.TreeSet

object MyClass {
    def main(args: Array[String]) {
        var myTreeSet = TreeSet(1, 3, 6, 9, 12, 54)
        
        println("The elements of the TreeSet are " + myTreeSet)
        
        println("\nRemoving a sequence from the TreeSet\n")
        myTreeSet =  myTreeSet -- Seq(6, 12)
        
        println("The elements of the TreeSet are " + myTreeSet)
    }
}

输出:

The elements of the TreeSet are TreeSet(1, 3, 6, 9, 12, 54)

Removing a sequence from the TreeSet

The elements of the TreeSet are TreeSet(1, 3, 9, 54)