AVL树算法实现

avl树算法资料收集

https://img-blog.csdn.net/20180515195752481

实现代码

  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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main

import (
	"fmt"
)

var nodeCnt int

type Node struct {
	val         int
	left, right *Node
	h           int
}

func max(i, j int) int {
	if i > j {
		return i
	}
	return j
}
func min(i, j int) int {
	if i < j {
		return i
	}
	return j
}
func (node *Node) height() int {
	if node == nil {
		return 0
	}
	return node.h
}

// 对节点y进行向右旋转操作,返回旋转后新的根节点x
//        y                              x
//       / \                           /   \
//      x   T4     向右旋转 (y)        z     y
//     / \       - - - - - - - ->    / \   / \
//    z   T3                       T1  T2 T3 T4
//   / \
// T1   T2
func adjustLeft(y *Node) *Node {
	if y == nil {
		panic("invalid node")
	}
	var x = y.left
	y.left = x.right
	x.right = y
	y.h = max(y.left.height(), y.right.height()) + 1
	x.h = max(x.left.height(), x.right.height()) + 1
	return x
	//var leftChild = this.left
	//this.left = this.right
	//leftChild.right = this
	//this.h = max(this.left.height(), this.right.height()) + 1
	//leftChild.h = max(leftChild.left.height(), leftChild.right.height()) + 1
	//return leftChild
}

/*
	  y                =>          x
   a      x                      y
        b    c                     xxxxxx
           e   d
*/

// right 重,左旋转
func adjustRight(y *Node) *Node {
	if y == nil {
		panic("invalid node")
	}
	var x = y.right
	// x  >= y
	y.right = x.left
	x.left = y
	y.h = max(y.left.height(), y.right.height()) + 1
	x.h = max(x.left.height(), x.right.height()) + 1
	return x

}

// left 重, left.right 重
func adjustLeftRight(node *Node) *Node {
	//左子树的右节点
	node.left = adjustRight(node.left)
	// left left rotation
	return adjustLeft(node)
}

// right 重,并且 right.left 重
func adjustRightLeft(node *Node) *Node {
	node.right = adjustLeft(node.right)

	return adjustRight(node)
}

func insertNode(node *Node, val int) *Node {
	if node == nil {
		nodeCnt++
		return &Node{val: val, h: 1}
	}
	switch {
	case node.val < val:
		// current < val , to right find
		node.right = insertNode(node.right, val)
		// 插入 右边节点,只能是  height(right) >= height(left)
		if node.right.height()-node.left.height() == 2 {
			// adjust right
			if val > node.right.val {
				// right 重,right.right也重
				node = adjustRight(node)
			} else {
				// right 重,right.left重
				node = adjustRightLeft(node)
			}
		}
	case node.val > val:
		// 去插入到左边节点
		node.left = insertNode(node.left, val)
		if node.left.height()-node.right.height() == 2 {
			// 判断 left.val < val
			if node.left.val > val {
				// left重,left.left也重, 处理 ll
				node = adjustLeft(node)
			} else {
				//left, left.right重
				node = adjustLeftRight(node)
			}
		}
	default:
		//log.Println("exists")

	}
	node.h = max(node.left.height(), node.right.height()) + 1
	return node
}
func findMinNode(node *Node) *Node {
	if node == nil {
		return nil
	}
	for node.left != nil {
		node = node.left
	}
	return node
}
func findMaxNode(node *Node) *Node {
	if node == nil {
		return nil
	}
	for node.right != nil {
		node = node.right

	}
	return node
}

// >= val => 天花板,下边界
func findFloorNode(node *Node, val int) *Node {
Loop:
	for node != nil {
		if node.val == val {
			//return node
			break Loop
		} else if node.val < val {
			// to right
			node = node.right
		} else {
			//to left, if left is nil , return current Node
			//node = node.right
			left := findFloorNode(node.left, val)
			if left != nil && left.val < node.val {
				node = left
			}
			break Loop
			// left is small

		}
	}
	return node
}

// <= val 的最大节点
func findCeilNode(node *Node, val int) *Node {
	// 早到 比 val 小的
	if node == nil {
		return nil
	}
find:
	for node != nil {
		if node.val == val {
			break find
		} else if node.val > val {
			// 比他大 , 左边 的 可能也比他大
			node = node.left
		} else {
			// node.val < val , to right find  <= val first
			var s = findCeilNode(node.right, val)
			if s != nil && s.val > node.val {
				node = s
			}
			break find

		}
	}
	return node
}
func adjustNode(node *Node) *Node {
	// adjust node
	switch node.left.height() - node.right.height() {
	case 2:
		// left 重
		//var left = node.left
		if node.left.height() > node.right.height() {
			node = adjustLeft(node)
		} else {
			node = adjustLeftRight(node)
		}

	case -2:
		if node.right.right.height() > node.right.left.height() {
			// right right 重
			node = adjustRight(node)
		} else {
			node = adjustRightLeft(node)
		}

	}
	return node
}

func deleteNode(node *Node, val int) *Node {
	if node == nil {
		return nil
	}
	if node.val < val {
		node.right = deleteNode(node.right, val)
	} else if node.val > val {
		// to left delete
		node.left = deleteNode(node.left, val)
	} else {
		// delete current
		nodeCnt--
		if node.left == nil {
			if node.right == nil {
				//delete one
				node = nil
			} else {
				// return node.right
				node = node.right
			}
		} else {
			if node.right == nil {
				//node.left != nil
				node = node.left
			} else {
				// node.left != nil  node.right !=nil
				//前驱节点
				var nextNode = findMinNode(node.right)
				node.val = nextNode.val
				//委托 删除 minNode ,  这边不好删除,直接 取巧 ,覆盖值即可
				node.right = deleteNode(node.right, nextNode.val)
			}
		}
	}
	if node != nil {
		//跳转高度
		node.h = max(node.left.height(), node.right.height()) + 1
		node = adjustNode(node)
	}
	return node
}

//func rightRotationNode(node *Node) *Node {
//	var right = node.right
//	node.right = node.left
//	node
//}
func ascPrint(node *Node) {
	if node == nil {
		return
	}
	ascPrint(node.left)
	fmt.Printf("%v,", node.val)
	ascPrint(node.right)
}
func main() {
	var node *Node
	//node = insertNode(node,2)
	//fmt.Println(node)
	//
	node = insertNode(node, 1)
	node = insertNode(node, 5)
	node = insertNode(node, 9)
	node = insertNode(node, 1)
	node = insertNode(node, 5)
	//node = insertNode(node,2)
	node = insertNode(node, 9)
	//node = deleteNode(node,1)
	ascPrint(node)
	fmt.Println()
	node = deleteNode(node, 4)
	ascPrint(node)
	fmt.Println()
	// <= 1
	fmt.Println(findCeilNode(node, 5-3).val)
	//q := make([]*Node, 0)
	//q = append(q, node)
	//for len(q) > 0 {
	//	var n = len(q)
	//	for i := 0; i < n; i++ {
	//		var res = q[0]
	//		q = q[1:]
	//		log.Printf("%+v,", res.val)
	//		if res.left != nil {
	//			q = append(q, res.left)
	//		}
	//		if res.right != nil {
	//			q = append(q, res.right)
	//		}
	//	}
	//	log.Println("----------------------------------------------")
	//}
	//fmt.Println("node count = ",nodeCnt)

}