-
Notifications
You must be signed in to change notification settings - Fork 0
/
avltree.go
166 lines (143 loc) · 2.94 KB
/
avltree.go
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
159
160
161
162
163
164
165
166
package sstable
import (
"fmt"
"math"
)
type AVLNode struct {
Key string
Height int
Lhs, Rhs *AVLNode
}
func setHeight(node *AVLNode) {
if node.Lhs == nil && node.Rhs == nil {
node.Height = 1
return
}
if node.Lhs == nil && node.Rhs != nil {
node.Height = node.Rhs.Height + 1
return
}
if node.Rhs == nil && node.Lhs != nil {
node.Height = node.Lhs.Height + 1
return
}
node.Height = int(math.Max(float64(node.Lhs.Height), float64(node.Rhs.Height))) + 1
}
func Insert(node *AVLNode, key string) *AVLNode {
if node == nil {
// TODO: Height0のほうが良いかも
return &AVLNode{Key: key, Height: 1}
}
if key < node.Key {
node.Lhs = Insert(node.Lhs, key)
} else if key > node.Key {
node.Rhs = Insert(node.Rhs, key)
}
setHeight(node)
r := rebalance(node)
return r
}
func isBalanced(node *AVLNode) bool {
if node.Lhs == nil {
return node.Rhs.Height <= 1
}
if node.Rhs == nil {
return node.Lhs.Height <= 1
}
return int(math.Abs(float64(node.Lhs.Height)-float64(node.Rhs.Height))) <= 1
}
func isLHeavy(node *AVLNode) bool {
if node.Lhs == nil {
return false
}
if node.Rhs == nil {
return node.Lhs.Height >= 1
}
return node.Lhs.Height-node.Rhs.Height >= 2
}
func bias(node *AVLNode) int {
if node.Lhs == nil {
return -node.Rhs.Height
}
if node.Rhs == nil {
return node.Lhs.Height
}
return node.Lhs.Height - node.Rhs.Height
}
func rotateL(node *AVLNode) *AVLNode {
newRoot := node.Rhs
node.Rhs = node.Rhs.Lhs
newRoot.Lhs = node
setHeight(newRoot.Lhs)
setHeight(newRoot)
return newRoot
}
func rotateR(node *AVLNode) *AVLNode {
newRoot := node.Lhs
node.Lhs = node.Lhs.Rhs
newRoot.Rhs = node
setHeight(newRoot.Rhs)
setHeight(newRoot)
return newRoot
}
func rebalance(node *AVLNode) *AVLNode {
if isBalanced(node) {
return node
}
if isLHeavy(node) {
if bias(node) == 2 && bias(node.Lhs) == -1 {
node.Lhs = rotateL(node.Lhs)
r := rotateR(node)
return r
}
if bias(node) == 2 && bias(node.Lhs) == 1 {
r := rotateR(node)
return r
}
} else {
// Here is rigtht heavy branch.
if bias(node) == -2 && bias(node.Rhs) == -1 {
r := rotateL(node)
return r
}
if bias(node) == -2 && bias(node.Rhs) == 1 {
node.Rhs = rotateR(node.Rhs)
r := rotateL(node)
return r
}
return nil
}
panic("something wrong")
}
func Print(root *AVLNode, indent int) {
if root != nil {
indent++
Print(root.Rhs, indent)
fmt.Printf("%*s", indent*2, "")
fmt.Printf("|%s\n", root.Key)
Print(root.Lhs, indent)
}
}
var tree [][]string
func PrintVertical(root *AVLNode) {
tree = [][]string{}
genTree(root, 0, false)
for _, t := range tree {
fmt.Printf("%s\n", t)
}
}
func genTree(root *AVLNode, depth int, isRhs bool) {
if root == nil {
return
}
for len(tree) <= depth {
tree = append(tree, []string{})
}
tree[depth] = append(tree[depth], root.Key)
if isRhs {
tree[depth] = append(tree[depth], "|")
}
depth++
genTree(root.Lhs, depth, false)
genTree(root.Rhs, depth, true)
}