golang ----------- 平衡二叉树-AVL树

平衡二叉树的旋转:有4类,每类中又有两类

1、左旋转:当插入的结点为失衡点的右子树的右子树中(左右孩子都行),如下图所示,A,B是同一种情况,虚线部分表示不存在。这两种情况都是左旋转,代码一样(A中140为新插入的点,B中130为新插入的点)

《golang ----------- 平衡二叉树-AVL树》

2、先右旋转,再左旋转:当插入的结点在失衡点的右子树的左子树中(左右孩子都行),如下图所示,A,B是同一种情况,代码一样

《golang ----------- 平衡二叉树-AVL树》

3、右旋转:当插入的结点为失衡点的左子树的左子树中,如下图所示,A,B是同一种情况,虚线部分表示不存在。这两种情况都是右旋转,代码一样

《golang ----------- 平衡二叉树-AVL树》

4、先左旋转,再右旋转:当插入的结点为失衡点的左子树的右子树中(左右孩子都行),如下图所示,A,B是同一种情况,代码一样

《golang ----------- 平衡二叉树-AVL树》

代码:

package main

import "fmt"

type AVL struct {
	value int      //值
	height int     //深度
	left *AVL      //左子树
	right *AVL     //右子树
}

//查找元素
func (t *AVL) Search(value int) bool {
	if t == nil {
		return false
	}
	compare := value - t.value
	if compare < 0 {
		return t.left.Search(value)
	}else if compare > 0 {
		return t.right.Search(value)
	}else {
		return true
	}
}



func (t *AVL) leftRotate() *AVL {  //左旋转
	headNode := t.right
	t.right = headNode.left
	headNode.left = t
    //更新结点高度
    t.height = max(t.left.getHeight(),t.right.getHeight()) + 1
    headNode.height = max(headNode.left.getHeight(),headNode.right.getHeight()) + 1
    return headNode
}

func (t *AVL) rightRotate() *AVL {  //右旋转
	headNode := t.left
	t.left = headNode.right
	headNode.right = t
	//更新结点高度
	t.height = max(t.left.getHeight(),t.right.getHeight()) +1
	headNode.height = max(headNode.left.getHeight(),headNode.right.getHeight()) + 1
	return headNode
}

func (t *AVL) rightThenLeftRotate() *AVL {  //右旋转,之后左旋转
	//以失衡点右结点先右旋转
	sonHeadNode := t.right.rightRotate()
	t.right = sonHeadNode
	//再以失衡点左旋转
	return t.leftRotate()
}

func (t *AVL) LeftThenRightRotate() *AVL {  //左旋转,之后右旋转
	//以失衡点左结点先左旋转
	sonHeadNode := t.left.leftRotate()
	t.left = sonHeadNode
	//再以失衡点左旋转
	return t.rightRotate()
}

func (t *AVL) adjust() *AVL {
	if t.right.getHeight() - t.left.getHeight() == 2 {
		if t.right.right.getHeight() > t.right.left.getHeight() {
			t = t.leftRotate()
		}else {
			t = t.rightThenLeftRotate()
		}
	}else if t.left.getHeight() - t.right.getHeight() == 2 {
		if t.left.left.getHeight() > t.left.right.getHeight() {
			t = t.rightRotate()
		} else {
			t = t.LeftThenRightRotate()
		}
	}
	return t
}

//添加元素
func (t *AVL) Insert(value int) *AVL {
	if t == nil {
		newNode := AVL{value,1,nil,nil}
		return &newNode
	}
	if value < t.value {
		t.left = t.left.Insert(value)
		t = t.adjust()
	}else if value > t.value{
		t.right = t.right.Insert(value)
		t = t.adjust()
	}else {
		fmt.Println("the node exit")
	}
	t.height = max(t.left.getHeight(),t.right.getHeight()) + 1
	return t
}


/*删除元素
*1、如果被删除结点只有一个子结点,就直接将A的子结点连至A的父结点上,并将A删除
*2、如果被删除结点有两个子结点,将该结点右子数内的最小结点取代A。
*3、查看是否平衡,该调整调整
*/
func (t *AVL) Delete(value int) *AVL {
	if t ==nil {
		return t
	}
	compare := value - t.value
	if compare < 0 {
		t.left = t.left.Delete(value)
	}else if compare > 0{
		t.right = t.right.Delete(value)
	}else { //找到结点,删除结点()
		if t.left != nil && t.right != nil {
			t.value = t.right.getMin()
			t.right = t.right.Delete(t.value)
		} else if t.left !=nil {
			t = t.left
		}else {//只有一个右孩子或没孩子
			t = t.right
		}
	}
	if t != nil {
		t.height = max(t.left.getHeight(),t.right.getHeight()) + 1
		t = t.adjust()
	}
	return t
}

//按顺序获得树中元素
func (t *AVL) getAll() []int {
	values := []int{}
	return addValues(values,t)
}

//将一个节点加入切片中
func addValues(values []int,t *AVL) []int {
	if t != nil {
		values = addValues(values,t.left)
		values = append(values,t.value)
		fmt.Println(t.value,t.height)
		values = addValues(values,t.right)
	}
	return values
}

//查找子树最小值
func (t *AVL) getMin() int {
	if t == nil {
		return -1
	}
	if t.left == nil {
		return t.value
	} else {
		return t.left.getMin()
	}
}

//查找子树最大值
func (t *AVL) getMax() int {
	if t == nil {
		return -1
	}
	if t.right == nil {
		return t.value
	} else {
		return t.right.getMax()
	}
}

//查找最小结点
func (t *AVL) getMinNode() *AVL {
	if t == nil {
		return nil
	}else {
		for t.left != nil {
			t = t.left
		}
	}
	return t
}

//查找最大结点
func (t *AVL) getMaxNode() *AVL {
	if t == nil {
		return nil
	}else {
		for t.right != nil {
			t = t.right
		}
	}
	return t
}

//得到树高
func (t *AVL) getHeight() int {
	if t == nil {
		return 0
	}
	return t.height
}

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


func main() {
	bsTree := AVL{100,1,nil,nil}
	newTree := bsTree.Insert(60)
	newTree = bsTree.Insert(120)
	newTree = bsTree.Insert(110)
	newTree = bsTree.Insert(130)
	newTree = bsTree.Insert(105)
	fmt.Println(newTree.getAll())

	newTree.Delete(110)
	fmt.Println(newTree.getAll())

}


    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/qq_36183935/article/details/80315808
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞