Skip to content

Commit 4ed133f

Browse files
committed
Expose root and balance of AVLTree publically
1 parent 2f96bbb commit 4ed133f

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Sources/DataStructures/AVLTree.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ public struct AVLTree <Key: Comparable, Value> {
1212

1313
// MARK: - Instance Properties
1414

15-
@usableFromInline
16-
var root: Node?
15+
/// The root node of this `AVLTree`.
16+
public var root: Node?
1717

1818
/// Creates an `AVLTree` with the given `root` node.
19-
@usableFromInline
20-
init(root: Node? = nil) {
19+
@inlinable
20+
public init(root: Node? = nil) {
2121
self.root = root
2222
}
2323
}
@@ -139,7 +139,9 @@ extension AVLTree {
139139
return balance(node, with: key)
140140
}
141141

142-
static func balance(_ node: Node, with key: Key) -> Node {
142+
/// Balances an `AVLTree` to ensure that leaf no path is no more than one longer than any other.
143+
@inlinable
144+
public static func balance(_ node: Node, with key: Key) -> Node {
143145
node.updateHeight()
144146
let balance = balanceFactor(node.left, node.right)
145147
if balance > 1 && key < node.left!.key {
@@ -158,10 +160,12 @@ extension AVLTree {
158160

159161
// TODO: removeValue(forKey key: Key) -> Node?
160162

163+
@usableFromInline
161164
static func balanceFactor(_ left: Node?, _ right: Node?) -> Int {
162165
return (left?.height ?? -1) - (right?.height ?? -1)
163166
}
164167

168+
@usableFromInline
165169
static func rotateLeft(_ node: Node) -> Node {
166170
guard let newRoot = node.right else { return node }
167171
node.right = newRoot.left
@@ -171,6 +175,7 @@ extension AVLTree {
171175
return newRoot
172176
}
173177

178+
@usableFromInline
174179
static func rotateRight(_ node: Node) -> Node {
175180
guard let newRoot = node.left else { return node }
176181
node.left = newRoot.right
@@ -183,6 +188,7 @@ extension AVLTree {
183188

184189
extension AVLTree.Node {
185190

191+
@usableFromInline
186192
func updateHeight() {
187193
self.height = max(left?.height ?? 0, right?.height ?? 0) + 1
188194
}

0 commit comments

Comments
 (0)