-
Notifications
You must be signed in to change notification settings - Fork 0
/
1066.swift
158 lines (139 loc) · 4.38 KB
/
1066.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import Foundation
class AVLTree<T: Comparable> {
enum Balance {
case LEFT_HEAVY, LEFT_HEAVY_BUT_BALANCED, COMPLETE_BLANCED, RIGHT_HEAVY_BUT_BALANCED, RIGHT_HEAVY
}
class Node {
var parent, left, right: Node?
var height = 1
var key: T
init(withKey key: T) {
self.key = key
}
var balanceFactor: Balance {
switch (left?.height ?? 0) - (right?.height ?? 0) {
case 2...:
return .LEFT_HEAVY
case 1:
return .LEFT_HEAVY_BUT_BALANCED
case 0:
return .COMPLETE_BLANCED
case -1:
return .RIGHT_HEAVY_BUT_BALANCED
default:
return .RIGHT_HEAVY
}
}
var isLeftChild: Bool {
return self === parent?.left
}
var isRightChild: Bool {
return self === parent?.right
}
func rotateLeft() {
let oldRoot = self, newRoot = right
newRoot?.left?.parent = oldRoot
oldRoot.right = newRoot?.left
if oldRoot.isLeftChild {
oldRoot.parent?.left = newRoot
} else if oldRoot.isRightChild {
oldRoot.parent?.right = newRoot
}
newRoot?.parent = oldRoot.parent
oldRoot.parent = newRoot
newRoot?.left = oldRoot
oldRoot.updateHeight()
newRoot?.updateHeight()
}
func rotateRight() {
let oldRoot = self, newRoot = left
newRoot?.right?.parent = oldRoot
oldRoot.left = newRoot?.right
if oldRoot.isLeftChild {
oldRoot.parent?.left = newRoot
} else if oldRoot.isRightChild {
oldRoot.parent?.right = newRoot
}
newRoot?.parent = oldRoot.parent
oldRoot.parent = newRoot
newRoot?.right = oldRoot
oldRoot.updateHeight()
newRoot?.updateHeight()
}
func updateHeight() {
height = max(left?.height ?? 0, right?.height ?? 0) + 1
}
func repair() {
switch balanceFactor {
case .LEFT_HEAVY:
switch left!.balanceFactor {
case .LEFT_HEAVY_BUT_BALANCED:
rotateRight()
case .RIGHT_HEAVY_BUT_BALANCED:
left?.rotateLeft()
rotateRight()
default:
break
}
case .RIGHT_HEAVY:
switch right!.balanceFactor {
case .RIGHT_HEAVY_BUT_BALANCED:
rotateLeft()
case .LEFT_HEAVY_BUT_BALANCED:
right?.rotateRight()
rotateLeft()
default:
break
}
default:
break
}
updateHeight()
parent?.repair()
}
}
var root: Node? = nil
private func updateRoot(withNode node: Node?) {
root = node
while let newRoot = root?.parent {
root = newRoot
}
}
func insert(withKey key: T) {
let newNode = Node(withKey: key)
if var current = root {
while true {
if key < current.key {
if let child = current.left {
current = child
} else {
current.left = newNode
newNode.parent = current
break
}
} else if key > current.key {
if let child = current.right {
current = child
} else {
current.right = newNode
newNode.parent = current
break
}
} else {
return
}
}
} else {
root = newNode
}
newNode.repair()
updateRoot(withNode: newNode)
}
}
let n = Int(readLine()!)!
let list = readLine()!.split(separator: " ").map{ Int(String($0))! }
var tree = AVLTree<Int>()
for i in list {
tree.insert(withKey: i)
}
print(tree.root?.key ?? 0)