×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Tree Set in Scala

By IncludeHelp Last updated : November 14, 2024

Data structures are the most important part of the programming language as the lay the base where are variables and data are stored that are used in the program. And for every specific need of the program, Scala has implemented a unique data structure that can perform the task.

One of the useful data structures that you need to learn is Tree set, which has found uses in a lot, of advanced software.

Tree Set

A tree set is a special type of set that is used to store unique elements in a sorted manner. It can be said to be an implementation of sortedSet in Scala. For keeping the elements in sorted order, the treeSet uses a Red-Black tree.

Set stores unique elements without duplicates, but the stored elements are not in any specific order. So, to keep these elements in a sorted order the treeSet is used.

Examples

Examples of treeSet in Scala:

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

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

In Scala, there are two types of TreeSets available,

They are,

  1. Mutable TreeSet : scala.collection.mutable.TreeSet
  2. Immutable TreeSet : scala.collection.immutable.TreeSet

Creating a TreeSet

The creation of TreeSet in Scala is easy. Both types of treeSet are created with the same syntax. And are differentiated using the import statement which is required for creating the TreeSet.

Syntax

var TreeSet_Name = TreeSet(elements...)

Example

import scala.collection.mutable.TreeSet

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

Output:

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

In the above code, we have created a simple TreeSet named MyTreeSet. The TreeSet consists of integer values and as we can see, while inputting the values we have written them in random order but they are sorted in the TreeSet.

Empty Tree Set

You can also, create an empty TreeSet. You need to provide the data type of the TreeSet beforehand for that.

Example

import scala.collection.immutable.TreeSet

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

Output:

The elements of the TreeSet are TreeSet()

Adding new elements in TreeSet

We can also add a new element to the TreeSet. The "+" operator is assigned for the task.

If we are using mutable TreeSet, then the updates can be done within the same data structure. Otherwise, for immutable TreeSet, we need to create a new object to make the update.

Syntax

For mutable TreeSet: 
	MyTreeSet = MyTreeSet + element 

For immutable TreeSet : 
	var newTreeSet = oldTreeSet + element

Example

Program to add new element to mutable treeSet

import scala.collection.mutable.TreeSet

object MyClass {
    def main(args: Array[String]): Unit = {
        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)
    }
}

Output:

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)

Adding a Sequence to the TreeSet

You can add a whole sequence of elements to the TreeSet using the "++" operator.

Syntax

For mutable TreeSet, 
	TreeSet_Name = TreeSet_Name ++Seq_Name

For immutable TreeSet
	var newTreeSet = oldTreeSet ++ Seq_Name

Example

Program to add a sequence to an immutable TreeSet

import scala.collection.immutable.TreeSet

object MyClass {
    def main(args: Array[String]): Unit = {
        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)
    }
}

Output:

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)

Removing an Element from the TreeSet

You can also remove an element from the TreeSet using the "-" operator.

Syntax

For mutable TreeSet: 
	MyTreeSet = MyTreeSet - element 

For immutable TreeSet : 
	var newTreeSet = oldTreeSet - element

Example

Program to remove an element from immutable TreeSet

import scala.collection.immutable.TreeSet

object MyClass {
    def main(args: Array[String]): Unit = {
        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)
    }
}

Output:

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)

Removing a Sequence from the TreeSet

Similar to adding a new sequence, we can remove a Sequence from TreeSet using the "--" operator.

Syntax

For mutable TreeSet, 
	TreeSet_Name = TreeSet_Name -- Seq_Name

For immutable TreeSet
	var newTreeSet = oldTreeSet -- Seq_Name

Example

Program to remove a Sequence from a mutable TreeSet

import scala.collection.mutable.TreeSet

object MyClass {
    def main(args: Array[String]): Unit = {
        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)
    }
}

Output:

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)

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.