golang实现AVL树

栏目: Go · 发布时间: 7年前

内容简介:AVL树是一棵高度平衡的二叉搜索树,它的特点是:1.本身首先是一棵二叉搜索树。2.带有平衡条件:每个结点的左右子树的高度之差的绝对值最多为1。

AVL树是一棵高度平衡的二叉搜索树,它的特点是:

1.本身首先是一棵二叉搜索树。

2.带有平衡条件:每个结点的左右子树的高度之差的绝对值最多为1。

不废话直接上代码(golang实现):

package main

import (
    "log"
)

func main() {
    array := []int{5, 3, 1, 8, 9, 10, 11, 2, 4, 7, 6, 12}
    var root *AVLTreeNode
    for _, v := range array {
        root = root.InsertNode(v)
        log.Println("root=> key:", root.value, "high:", root.high)
    }
    log.Println("-----------------")
    root.PrintSortTree()
}

type AVLTreeNode struct {
    value int
    high  int
    left  *AVLTreeNode
    right *AVLTreeNode
}

func NewAVLTreeRoot(root int) *AVLTreeNode {
    return &AVLTreeNode{root, 0, nil, nil}
}

func (this *AVLTreeNode) InsertNode(v int) *AVLTreeNode {
    if this == nil {
        return &AVLTreeNode{v, 0, nil, nil}
    }
    if v < this.value {
        this.left = this.left.InsertNode(v)
        this.high = getMax(this.left.getNodeHigh(), this.right.getNodeHigh()) + 1
        if this.left.getNodeHigh()-this.right.getNodeHigh() == 2 {
            if v < this.left.value {
                return this.rightRotation()
            } else {
                return this.leftRightRotation()
            }
        }
    } else {
        this.right = this.right.InsertNode(v)
        this.high = getMax(this.left.getNodeHigh(), this.right.getNodeHigh()) + 1
        if this.right.getNodeHigh()-this.left.getNodeHigh() == 2 {
            if v < this.right.value {
                return this.rightLeftRotation()
            } else {
                return this.leftRotation()
            }
        }
    }
    return this
}

func (this *AVLTreeNode) leftRotation() *AVLTreeNode {
    node := this.right
    this.right = node.left
    node.left = this
    this.high = getMax(this.left.getNodeHigh(), this.right.getNodeHigh()) + 1
    node.high = getMax(node.left.getNodeHigh(), node.right.getNodeHigh()) + 1
    return node
}

func (this *AVLTreeNode) rightRotation() *AVLTreeNode {
    node := this.left
    this.left = node.right
    node.right = this
    this.high = getMax(this.left.getNodeHigh(), this.right.getNodeHigh()) + 1
    node.high = getMax(node.left.getNodeHigh(), node.right.getNodeHigh()) + 1
    return node
}

func (this *AVLTreeNode) leftRightRotation() *AVLTreeNode {
    this.left = this.left.leftRotation()
    return this.rightRotation()
}

func (this *AVLTreeNode) rightLeftRotation() *AVLTreeNode {
    this.right = this.right.rightRotation()
    return this.leftRotation()
}

func (this *AVLTreeNode) getNodeHigh() int {
    if this == nil {
        return -1
    }
    return this.high
}

func (this *AVLTreeNode) PrintSortTree() {
    if this == nil {
        return
    }
    this.left.PrintSortTree()
    log.Println(this.value)
    this.right.PrintSortTree()
}

func getMax(a, b int) int {
    if a > b {
        return a
    }
    return b
}

以上所述就是小编给大家介绍的《golang实现AVL树》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

追踪Linux TCP/IP代码运行

追踪Linux TCP/IP代码运行

秦健 / 北京航空航天大学出版社 / 2010-4-1 / 69.00元

本书以应用程序为线索,详细描述了数据包在协议栈的分段、重组、发送、接收过程,同时分析了路由的初始化和设置过程,主要包括socket应用程序、 TCP/IP协议、路由、通知链、邻居子系统等内容。全书涵盖了协议栈的全部知识点,对于广大的读者来说这是一本极其难得的技术资料。同时,书中论述了网络设备的工作原理,解释了RTL8169和嵌入式CS8900、DM9000网卡设备的核心过程。一起来看看 《追踪Linux TCP/IP代码运行》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具