Thursday, September 11, 2025
HomeiOS DevelopmentConstructing tree information buildings in Swift

Constructing tree information buildings in Swift


What’s a tree?

A tree) is an summary information construction that can be utilized to signify hierarchies. A tree often accommodates nodes with related information values. Every node can have youngster nodes and these nodes are linked collectively through a parent-child relationship.

The identify tree comes from the real-world, each digital and the bodily bushes have branches, there’s often one node that has many kids, and people also can have subsequent youngster nodes. 🌳

Every node within the tree can have an related information worth and a reference to the kid nodes.

The basis object is the place the tree begins, it is the trunk of the tree. A department node is just a few a part of the tree that has one other branches and we name nodes with out additional branches as leaves.

In fact there are numerous sorts of tree buildings, perhaps the most typical one is the binary tree. Strolling by means of the gadgets in a tree is known as traversal, there are a number of methods to step by means of the tree, in-order, pre-order, post-order and level-order. Extra about this in a while. 😅

Information bushes utilizing structs in Swift

After the short intro, I might like to indicate you find out how to construct a generic tree object utilizing structs in Swift. We’ll create a easy struct that may maintain any worth sort, by utilizing a generic placeholder. We’re additionally going to retailer the kid objects in an array that makes use of the very same node sort. First we will begin with a easy Node object that may retailer a String worth.

struct Node {
    var worth: String
    var kids: [Node]
}

var youngster = Node(worth: "youngster", kids: [])
var guardian = Node(worth: "guardian", kids: [child])

print(guardian) 

Let’s alter this code by introducing a generic variable as a substitute of utilizing a String sort. This manner we’re going to have the ability to reuse the identical Node struct to retailer every kind of values of the identical sort. We’re additionally going to introduce a brand new init methodology to make the Node creation course of only a bit extra easy.

struct Node {
    var worth: Worth
    var kids: [Node]
    
    init(_ worth: Worth, kids: [Node] = []) {
        self.worth = worth
        self.kids = kids
    }
}

var youngster = Node(2)
var guardian = Node(1, kids: [child])

print(guardian)

As you’ll be able to see the underlying sort is an Int, Swift is sensible sufficient to determine this out, however you too can explicitly write Node(2) or in fact every other sort that you just’d like to make use of.

One factor that it’s important to be aware when utilizing structs is that these objects are worth varieties, so if you wish to modify a tree you will want a mutating perform and it’s important to watch out when defining nodes, you may need to retailer them as variables as a substitute of constants if you want to alter them in a while. The order of your code additionally issues on this case, let me present you an instance. 🤔

struct Node {
    var worth: Worth
    var kids: [Node]
    
    init(_ worth: Worth, kids: [Node] = []) {
        self.worth = worth
        self.kids = kids
    }
    
    mutating func add(_ youngster: Node) {
        kids.append(youngster)
    }
}

var a = Node("a")
var b = Node("b")
var c = Node("c")

a.add(b)

print(a)


b.add(c) 

print(a)


print(b)

We have tried so as to add a baby node to the b object, however for the reason that copy of b is already added to the a object, it will not have an effect on a in any respect. It’s important to watch out when working with structs, since you are going to cross round copies as a substitute of references. That is often an awesome benefit, however generally it will not provide the anticipated conduct.

Yet another factor to notice about structs is that you’re not allowed to make use of them as recursive values, so for instance if we might wish to construct a linked checklist utilizing a struct, we cannot have the ability to set the subsequent merchandise.

struct Node {
    let worth: String
    
    let subsequent: Node?
}

The reason of this subject is well-written right here, it is all in regards to the required area when allocating the thing. Please attempt to determine the explanations by yourself, earlier than you click on on the hyperlink. 🤔

Learn how to create a tree utilizing a Swift class?

Most frequent examples of tree buildings are utilizing lessons as a base sort. This solves the recursion subject, however since we’re working with reference varieties, now we have to be extraordinarily cautious with reminiscence administration. For instance if we need to place a reference to the guardian object, now we have to declare it as a weak variable.

class Node {
    var worth: Worth
    var kids: [Node]
    weak var guardian: Node?

    init(_ worth: Worth, kids: [Node] = []) {
        self.worth = worth
        self.kids = kids

        for youngster in self.kids {
            youngster.guardian = self
        }
    }

    func add(youngster: Node) {
        youngster.guardian = self
        kids.append(youngster)
    }
}

let a = Node("a")
let b = Node("b")

a.add(youngster: b)

let c = Node("c", kids: [Node("d"), Node("e")])
a.add(youngster: c)

print(a) 

This time once we alter a node within the tree, the unique tree will probably be up to date as effectively. Since we’re now working with a reference sort as a substitute of a worth sort, we are able to safely construct a linked checklist or binary tree by utilizing the very same sort inside our class.

class Node {
    var worth: Worth
    
    var left: Node?
    var proper: Node?
    
    init(
        _ worth: Worth, 
        left: Node? = nil,
        proper: Node? = nil
    ) {
        self.worth = worth
        self.left = left
        self.proper = proper
    }
}


let proper = Node(3)
let left = Node(2)
let tree = Node(1, left: left, proper: proper)
print(tree) 

In fact you’ll be able to nonetheless use protocols and structs when you want worth varieties over reference varieties, for instance you’ll be able to provide you with a Node protocol after which two separate implementation to signify a department and a leaf. That is how a protocol oriented method can seem like.

protocol Node {
    var worth: Int { get }
}

struct Department: Node {
    var worth: Int
    var left: Node
    var proper: Node
}

struct Leaf: Node {
    var worth: Int
}


let tree = Department(
    worth: 1, 
    left: Leaf(worth: 2), 
    proper: Leaf(worth: 3)
)
print(tree)

I like this resolution quite a bit, however in fact the precise selection is yours and it ought to at all times rely in your present use case. Do not be afraid of lessons, polymorphism may saves you numerous time, however in fact there are circumstances when structs are merely a greater solution to do issues. 🤓

Implementing bushes utilizing Swift enums

One very last thing I might like to indicate you on this article is find out how to implement a tree utilizing the highly effective enum sort in Swift. Identical to the recursion subject with structs, enums are additionally problematic, however happily there’s a workaround, so we are able to use enums that references itself by making use of the oblique key phrase.

enum Node {
    case root(worth: Worth)
    oblique case leaf(guardian: Node, worth: Worth)

    var worth: Worth {
        change self {
        case .root(let worth):
            return worth
        case .leaf(_, let worth):
            return worth
        }
    }
}
let root = Node.root(worth: 1)
let leaf1 = Node.leaf(guardian: root, worth: 2)
let leaf2 = Node.leaf(guardian: leaf1, worth: 3)

An oblique enum case can reference the enum itself, so it’s going to allo us to create circumstances with the very same sort. This manner we’re going to have the ability to retailer a guardian node or alternatively a left or proper node if we’re speaking a few binary tree. Enums are freaking highly effective in Swift.

enum Node {
    case empty
    oblique case node(Worth, left: Node, proper: Node)
}

let a = Node.node(1, left: .empty, proper: .empty)
let b = Node.node(2, left: a, proper: .empty)
print(b)

These are just some examples how one can construct varied tree information buildings in Swift. In fact there’s much more to the story, however for now I simply needed to indicate you what are the professionals and cons of every method. You need to at all times select the choice that you just like the perfect, there is no such thing as a silver bullet, however solely choices. I hope you loved this little submit. ☺️

If you wish to know extra about bushes, you must learn the linked articles, since they’re actually well-written and it helped me quite a bit to grasp extra about these information buildings. Traversing a tree can be fairly an attention-grabbing subject, you’ll be able to be taught quite a bit by implementing varied traversal strategies. 👋

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments