Golang Avltree Source

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
Here Golang no generics, code overlaps too many package baseimport ("FMT" "math") type (avlbittree struct {data int lchild *avlbitt REE rchild *avlbittree BT int} Iavlbittree interface {less (int) bool Equal (int) bool}) func (t His *avlbittree) less (data int) bool{return Data < This.data}func (this *avlbittree) Equal (data int) bool{return data = = This.data}func Getavlheight (tree *avlbittree) int{if tree = = nil{return 0} return int (math. Max (Float64 (Getavlheight (Tree.lchild)), Float64 (Getavlheight (tree.rchild))) +1}func Deleteavltree (tree * Avlbittree, data int) *avlbittree{tree1: = &tree Delteavl (tree, tree, tree1, data) return *tree1}func INSERTAVL Bittree (tree *avlbittree, data int) *avlbittree {tree1: = &tree Insertavl (tree, &tree, data) return *TREE1 }func r_rorate (tree *avlbittree, root **avlbittree) *avlbittree{p: = tree.lchild Tree.lchild = p.rchild P.rchild = t Ree p.bt = getavlheight (p.lchild)-GetavlheighT (p.rchild) tree.bt = Getavlheight (tree.lchild)-Getavlheight (tree.rchild) if tree = = *root{*root = p} tre E = P return tree}func l_rorate (tree *avlbittree, root **avlbittree) *avlbittree{p: = tree.rchild tree.rchild = p.lc Hild P.lchild = Tree p.bt = getavlheight (p.lchild)-Getavlheight (p.rchild) tree.bt = Getavlheight (tree.lchild)-Ge Tavlheight (tree.rchild) if tree = = *root{*root = p} tree = P return tree}func lr_rorate (tree *avlbittree, R Oot **avlbittree) *avlbittree{tree.lchild = L_rorate (tree.lchild, Root) return r_rorate (tree, Root)}func rl_rorate (tre e *avlbittree, Root **avlbittree) *avlbittree{Tree.rchild = R_rorate (tree.rchild, Root) return l_rorate (tree, Root)}fu NC middleavlprint (tree *avlbittree) {if tree! = nil{Middleavlprint (tree.lchild) fmt. Println (Tree.data, tree) middleavlprint (tree.rchild)}}func Blanceavl (tree* avlbittree,root **avlbittree, data int) {//fmt. Println (tree) tree.bt = Getavlheight (Tree.lchild)-Getavlheight (tree.rchild) if tree.bt >= 2{if data < tree.lchild.data{r_rorate (tree,         Root)}else{lr_rorate (tree, Root)}} if tree.bt <= -2{if data >= tree.rchild.data{ L_rorate (tree, Root)}else{rl_rorate (tree, Root)}}}func Delteavl (tree *avlbittree, parent *avl Bittree, root **avlbittree, data int) *avlbittree{if tree! = nil{if tree. Equal (data) {if *root = = tree{if Tree.lchild = = nil{*root = Tree.rchild p            arent = *root}else if tree.rchild = = nil{*root = Tree.lchild parent = *root                  }else{if Tree.lchild.rchild = = nil{Tree.lchild.rchild = Tree.rchild *root = tree.lchild parent = *root}else{tree1, Tree2: = Tree.lchild, t Ree.lchild for; Tree1.rchild! = nil;         {            Tree2 = tree1 tree1 = tree1.rchild} if tree1! = tree2{                     If tree1.lchild! = Nil {tree2.rchild = tree1.lchild} Tree2.rchild = Nil Tree1.lchild = tree.lchild Tree1.rchild = tree. Rchild *root = tree1 parent = *root}else{TR               Ee1.rchild = Tree.rchild *root = tree1 parent = *root} }}} or else if tree.lchild = = nil{if parent.            Less (data) {Parent.lchild = tree.rchild}else{parent.rchild = Tree.rchild }}else If Tree.rchild = = Nil {if parent.        Less (data) {parent.lchild = Tree.rchild} else {parent.rchild = Tree.rchild    }}else{parent.lchild = Tree.lchild tree.lchild.rchild = tree.rchild}} E LSE if tree.      Less (data) {Delteavl (Tree.lchild, tree, root, data)}else{Delteavl (Tree.rchild, tree, root, data) } Blanceavl (tree, root, data)} return Tree}func Insertavl (tree *avlbittree, root **avlbittree, data int) *av      lbittree{if tree = = nil{tree = &avlbittree{} tree.lchild, tree.rchild = nil, nil Tree.data = data TREE.BT = 0 if *root = = nil{*root = tree} return tree} if tree. Less (data) {Tree.lchild = Insertavlbittree (tree.lchild, data) tree.bt = Getavlheight (tree.lchild)-Getavlheight (tree.rchild)}else{tree.rchild = Insertavlbittree (tree.rchild, data) tree.bt = Getavlheight (tree.lchild)-G Etavlheight (Tree.rchild)} Blanceavl (tree, root, data) return tree}

var tree *base. Avlbittree//tree = base. Insertavlbittree (tree, 3)//tree = base. Insertavlbittree (tree, 2)//tree = base. Insertavlbittree (tree, 1)//tree = base. Insertavlbittree (tree, 0)//tree = base. Insertavlbittree (tree,-1)//fmt. Println ("---------------")//base. Middleavlprint (tree)//tree = base. Deleteavltree (tree, 2)//tree = base. Deleteavltree (tree, 3)//fmt. Println ("---------------")//base. Middleavlprint (tree)

226 Reads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.