简体   繁体   中英

Binary Tree with struct in swift

I was trying to make a binary tree with the help of struct as given below:

struct BinaryTree {
    var value: Int
    var left: BinaryTree
    var right: BinaryTree 
}

But I am getting error error: value type 'BinaryTree' cannot have a stored property that recursively contains it . Here struct is value type so I can't create same struct object in it.

How can I achieve this???

You can use class for this structure, structs do not allow to reference itself.

class BinaryTree {
    var value: Int?
    var left: BinaryTree?
    var right: BinaryTree?
    init(value: Int) {
        self.value = value
    }
}

let node = BinaryTree.init(value: 1)
node.left = BinaryTree(value: 2)

The reason for this compile error is memory allocation: Value types are fixed structures and they occupy a fixed space in memory, in registers and the stack, depending on its size. That space is pre-determined by the type and must be known at compile time.

https://medium.com/@leandromperez/bidirectional-associations-using-value-types-in-swift-548840734047

Structs are value types that's the reason why recursion won't work. You have to use Class instead, because they are reference types. But as you said you want a solution with value types. Here is a solution for you using enum

Enums with indirect cases are allocated on the heap and thus contain only pointers to the recursive children. Without pointer indirection, the type would otherwise be infinitely large, since it contains infinitely many times.

enum BinaryTree<Element: Comparable> {
    case empty
    indirect case node(value: Element, left: BinaryTree<Element>, right: BinaryTree<Element>)
}

extension BinaryTree {
    func addNode(_ newValue: Element) -> BinaryTree<Element> {
        switch self {
        case .empty:
            return BinaryTree.node(value: newValue, left: .empty, right: .empty)
        case let .node(value, left, right):
            if newValue < value {
                return BinaryTree.node(value: value, left: left.addNode(newValue), right: right)
            } else {
                return BinaryTree.node(value: value, left: left, right: right.addNode(newValue))
            }
        }    
    } 
}

let tree = BinaryTree<Int>.empty.addNode(2)

OR

You simply just use Class

You can use class for this structure, structs do not allow to reference itself.

class BinaryTree {
    var value: Int
    var left: BinaryTree?
    var right: BinaryTree?
    init(value: Int) {
        self.value = value
    }
}

I hope this will work for you.

To construct the tree using value types, you need to add protocols into the mix. Here is an example:

protocol Node {
  var value: Int {get}
}

struct Tree: Node {
  var value: Int
  var  left: Node
  var right: Node
}

struct Leaf: Node {
  var value: Int
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM