How do you assign and unwrap an optional array object in Swift? - swift

I'm trying to invert a binary tree in Swift using the iterative approach. Essentially I just keep looping through each node, and putting it into a stack array. This array should be an array of optional nodes. Here is my code:
func invertTree(_ root: TreeNode?) -> TreeNode? {
if root != nil {
stack.append(root)
} else {
return root
}
var stack = [TreeNode?]()
while stack.count > 0 {
if stack.last == nil {
stack.removeLast()
} else {
var tempLeft : TreeNode? = stack.last!.left
stack.last!.left = stack.last!.right
stack.last!.right = tempLeft
if stack.last!.left != nil {
stack.append(stack.last!.left)
}
if stack.last.right != nil {
stack.append(stack.last!.right)
}
}
}
return root
}
I'm getting all sorts of optional errors. In particular, when I set:
var tempLeft : TreeNode? = stack.last!.left
I'm not sure why it says "value of optional type TreeNode? must be unwrapped to refer to member 'left' of unwrapped base type 'TreeNode' in solution.swift"
I don't understand why it's telling me to unwrap the optional, when I'm already force unwrapping it. I don't want to unwrap the .left node since I want to include nils in my stack for the iteration to work.
Any help would be greatly appreciated.

stack.last is a doubly optional TreeNode: TreeNode??.
You could use optional chaining like so:
if stack.last == nil {
stack.removeLast()
} else {
stack.last
var tempLeft : TreeNode? = stack.last!?.left
stack.last!?.left = stack.last!?.right
stack.last!?.right = tempLeft
if stack.last!?.left != nil {
stack.append(stack.last!?.left)
}
if stack.last??.right != nil {
stack.append(stack.last!?.right)
}
}
Or avoid the drama and use pattern matching:
if case let node?? = stack.last {
let tempLeft = node.left
node.left = node.right
node.right = tempLeft
...
Other than that, it seems that there are other areas of your code that you need to tend to. For example, using stack before its declaration.
Alternative implementation
Here is a solution that uses a stack:
class Solution {
func invertTree(_ root: TreeNode?) -> TreeNode? {
guard let r = root else {
return nil
}
var stack = [r]
while let node = stack.last {
let temp = node.left
node.left = node.right
node.right = temp
stack.removeLast()
if let left = node.left { stack.append(left) }
if let right = node.right { stack.append(right) }
}
return r
}
}

Related

How to fix this Binary Tree Deserialize problem?

Where is the bug in this serialize() function
I wrote a function deserialize() in Swift to construct a binary tree from an array of optional Integer. I tested this function by comparing the output with the output of a tree constructed using manual method with same array. They look the same, which is good !
However, when I run another function isSameTree(), that is used to compare 2 trees (I am sure this function is working correctly), on deserialize()'s output and manual method's output, I have different results !
I assume deserialize() is not correct, but I could not find the bug!
// helper code
public class BinaryNode {
public var value: Int
public var left: BinaryNode?
public var right: BinaryNode?
public init(_ value: Int) {
self.value = value
self.left = nil
self.right = nil
}
}
extension BinaryNode {
public var description: String {
return diagram(for: self)
}
private func diagram(for node: BinaryNode?,
_ top: String = "",
_ root: String = "",
_ bottom: String = "") -> String {
guard let node = node else {
return root + "nil\n"
}
if node.left == nil && node.right == nil {
return root + "\(node.value)\n"
}
return diagram(for: node.right,
top + " ", top + "┌──", top + "│ ")
+ root + "\(node.value)\n"
+ diagram(for: node.left,
bottom + "│ ", bottom + "└──", bottom + " ")
}
}
public func deserialize(_ array: inout [Int?]) -> BinaryNode? {
guard !array.isEmpty, let value = array.removeFirst() else {
return nil
}
let node = BinaryNode(value)
node.left = deserialize(&array)
node.right = deserialize(&array)
return node
}
func isSameTree(_ p: BinaryNode?, _ q: BinaryNode?) -> Bool {
guard let p = p else {
return q == nil
}
guard let q = q else {
return p == nil
}
if p.value != q.value {
return false
}
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right)
}
// Using deserialize to construct trees
var a1: [Int?] = [1,nil,2,3]
var a2: [Int?] = [1,nil,2,nil,3]
if let tree = deserialize(&a1) {
print(tree.description)
}
if let tree = deserialize(&a2) {
print(tree.description)
}
// Using manual to construct trees
let a3: BinaryNode = {
let one = BinaryNode(1)
let two = BinaryNode(2)
let three = BinaryNode(3)
one.right = two
two.left = three
return one
}()
print(a3.description)
let a4: BinaryNode = {
let one = BinaryNode(1)
let two = BinaryNode(2)
let three = BinaryNode(3)
one.right = two
two.right = three
return one
}()
print(a4.description)
// The print statements above show similar trees are constructed
// However, below results are not same
isSameTree(deserialize(&a1), deserialize(&a2)) // true <- this is wrong
isSameTree(a3, a4) // false <--- this is correct
Seems you have forgotten that your deserialize(_:) is destructive for its parameter... Please remember why you need &.
//Re-load a1 & a2...
a1 = [1,nil,2,3]
a2 = [1,nil,2,nil,3]
print(isSameTree(deserialize(&a1), deserialize(&a2))) //-> false
print(isSameTree(a3, a4)) //-> false

Reverse a linked list in Swift

Reversing a linked list in Swift is easy. I have a working solution. However, while preparing for whiteboard interviews the version I produce quickly simply does not work and I cannot identify why.
I need to know why the following does not work - from the Playground I believe it is because
tail = previous
line errors and execution never completes.
func reverseLL (node: Node?) -> Node? {
guard node != nil else { return nil }
var tail : Node? = node
var previous = node?.next
while previous != nil {
let tailRef = previous?.next
previous?.next = tail
tail = previous
previous = tailRef
}
return tail
}
My definition of the linked list is:
class Node: CustomStringConvertible {
var data: Int
var next: Node?
var description: String {
return String(data) + (next != nil ? next!.description : "")
}
init (data: Int) {
self.data = data
next = nil
}
func appendToTail(data: Int) {
if (next != nil) {
next?.appendToTail(data: data)
}
else {
next = Node(data: data)
}
}
}
My working version of reverseLL (which I accept is more 'Swifty') is as follows, but I believe it should be functionally identical to my earlier definition.
func reverseLL (node: Node?) -> Node? {
guard node != nil else { return nil }
var tail: Node?
var headNode = node
while let head = headNode {
let tailRef = head.next
head.next = tail
tail = head
headNode = tailRef
}
return tail
}
So creating a linked list with
let ll = Node(data: 3)
ll.appendToTail(data: 4)
ll.appendToTail(data: 4)
ll.appendToTail(data: 5)
gives the data in order of 3445
and reversed through
reverseLL(node: ll)
gives the data in order of 5443
To be clear, why does the
tail = previous
line halt execution in my first definition of reverseLL?
The second version is more Swifty since you're using optional binding and avoiding the horrendous forced-unwrapping.
The problem in the first version is that tail is initially equal to node. In the example that you've given that is (3->4->4->5).
So when you do previous?.next = tail in the first iteration, previous becomes (4->4->5->3->4->5->3->4->5->...). Notice that The node with data equal to 5 now points to a node with data equal to 3. Which creates an infinite loop.
Simplification
The guard statement could also be written as :
guard node?.next != nil else {
return node
}
which would include lists with a single node.
Here is the complete code including class, function and input and output things
// First we are creating class to store the data of Linked List
class Node {
var data: Int
var next: Node?
var description: String {
return String(data) + (next != nil ? next!.description : "")
}
init (data: Int) {
self.data = data
next = nil
}
func appendToLast(data: Int) {
if (next != nil) {
next?.appendToLast(data: data)
} else {
next = Node(data: data)
}
}
}
//Function that return the reversed Linked List
func reverseLinkedList(node: Node?) -> Node? {
guard node != nil else { return nil }
var tail: Node?
var headNode = node
while let head = headNode {
let tailRef = head.next
head.next = tail
tail = head
headNode = tailRef
}
return tail
}
//Input all the data which we have in the linkedlist and output of that inside the print function.
let ll = Node(data: 3)
ll.appendToLast(data: 4)
ll.appendToLast(data: 4)
ll.appendToLast(data: 5)
let reversedLL = reverseLinkedList(node: ll)
print(reversedLL?.description ?? "No Data")

Swift 3 delete nodes from singly linked list

I am trying to remove all elements from a linked list of integers that have value val. Is it necessary to set the removed nodes to nil to free memory?
func removeElements(_ head: Node?, _ val: Int) -> Node? {
var first = head
var current = head
var prev: Node?
while current != nil {
if current?.val != val {
prev = current
} else if current?.val == first?.val {
var oldFirst = first
first = current?.next
oldFirst = nil // is this line necessary?
} else {
prev?.next = current?.next // do I need to set current to nil?
}
current = current?.next
}
return first
}
oldFirst = nil only sets the variable in your current scope to nil.
Again, current is a variable in your local scope, it gets already dereferenced and thus cleaned up once you leave its scope.
If you have no strong references to an object anymore it is released by itself because Swift uses Automatic Reference Counting (ARC: https://en.wikipedia.org/wiki/Automatic_Reference_Counting)
I am not sure why you have the 2nd case in your code. I guess it checks the case where the current node has value val but you compare to first.val instead of val
func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
guard head != nil else {
return nil
}
let preDummy = ListNode(val: 0), postDummy = ListNode(val: 0)
var pre = preDummy, node = head
while node != nil {
if node!.val == val {
postDummy.next = node?.next
node!.next = nil
} else {
pre.next = node
pre = node!
}
node = node!.next
}
pre.next = postDummy.next
return preDummy.next
}

Swift: Avoid imperative For Loop

What I'm trying to accomplish in imperative:
var mapNames = [String]()
var mapLocation = [String]()
for valueMap in valueMaps {
if let name = valueMap.name {
mapNames.append(name)
}
if let location = valueMap.location {
mapLocation.append(location)
}
}
What's the best way using a high order function or perhaps an array method (array.filter etc.) to compact the code above and also avoid using the for loop
Here is what I have tried, but the compiler gives an error:
let getArrayOfNames = valueMaps.filter() {
if let name = ($0 as valueMaps).name as [String]! {
return name;
}
}
let getArrayOfLocations = valueMaps.filter() {
if let type = ($0 as valueMaps).location as [String]! {
return type;
}
}
You need both filter() and map() :
let mapNames = valueMaps.filter( {$0.name != nil }).map( { $0.name! })
let mapLocations = valueMaps.filter( {$0.location != nil }).map( { $0.location! })
The filter takes a predicate as an argument (which specifies which
elements should be included in the result), and the map takes
a transformation as an argument. You were trying to merge both
aspects into the filter, which is not possible.
Update: As of Swift 2(?) has a flatMap() method for sequences, which
can be used to obtain the result in a single step:
let mapNames = valueMaps.flatMap { $0.name }
The closure is applied to all array elements, and the return value is an
array with all non-nil unwrapped results.
The filter() function needs its closure to return a bool - not the value you want to store in an array. You could chain filter and map together to get what you want, then:
let getArrayOfNames = valueMaps
.filter { $0.name != nil }
.map{ $0.name! }
Or, to do it in one function, with reduce:
let getArrayOfNames = valueMaps
.reduce([String]()) {
accu, element in
if let name = element.name {
return accu + [name]
} else {
return accu
}
}
Actually, the reduce can be a little better:
let getArrayOfNames = valueMaps.reduce([String]()) {
(names, value) in names + (value.name.map{[$0]} ?? [])
}
let getArrayOfLocations = valueMaps.reduce([String]()) {
(locs, value) in locs + (value.location.map{[$0]} ?? [])
}

Why does this work (not using vs. using optionals)?

Why does alternative1 below work flawlessly?
The macros are bogus of course and for illustration purposes only:
func commonPrefixLength<T: Swift.Collection, U: Swift.Collection where
T: Sequence, U: Sequence,
T.GeneratorType.Element: Equatable,
T.GeneratorType.Element == U.GeneratorType.Element>
(collection1: T, collection2: U) -> T.IndexType.DistanceType {
var collection2generator = collection2.generate()
var i: T.IndexType.DistanceType = 0
for element1 in collection1 {
#if alternative1
let element2 = collection2generator.next()
if (element1 != element2) {
return i
}
#elseif alternative2
let optionalElement2 = collection2generator.next()
if let element2 = optionalElement2 {
if (element1 != element2) {
return i
}
}
else {
break
}
#endif
i++
}
return i
}
commonPrefixLength("abX", "abc")
Here is a gist of the above.
In the comparison, you are comparing an optional (element2) with an non-optional (element1).
if (element1 != element2) {
return i
}
There is no problem comparing an optional to an non-optional. Why should there be? If element2 is nil, then the result of the above comparison will be true. That's well defined.
Non-optionals can be implicitly cast to optionals, otherwise you wouldn't be able to assign a non-optional to an optional.
let nonOptional = ""
var optional: String? = nonOptional