-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
247b922
commit 582b8e6
Showing
6 changed files
with
235 additions
and
1 deletion.
There are no files selected for viewing
Binary file modified
BIN
+14.6 KB
(130%)
...StructuresAlgorithms.xcworkspace/xcuserdata/hi.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
7 changes: 7 additions & 0 deletions
7
Trees.playground/Pages/AVL Trees.xcplaygroundpage/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
//: [Previous](@previous) | ||
|
||
import Foundation | ||
|
||
var greeting = "Hello, playground" | ||
|
||
//: [Next](@next) |
65 changes: 65 additions & 0 deletions
65
Trees.playground/Pages/AVL Trees.xcplaygroundpage/Sources/AVLNode.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Foundation | ||
|
||
public class AVLNode<Element> { | ||
public var value: Element | ||
public var leftChild, rightChild: AVLNode? | ||
public var height = 0 | ||
|
||
public var balanceFactor: Int { | ||
leftHeight - rightHeight | ||
} | ||
|
||
public var leftHeight: Int { | ||
leftChild?.height ?? -1 | ||
} | ||
|
||
public var rightHeight: Int { | ||
rightChild?.height ?? -1 | ||
} | ||
|
||
public init(value: Element) { | ||
self.value = value | ||
} | ||
} | ||
|
||
extension AVLNode: CustomStringConvertible { | ||
public var description: String { | ||
diagram(for: self) | ||
} | ||
|
||
private func diagram(for node: AVLNode?, | ||
_ top: String = "", | ||
_ root: String = "", | ||
_ bottom: String = "") -> String { | ||
guard let node = node else { | ||
return root + "nil\n" | ||
} | ||
if node.leftChild == nil && node.rightChild == nil { | ||
return root + "\(node.value)\n" | ||
} | ||
return diagram(for: node.rightChild, top + " ", top + "┌──", top + "│ ") | ||
+ root + "\(node.value)\n" | ||
+ diagram(for: node.leftChild, bottom + "│ ", bottom + "└──", bottom + " ") | ||
} | ||
} | ||
|
||
//MARK: - traverse | ||
extension AVLNode { | ||
public func traverseInOrder(visit: (Element) -> Void) { | ||
leftChild?.traverseInOrder(visit: visit) | ||
visit(value) | ||
rightChild?.traverseInOrder(visit: visit) | ||
} | ||
|
||
public func traversePreOrder(visit: (Element) -> Void) { | ||
visit(value) | ||
leftChild?.traversePreOrder(visit: visit) | ||
rightChild?.traversePreOrder(visit: visit) | ||
} | ||
|
||
public func traversePostOrder(visit: (Element) -> Void) { | ||
leftChild?.traversePostOrder(visit: visit) | ||
rightChild?.traversePostOrder(visit: visit) | ||
visit(value) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
Trees.playground/Pages/AVL Trees.xcplaygroundpage/Sources/AVLTree.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Foundation | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters