예제 #1
0
파일: AVLTree.cs 프로젝트: Bleizard/ITMO
 public static AVLTree Initialize(int[] keys)
 {
     AVLTree result = new AVLTree(keys[0]);
     for (int i = 1; i < keys.Length; i++)
         result = Grow(result, keys[i]);
     return result;
 }
예제 #2
0
파일: AVLTree.cs 프로젝트: Bleizard/ITMO
 public AVLTree(int key)
 {
     this.key = key;
     left = null;
     right = null;
     height = 1;
 }
예제 #3
0
파일: AVLTree.cs 프로젝트: Bleizard/ITMO
 private AVLTree RotateLeft()
 {
     AVLTree result = this.right;
     this.right = result.left;
     result.left = this;
     this.FixHeight();
     result.FixHeight();
     return result;
 }
예제 #4
0
파일: AVLTree.cs 프로젝트: Bleizard/ITMO
 public static AVLTree Grow(AVLTree tree, int key)
 {
     if (tree == null)
         return new AVLTree(key);
     if (key < tree.key)
         tree.left = Grow(tree.left, key);
     else
         tree.right = Grow(tree.right, key);
     return tree.Balance();
 }
예제 #5
0
파일: AVLTree.cs 프로젝트: Bleizard/ITMO
 private AVLTree Balance()
 {
     this.FixHeight();
     if (this.GetBalanceFactor() == 2)
     {
         if (this.right.GetBalanceFactor() < 0)
             this.right = this.right.RotateRight();
         return this.RotateLeft();
     }
     if (this.GetBalanceFactor() == -2)
     {
         if (this.left.GetBalanceFactor() > 0)
             this.left = this.left.RotateLeft();
         return this.RotateRight();
     }
     return this;
 }
예제 #6
0
파일: AVLTree.cs 프로젝트: topor-fake/ITMO
 private static int GetHeight(AVLTree tree)
 {
     return((tree == null) ? 0 : tree.height);
 }
예제 #7
0
파일: AVLTree.cs 프로젝트: Bleizard/ITMO
 public bool Search(AVLTree tree, int key)
 {
     if (tree == null)
         return false;
     if (tree.key > key)
         return Search(tree.left, key);
     if (tree.key < key)
         return Search(tree.right, key);
     else return true;
 }
예제 #8
0
파일: AVLTree.cs 프로젝트: Bleizard/ITMO
 private static int GetHeight(AVLTree tree)
 {
     return (tree == null) ? 0 : tree.height; 
 }
 bool IsLeftRight(T item, AVLTree <T> node)
 {
     return(Comparer.Compare(item, node._left.Value).IsFirstMore);
 }
 bool IsLeftLeft(T item, AVLTree <T> node)
 {
     return(Comparer.Compare(item, node._left.Value).IsFirstLess);
 }
 void SetHeight(AVLTree <T> node)
 {
     node.Height = 1 + Math.Max(GetHeight(node._left), GetHeight(node._right));
 }