简单地撸了个AVL树练练手
package main
import "fmt"
type AVLNode struct {
left *AVLNode
right *AVLNode
hight int
data int
}
func (this *AVLNode)FindMaxData() int {
if this == nil {
return 0
}
if this.right == nil {
return this.data
} else {
return this.right.FindMaxData()
}
}
func (this *AVLNode)FindMinData() int {
if this == nil {
return 0
}
if this.left == nil {
return this.data
} else {
return this.right.FindMinData()
}
}
func GetTreeHight(Node *AVLNode) int {
if Node == nil {
return 0
} else {
return Node.hight
}
}
func Max(a int, b int) int {
if a > b {
return a
} else {
return b
}
}
//单旋转,左旋
func (this *AVLNode) LL() *AVLNode {
Temp := this.right
this.right = Temp.left
Temp.left = this
this.hight = Max(GetTreeHight(this.left), GetTreeHight(this.right)) + 1
Temp.hight = Max(GetTreeHight(Temp.left), GetTreeHight(Temp.right)) + 1
return Temp
}
//单旋转,右旋
func (this *AVLNode) RR() *AVLNode {
Temp := this.left
this.left = Temp.right
Temp.right = this
this.hight = Max(GetTreeHight(this.left), GetTreeHight(this.right)) + 1
Temp.hight = Max(GetTreeHight(Temp.left), GetTreeHight(Temp.right)) + 1
return Temp
}
//双旋转,先右再左
func (this *AVLNode) RL() *AVLNode {
Temp := this.right.RR()
this.right = Temp
return this.LL()
}
//双旋转,先左再右
func (this *AVLNode) LR() *AVLNode {
Temp := this.left.LL()
this.left = Temp
return this.RR()
}
//平衡
func (this *AVLNode) adjust() *AVLNode {
if GetTreeHight(this.right) - GetTreeHight(this.left) == 2 {
if GetTreeHight(this.right.right) > GetTreeHight(this.right.left) {
return this.LL()
} else {
return this.RL()
}
} else if GetTreeHight(this.left) - GetTreeHight(this.right)== 2 {
if GetTreeHight(this.left.left) > GetTreeHight(this.left.right) {
return this.RR()
} else {
return this.LR()
}
}
return this
}
//插入节点
func (this *AVLNode) InsertNode(value int) *AVLNode {
if this == nil {
NewRootNode := AVLNode{
left:nil,
right:nil,
hight:1,
data:value}
return &NewRootNode
}
if value < this.data {
this.left = this.left.InsertNode(value)
this = this.adjust()
} else if value > this.data {
this.right = this.right.InsertNode(value)
this = this.adjust()
} else {
return nil
}
this.hight = Max(GetTreeHight(this.left), GetTreeHight(this.right)) + 1
return this
}
//查找节点
func (this *AVLNode)FindNode(value int) bool {
if this == nil {
return false
}
if value > this.data {
return this.right.FindNode(value)
} else if value < this.data {
return this.left.FindNode(value)
} else {
return true
}
}
//删除节点
func (this *AVLNode) DeleteNode(value int) *AVLNode {
if this == nil {
return this
}
if value < this.data {
this.left.DeleteNode(value)
} else if value > this.data {
this.right.DeleteNode(value)
} else {
if this.left != nil && this.right != nil {
this.data = this.right.FindMinData()
this.right = this.right.DeleteNode(this.data)
} else if this.left != nil {
this = this.left
} else {
this = this.right
}
}
if this != nil {
this.hight = Max(GetTreeHight(this.left) , GetTreeHight(this.right)) + 1
this = this.adjust()
}
return this
}
//中序遍历
func (this *AVLNode) InorderTraversal() {
if this != nil {
this.left.InorderTraversal()
fmt.Print(this.data, " ")
this.right.InorderTraversal()
}
}
//前序遍历
func (this *AVLNode) PreorderTraversal() {
if this != nil {
fmt.Print(this.data, " ")
this.left.PreorderTraversal()
this.right.PreorderTraversal()
}
}
//后序遍历
func (this *AVLNode) PostorderTraversal() {
if this != nil {
this.left.PostorderTraversal()
this.right.PostorderTraversal()
fmt.Print(this.data, " ")
}
}
func main() {
Root := AVLNode{
left:nil,
right:nil,
hight:1,
data:100}
Root.InsertNode(120)
Root.InsertNode(80)
Root.InsertNode(150)
Root.InsertNode(200)
Root.InorderTraversal()
fmt.Println("\n")
Root.DeleteNode(150)
Root.InorderTraversal()
}