Data structures and algorithms (C # implementation) series --- avltree (II)
// --------------- Override --------------------
Public override void attachkey (object _ OBJ)
{
If (! Isempty ())
Throw new exception ("My: this node must be a empty tree node! ");
This. Key = _ OBJ;
// Generate a degree long array and initialize it as an empty tree
This. treelist = new arraylist ();
This. treelist. capacity = (INT) This. degree;
For (INT I = 0; I <this. degree; I ++)
{
Treelist. Add (New avltree ());
}
//
This. Height = 0;
}
// Balance the tree after modifying the Tree Structure
Public override void balance ()
{
This. adjustheight ();
// If the value is greater than 1, it indicates imbalance.
If (math. Abs (this. balancefactor ()> 1)
{
If (this. balancefactor ()> 0)
{
If (avltree) This. Left). balancefactor ()> 0)
This. llrotation ();
Else
This. lrrotation ();
}
Else
{
If (avltree) This. Right). balancefactor () <0)
This. rrrotation ();
Else
This. rlrotation ();
}
}
}
Public int height {get {return this. height ;}}
}