Skip to content

Commit

Permalink
Removing "open" access level that was automatically included in auto …
Browse files Browse the repository at this point in the history
…migration to swift 3. Removing underscore labels.
  • Loading branch information
Chris Pilcher committed Aug 18, 2016
1 parent 4683bb9 commit cfb23c4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 68 deletions.
102 changes: 51 additions & 51 deletions AVL Tree/AVLTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

open class TreeNode<Key: Comparable, Payload> {
public class TreeNode<Key: Comparable, Payload> {
public typealias Node = TreeNode<Key, Payload>

open var payload: Payload?
var payload: Payload?

fileprivate var key: Key
internal var leftChild: Node?
Expand Down Expand Up @@ -51,35 +51,35 @@ open class TreeNode<Key: Comparable, Payload> {
self.init(key: key, payload: nil)
}

open var isRoot: Bool {
var isRoot: Bool {
return parent == nil
}

open var isLeaf: Bool {
var isLeaf: Bool {
return rightChild == nil && leftChild == nil
}

open var isLeftChild: Bool {
var isLeftChild: Bool {
return parent?.leftChild === self
}

open var isRightChild: Bool {
var isRightChild: Bool {
return parent?.rightChild === self
}

open var hasLeftChild: Bool {
var hasLeftChild: Bool {
return leftChild != nil
}

open var hasRightChild: Bool {
var hasRightChild: Bool {
return rightChild != nil
}

open var hasAnyChild: Bool {
var hasAnyChild: Bool {
return leftChild != nil || rightChild != nil
}

open var hasBothChildren: Bool {
var hasBothChildren: Bool {
return leftChild != nil && rightChild != nil
}
}
Expand All @@ -89,8 +89,8 @@ open class TreeNode<Key: Comparable, Payload> {
open class AVLTree<Key: Comparable, Payload> {
public typealias Node = TreeNode<Key, Payload>

fileprivate(set) open var root: Node?
fileprivate(set) open var size = 0
fileprivate(set) var root: Node?
fileprivate(set) var size = 0

public init() { }
}
Expand All @@ -115,26 +115,26 @@ extension TreeNode {

extension AVLTree {
subscript(key: Key) -> Payload? {
get { return search(key) }
set { insert(key, newValue) }
get { return search(input: key) }
set { insert(key: key, payload: newValue) }
}

public func search(_ input: Key) -> Payload? {
if let result = search(input, root) {
public func search(input: Key) -> Payload? {
if let result = search(key: input, node: root) {
return result.payload
} else {
return nil
}
}

fileprivate func search(_ key: Key, _ node: Node?) -> Node? {
fileprivate func search(key: Key, node: Node?) -> Node? {
if let node = node {
if key == node.key {
return node
} else if key < node.key {
return search(key, node.leftChild)
return search(key: key, node: node.leftChild)
} else {
return search(key, node.rightChild)
return search(key: key, node: node.rightChild)
}
}
return nil
Expand All @@ -144,31 +144,31 @@ extension AVLTree {
// MARK: - Inserting new items

extension AVLTree {
public func insert(_ key: Key, _ payload: Payload? = nil) {
public func insert(key: Key, payload: Payload? = nil) {
if let root = root {
insert(key, payload, root)
insert(input: key, payload: payload, node: root)
} else {
root = Node(key: key, payload: payload)
}
size += 1
}

fileprivate func insert(_ input: Key, _ payload: Payload?, _ node: Node) {
private func insert(input: Key, payload: Payload?, node: Node) {
if input < node.key {
if let child = node.leftChild {
insert(input, payload, child)
insert(input: input, payload: payload, node: child)
} else {
let child = Node(key: input, payload: payload, leftChild: nil, rightChild: nil, parent: node, height: 1)
node.leftChild = child
balance(child)
balance(node: child)
}
} else {
if let child = node.rightChild {
insert(input, payload, child)
insert(input: input, payload: payload, node: child)
} else {
let child = Node(key: input, payload: payload, leftChild: nil, rightChild: nil, parent: node, height: 1)
node.rightChild = child
balance(child)
balance(node: child)
}
}
}
Expand All @@ -177,37 +177,37 @@ extension AVLTree {
// MARK: - Balancing tree

extension AVLTree {
fileprivate func updateHeightUpwards(_ node: Node?) {
fileprivate func updateHeightUpwards(node: Node?) {
if let node = node {
let lHeight = node.leftChild?.height ?? 0
let rHeight = node.rightChild?.height ?? 0
node.height = max(lHeight, rHeight) + 1
updateHeightUpwards(node.parent)
updateHeightUpwards(node: node.parent)
}
}

fileprivate func lrDifference(_ node: Node?) -> Int {
fileprivate func lrDifference(node: Node?) -> Int {
let lHeight = node?.leftChild?.height ?? 0
let rHeight = node?.rightChild?.height ?? 0
return lHeight - rHeight
}

fileprivate func balance(_ node: Node?) {
fileprivate func balance(node: Node?) {
guard let node = node else {
return
}

updateHeightUpwards(node.leftChild)
updateHeightUpwards(node.rightChild)
updateHeightUpwards(node: node.leftChild)
updateHeightUpwards(node: node.rightChild)

var nodes = [Node?](repeating: nil, count: 3)
var subtrees = [Node?](repeating: nil, count: 4)
let nodeParent = node.parent

let lrFactor = lrDifference(node)
let lrFactor = lrDifference(node: node)
if lrFactor > 1 {
// left-left or left-right
if lrDifference(node.leftChild) > 0 {
if lrDifference(node: node.leftChild) > 0 {
// left-left
nodes[0] = node
nodes[2] = node.leftChild
Expand All @@ -230,7 +230,7 @@ extension AVLTree {
}
} else if lrFactor < -1 {
// right-left or right-right
if lrDifference(node.rightChild) < 0 {
if lrDifference(node: node.rightChild) < 0 {
// right-right
nodes[1] = node
nodes[2] = node.rightChild
Expand All @@ -253,7 +253,7 @@ extension AVLTree {
}
} else {
// Don't need to balance 'node', go for parent
balance(node.parent)
balance(node: node.parent)
return
}

Expand Down Expand Up @@ -285,19 +285,19 @@ extension AVLTree {
nodes[0]?.rightChild = subtrees[3]
subtrees[3]?.parent = nodes[0]

updateHeightUpwards(nodes[1]) // Update height from left
updateHeightUpwards(nodes[0]) // Update height from right
updateHeightUpwards(node: nodes[1]) // Update height from left
updateHeightUpwards(node: nodes[0]) // Update height from right

balance(nodes[2]?.parent)
balance(node: nodes[2]?.parent)
}
}

// MARK: - Displaying tree

extension AVLTree {
fileprivate func display(_ node: Node?, level: Int) {
fileprivate func display(node: Node?, level: Int) {
if let node = node {
display(node.rightChild, level: level + 1)
display(node: node.rightChild, level: level + 1)
print("")
if node.isRoot {
print("Root -> ", terminator: "")
Expand All @@ -306,30 +306,30 @@ extension AVLTree {
print(" ", terminator: "")
}
print("(\(node.key):\(node.height))", terminator: "")
display(node.leftChild, level: level + 1)
display(node: node.leftChild, level: level + 1)
}
}

public func display(_ node: Node) {
display(node, level: 0)
public func display(node: Node) {
display(node: node, level: 0)
print("")
}
}

// MARK: - Delete node

extension AVLTree {
public func delete(_ key: Key) {
public func delete(key: Key) {
if size == 1 {
root = nil
size -= 1
} else if let node = search(key, root) {
delete(node)
} else if let node = search(key: key, node: root) {
delete(node: node)
size -= 1
}
}

fileprivate func delete(_ node: Node) {
private func delete(node: Node) {
if node.isLeaf {
// Just remove and balance up
if let parent = node.parent {
Expand All @@ -344,7 +344,7 @@ extension AVLTree {
parent.rightChild = nil
}

balance(parent)
balance(node: parent)
} else {
// at root
root = nil
Expand All @@ -354,11 +354,11 @@ extension AVLTree {
if let replacement = node.leftChild?.maximum() , replacement !== node {
node.key = replacement.key
node.payload = replacement.payload
delete(replacement)
delete(node: replacement)
} else if let replacement = node.rightChild?.minimum() , replacement !== node {
node.key = replacement.key
node.payload = replacement.payload
delete(replacement)
delete(node: replacement)
}
}
}
Expand Down
34 changes: 17 additions & 17 deletions AVL Tree/Tests/AVLTreeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AVLTreeTests: XCTestCase {
self.tree?.autopopulateWithNodes(5)

for i in 6...10 {
self.tree?.insert(i)
self.tree?.insert(key: i)
do {
try self.tree?.inOrderCheckBalanced(self.tree?.root)
} catch _ {
Expand All @@ -50,7 +50,7 @@ class AVLTreeTests: XCTestCase {
self.tree?.autopopulateWithNodes(5)

for i in 1...6 {
self.tree?.delete(i)
self.tree?.delete(key: i)
do {
try self.tree?.inOrderCheckBalanced(self.tree?.root)
} catch _ {
Expand All @@ -68,7 +68,7 @@ class AVLTreeTests: XCTestCase {

func testSingleInsertionPerformance() {
self.measure {
self.tree?.insert(5, "E")
self.tree?.insert(key: 5, payload: "E")
}
}

Expand All @@ -80,14 +80,14 @@ class AVLTreeTests: XCTestCase {

func testSearchExistentOnSmallTreePerformance() {
self.measure {
self.tree?.search(2)
self.tree?.search(input: 2)
}
}

func testSearchExistentElementOnLargeTreePerformance() {
self.measure {
self.tree?.autopopulateWithNodes(500)
self.tree?.search(400)
self.tree?.search(input: 400)
}
}

Expand All @@ -106,19 +106,19 @@ class AVLTreeTests: XCTestCase {
}

func testDeleteExistentKey() {
self.tree?.delete(1)
XCTAssertNil(self.tree?.search(1), "Key should not exist anymore")
self.tree?.delete(key: 1)
XCTAssertNil(self.tree?.search(input: 1), "Key should not exist anymore")
}

func testDeleteNotExistentKey() {
self.tree?.delete(1056)
XCTAssertNil(self.tree?.search(1056), "Key should not exist")
self.tree?.delete(key: 1056)
XCTAssertNil(self.tree?.search(input: 1056), "Key should not exist")
}

func testInsertSize() {
let tree = AVLTree<Int, String>()
for i in 0...5 {
tree.insert(i, "")
tree.insert(key: i, payload: "")
XCTAssertEqual(tree.size, i + 1, "Insert didn't update size correctly!")
}
}
Expand All @@ -134,15 +134,15 @@ class AVLTreeTests: XCTestCase {
for p in permutations {
let tree = AVLTree<Int, String>()

tree.insert(1, "five")
tree.insert(2, "four")
tree.insert(3, "three")
tree.insert(4, "two")
tree.insert(5, "one")
tree.insert(key: 1, payload: "five")
tree.insert(key: 2, payload: "four")
tree.insert(key: 3, payload: "three")
tree.insert(key: 4, payload: "two")
tree.insert(key: 5, payload: "one")

var count = tree.size
for i in p {
tree.delete(i)
tree.delete(key: i)
count -= 1
XCTAssertEqual(tree.size, count, "Delete didn't update size correctly!")
}
Expand All @@ -154,7 +154,7 @@ extension AVLTree where Key : SignedInteger {
func autopopulateWithNodes(_ count: Int) {
var k: Key = 1
for _ in 0...count {
self.insert(k)
self.insert(key: k)
k = k + 1
}
}
Expand Down

0 comments on commit cfb23c4

Please sign in to comment.