Pat:root of AVL Tree, go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

An AVL tree is a self-balancing binary search tree. In an avltree, the heights of the both child subtrees of any node differ Byat most one; If at any time they differ by the more than one,rebalancing was done to restore the This property. Figures 1-4illustrate the rotation rules.

       

Now given a sequence of insertions, you is supposed to tell theroot of the resulting AVL tree.

Input Specification:

Each input file contains the one test case. For each case, the firstline contains a positive integer N (<=20) which are the totalnumber of keys to being inserted. Then N distinct integer keys Aregiven on the next line. All the numbers-a line is separated bya space.

Output Specification:

For each test case, print ythe root of the resulting AVL tree Inone line.

Sample Input 1:
588 70 61) 96 120
Sample Output 1:
70
Sample Input 2:
788 70 61 96 120 90 65
Sample Output 2:
--------------------------------------------------------------------------------

The code refers to the blogger http://blog.csdn.net/tiantangrenjian/article/details/12891091 and rewrites it for the Go language version.

------------------------------------------------------------------------------------------------------ ---------

001 Package Main
002
003 Import (
004 "FMT"
005 "Math"
006 )
007
008 type Node struct {
009 value int
010 Leftchild * Node
011 Rightchild * Node
012 Height int
013 }
014
015 func getheight ( node * Node ) ( Height int ){
016 if node == Nil {
017 return - 1
018 }
019 return node . Height
020 }
021
022 func isbalanced ( Parent * Node ) ( Balanced BOOL ){
023 diff := getheight ( Parent . Leftchild ) - getheight ( Parent . Rightchild )
024 Diff_abs := Math . Abs ( float64 ( diff ))
025 if Diff_abs <</SPAN>2{
026 Balanced =true
027 }Else{
028 Balanced =false
029 }
030 returnBalanced
031 }
032
033 func Max(N1int,N2int) (Num int){
034 Num= N1
035 ifN2 >N1{
036 Num = N2
037 }
038 returnNum
039 }
040
041 func Rotate_ll(Parent*Node)( Child *Node){
042 Child= Parent.Leftchild
043 Parent.Leftchild= Child.Rightchild
044 Child.Rightchild= Parent
045
046 Parent.Height= Max(getheight(Parent.Leftchild),getheight(Parent.Rightchild))+ 1the height of the//node must be the maximum of two child nodes height +1
047 Child.Height= Max(getheight( Child.Leftchild),getheight( Child.Rightchild))+ 1//The height of the wreck has not changed, no treatment.
048 return Child
049 }
050
051 func ROTATE_RR(Parent*Node)( Child *Node){
052 Child= Parent.Rightchild
053 Parent.Rightchild= Child.Leftchild
054 Child.Leftchild= Parent
055
056 Parent.Height= Max(getheight(Parent.Leftchild),getheight(Parent.Rightchild))+ 1
057 Child.Height= Max(getheight( Child.Leftchild),getheight( Child.Rightchild))+ 1
058 return Child
059 }
060
061 func ROTATE_RL(Parent*Node)( Child *Node){
062 Child= Parent.Rightchild
063 Parent.Rightchild= Rotate_ll( Child)
064 returnROTATE_RR(Parent)
065 }
066
067 func ROTATE_LR(Parent*Node)( Child *Node){
068 Child= Parent.Leftchild
069 Parent.Leftchild= ROTATE_RR( Child)
070 returnRotate_ll(Parent)
071 }
072
073 func Insertnode(Root*Node,NewValue int)(*Node){
074 ifRoot ==Nil{
075 Root = &Node{NewValue,Nil,Nil,0}
076 return Root
077 }
078 ifNewValue >Root.value{//Insert on right
079 Root.Rightchild= Insertnode(Root.Rightchild,NewValue);
080 if !isbalanced(Root){
081 if NewValue > Root.Rightchild.value{
082 Root=ROTATE_RR(Root)
083 }Else{
084 Root=ROTATE_RL(Root)
085 }
086 }
087 }Else{//Insert on left
088 Root.Leftchild= Insertnode(Root.Leftchild,NewValue)
089 if !isbalanced(Root){
090 if NewValue > Root.Leftchild.value{
091 Root=ROTATE_LR(Root);
092 }Else{
093 Root=Rotate_ll(Root)
094 }
095 }
096 }
097 Root.Height= Max(getheight(Root.Leftchild),getheight(Root.Rightchild))+ 1
098 returnRoot;
099 }
-
101 func Main() {
102 varN int
103 _, Err:=FMT.Scanf("%d\n",&N)
104 ifErr !=Nil {
the FMT.Println("Error",Err)
106 }
107 varRoot *Node= Nil
108 forI :=0; I<</SPAN>N;I++{
109 var Dataint
the _, Err =FMT.Scanf("%d",&Data)
111 if Err != Nil {
the FMT.Println("Error",Err)
113 }
the Root = Insertnode(Root,Data)
the }
the FMT.Println(Root.value)
117 }
Related Article

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.