二叉平衡树旋转

二叉平衡树旋转

二叉平衡树操作

添加

二叉平衡树的添加节点,首先找到对应位置增加节点,其次查看左右子树的高度差是否符合标准,不然则调整。

删除

二叉平衡树删除,首先和二叉排序树同样的规则,删除对应节点,主要考虑一下三点情况

  • 删除节点是叶子节点,则直接删除
  • 删除节点有左或者右节点的存在,则将其存在的节点继承该删除节点的位置,则满足二叉排序树性质
  • 删除节点存在左右节点,则找到其后继节点,替换后继节点的值,删除后继节点(也可使用前驱节点操作)

其次删除后向上查看是否符合左右子树平衡标准,不然则调整。

整体代码以及单测

  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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package AVLT

type AVLT struct {
	value int
	h int
	left *AVLT
	right *AVLT
}

func InitAVLT(v int, h int) *AVLT{
	t := AVLT{
		value:v,
		h:h,
		left:nil,
		right:nil,
	}
	return &t
}

func FindMax(root *AVLT) *AVLT{
	if root == nil {
		return nil
	}
	if root.right == nil {
		return root
	}
	return FindMax(root.right)
}

func FindMin(root *AVLT) *AVLT{
	if root == nil {
		return nil
	}
	if root.left == nil {
		return root
	}
	return FindMin(root.left)
}

func GetHeight(root *AVLT) int {
	if root == nil {
		return 0
	}
	return root.h
}

func LL(root *AVLT) *AVLT{
	tmp := root.left
	root.left = tmp.right
	root.h = max(GetHeight(root.left),GetHeight(root.right)) + 1
	tmp.right = root
	root = tmp
	tmp.h = max(GetHeight(tmp.left),GetHeight(tmp.right)) + 1
	return tmp
}

func RR(root *AVLT) *AVLT{
	tmp := root.right
	root.right = tmp.left
	root.h = max(GetHeight(root.left),GetHeight(root.right)) + 1
	tmp.left = root
	root = tmp
	tmp.h = max(GetHeight(tmp.left),GetHeight(tmp.right)) + 1
	return tmp
}

func LR(root *AVLT) *AVLT{
	//tmp := root.left.RR()
	//root.left = tmp
	RR(root.left)
	return LL(root)
}

func RL(root *AVLT) *AVLT{
	//tmp := root.right.LL()
	//root.right = tmp
	LL(root.right)
	return RR(root)
}

func Insert(root *AVLT,x int) *AVLT{
	if root == nil {
		root = InitAVLT(x,1)
	} else if x < root.value {
		root.left = Insert(root.left,x)
		if GetHeight(root.left) - GetHeight(root.right) > 1 {
			if x < root.left.value {
				root = LL(root)
			} else {
				root = LR(root)
			}
		}
	} else if root.value < x {
		root.right = Insert(root.right,x)
		if GetHeight(root.right) - GetHeight(root.left) > 1 {
			if x > root.right.value {
				root = RR(root)
			} else {
				root = RL(root)
			}
		}
	}
	root.h = max(GetHeight(root.left),GetHeight(root.right)) + 1
	return root
}

func Delete(root *AVLT,x int) *AVLT{
	if root == nil {
		return nil
	} else if root.value == x {
		if root.left != nil && root.right != nil {
			if GetHeight(root.left) > GetHeight(root.right) {
				root.value = FindMax(root.left).value
				root.left = Delete(root.left,root.value)
			} else {
				root.value = FindMin(root.right).value
				root.right = Delete(root.right,root.value)
			}
		} else {
			if root.left != nil {
				root = root.left
			} else if root.right != nil {
				root = root.right
			} else {
				root = nil
			}
		}
	} else if root.value > x {
		root.left = Delete(root.left,x)
		if GetHeight(root.right) - GetHeight(root.left) > 1 {
			if GetHeight(root.right.left) > GetHeight(root.right.right) {
				root = RL(root)
			} else {
				root = RR(root)
			}
		} else {
			root.h = max(GetHeight(root.left),GetHeight(root.right)) + 1
		}
	} else {
		root.right = Delete(root.right,x)
		if GetHeight(root.left) - GetHeight(root.right) > 1 {
			if GetHeight(root.left.left) < GetHeight(root.left.right) {
				root = LR(root)
			} else {
				root = LL(root)
			}
		} else {
			root.h = max(GetHeight(root.left),GetHeight(root.right)) + 1
		}
	}
	return root;
}

func max(a,b int) int{
	if a<b {
		return b
	}
	return a
}

func min(a,b int) int {
	if a<b {
		return a
	}
	return b
}

func abs(a,b int) int {
	t1 := a-b
	if t1<0 {
		t1 = -1 * t1
	}
	t2 := b-a
	if t2<0 {
		t2 = -1 * t2
	}
	return max(t1,t2) } //===================单测=================
package AVLT

import (
	"fmt"
	"math/rand"
	"testing"
	"time"
)


func TestInsert(t *testing.T) {
	a := []int{1, 2, 3, 4, 5, 6, 7, 8}
	root := InitAVLT(0, 1)
	for i, _ := range a {
		root = Insert(root, a[i])
	}
	PrintAVLT(root,4)
}

func TestDelete(t *testing.T) {
	a := []int{1, 2, 3, 4, 5, 6, 7, 8}
	ra := []int{0,1, 2, 3, 4, 5, 6, 7, 8}
	root := InitAVLT(0, 1)
	for i, _ := range a {
		root = Insert(root, a[i])
	}
	ra = getRandomArray(ra)
	for i,_ := range ra {
		root  = Delete(root,ra[i])
		//PrintAVLT(root,4)
		PrintAVLTNoHeight(root,4)
		fmt.Println("===============")
	}
}

func PrintAVLT(root *AVLT, C int) {
	if root == nil {
		fmt.Println("weng")
		return
	}
	var q []*AVLT
	tmp := 1
	q = append(q,root)
	j := 0
	c := 0
	for j < len(q) && c < C{
		for i:=0;i<tmp && j+i < len(q);i++{
			if q[j+i].h == -2 {
				fmt.Printf("x,x  ")
			} else {
				fmt.Printf("%d,%d  ",q[j+i].value,q[j+i].h)
			}
			if q[j+i].left != nil && q[j+i].right != nil{
				q = append(q,q[j+i].left)
				q = append(q,q[j+i].right)
			} else if q[j+i].left == nil && q[j+i].right != nil{
				q = append(q,&AVLT{0,-2,nil,nil})
				q = append(q,q[j+i].right)
			} else if q[j+i].left != nil && q[j+i].right == nil{
				q = append(q,q[j+i].left)
				q = append(q,&AVLT{0,-2,nil,nil})
			} else  {
				q = append(q,&AVLT{0,-2,nil,nil})
				q = append(q,&AVLT{0,-2,nil,nil})
			}
		}
		fmt.Println()
		j += tmp
		tmp = tmp * 2
		c += 1
	}
}

func PrintAVLTNoHeight(root *AVLT, C int) {
	if root == nil {
		fmt.Println("weng")
		return
	}
	var q []*AVLT
	tmp := 1
	q = append(q,root)
	j := 0
	c := 0
	for j < len(q) && c < C{
		for i:=0;i<tmp && j+i < len(q);i++{
			if q[j+i].h == -2 {
				fmt.Printf("x  ")
			} else {
				fmt.Printf("%d  ",q[j+i].value)
			}
			if q[j+i].left != nil && q[j+i].right != nil{
				q = append(q,q[j+i].left)
				q = append(q,q[j+i].right)
			} else if q[j+i].left == nil && q[j+i].right != nil{
				q = append(q,&AVLT{0,-2,nil,nil})
				q = append(q,q[j+i].right)
			} else if q[j+i].left != nil && q[j+i].right == nil{
				q = append(q,q[j+i].left)
				q = append(q,&AVLT{0,-2,nil,nil})
			} else  {
				q = append(q,&AVLT{0,-2,nil,nil})
				q = append(q,&AVLT{0,-2,nil,nil})
			}
		}
		fmt.Println()
		j += tmp
		tmp = tmp * 2
		c += 1
	}
}

func getRandomArray(vs []int) []int {
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < 6; i++ {
		ri := rand.Intn(6)
		tmp := vs[i]
		vs[i] = vs[ri]
		vs[ri] = tmp
	}
	return vs
}