Golang 二叉平衡树

简单地撸了个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()
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值