2014-03-29 2 views
-1

안녕하세요. 저는이 AVLTree를 보유하고 있지만 높이 메서드를 작동시킬 수 없습니다. 항상 0입니다. 당신 중 일부는 그것에 대해 살펴보고 아마도 내가 무엇을 놓치고 있는지 알아낼 수 있습니까? 보시다시피 나는 다른 해결책을 시도했지만 잘 모르겠습니다. 시간을 절약 할 수 있습니다. 감사!AVLTree heigth가 항상 0으로 돌아 왔습니다.

 public class AVLTree2<TKey, TValue> : BinarySearchTree<TKey, TValue> 
      where TKey : IComparable<TKey> 
     { 

    private AVLNode node; 
    int count; 
      /// <summary> 
      /// Initializes a new instance of the AVLTree&lt;TKey, TValue> class that is empty. 
      /// </summary> 
      public AVLTree2() { node = null;} 

      /// <summary> 
      /// Initializes a new instance of the AVLTree&lt;TKey, TValue> class that contains elements copied from the specified IDictionary&lt;TKey, TValue>. 
      /// </summary> 
      /// <param name="collection">The IDictionary&lt;TKey, TValue> whose elements are copied to the new AVLTree&lt;TKey, TValue>.</param> 
      public AVLTree2(IDictionary<TKey, TValue> collection) : base(collection) 
      { 
       foreach (KeyValuePair<TKey, TValue> t in collection) 
        this.Add(t.Key, t.Value); 
      } 

      /// <summary> 
      /// Adds the specified key and value to the AVLTree&lt;TKey, TValue>. 
      /// </summary> 
      /// <param name="key">The key of the element to add.</param> 
      /// <param name="value">The value of the element to add.</param> 
      /// 

      public new int Count() 
      { 
       return count; 
      } 

      public override void Add(TKey key, TValue value) 
      { 
       AVLNode node = new AVLNode(key, value); 
       base.Add(node); 
       this.Rebalance(node.Parent); 
       this.count++; 
      } 



      public override void Delete(TreeNode current) 
      { 
       base.Delete(current); 
       this.Rebalance((AVLNode)current.Parent); 
       this.count--; 
      } 

      private void Rebalance(AVLNode start) 
      { 
       int height, left, right, diff, leftC, rightC, diffC; 
       while (start != null) 
       { 
        height = start.Height; 
        left = start.Left != null ? start.Left.Height : -1; 
        right = start.Right != null ? start.Right.Height : -1; 
        diff = left - right; 

        if (diff == -2) 
        { 
         leftC = start.Right.Left != null ? start.Right.Left.Height : -1; 
         rightC = start.Right.Right != null ? start.Right.Right.Height : -1; 
         diffC = leftC - rightC; 

         if (diffC == 1) 
          this.RotateRight(start.Right); 
         this.RotateLeft(start); 

         start = start.Parent.Parent; 
         continue; 
        } 
        else if (diff == 2) 
        { 
         leftC = start.Left.Left != null ? start.Left.Left.Height : -1; 
         rightC = start.Left.Right != null ? start.Left.Right.Height : -1; 
         diffC = leftC - rightC; 

         if (diffC == -1) 
          this.RotateLeft(start.Left); 
         this.RotateRight(start); 

         start = start.Parent.Parent; 
         continue; 
        } 

        start.Height = Math.Max(left, right) + 1; 
        if (height == start.Height) 
         break; 
        else 
         start = start.Parent; 
       } 
      } 

      protected void RotateLeft(AVLNode start) 
      { 
       base.RotateLeft(start); 

       int left, right; 
       left = start.Left != null ? start.Left.Height : -1; 
       right = start.Right != null ? start.Right.Height : -1; 
       start.Height = Math.Max(left, right) + 1; 

       left = start.Height; 
       right = start.Parent.Right != null ? start.Parent.Right.Height : -1; 
       start.Parent.Height = Math.Max(left, right) + 1; 
      } 

      protected void RotateRight(AVLNode start) 
      { 
       base.RotateRight(start); 

       int left, right; 
       left = start.Left != null ? start.Left.Height : -1; 
       right = start.Right != null ? start.Right.Height : -1; 
       start.Height = Math.Max(left, right) + 1; 

       left = start.Parent.Left != null ? start.Parent.Left.Height : -1; 
       right = start.Height; 
       start.Parent.Height = Math.Max(left, right) + 1; 

      } 

    public int Height() 
      { 
       return height(node); 
      } 

    public int height(AVLNode start) 
    { 
     AVLNode temp = start; 

     if (temp == null) 
      return 0; 
     else if (start.Left == null && start.Right == null) 
      return 0; 
     else 
      return 1 + Math.Max(height(start.Left), height(start.Right)); 





    } 
      public class AVLNode : KeyValueTreeNode 
      { 
       public AVLNode(TKey key, TValue value) : base(key, value) { this.Height = 0; } 

       public int Height { get; set; } 
       public new AVLNode Parent { get { return (AVLNode)base.Parent; } set { base.Parent = value; } } 
       public new AVLNode Left { get { return (AVLNode)base.Left; } set { base.Left = value; } } 
       public new AVLNode Right { get { return (AVLNode)base.Right; } set { base.Right = value; } } 
      } 
     } 
    } 
+0

디버깅에서 얻은 지식, 디자인 및 알고리즘을 공유하십시오. 이것은 사람들이 리버스 엔지니어링 할 것으로 기대하는 많은 코드입니다. 특히, 높이가 먼저 어느 라인에서 잘못보고됩니까? 내가 의심하는 것처럼 –

답변

1

node은 절대로 초기화하지 않습니다. Height()height(node)node==null이므로 height은 0을 반환합니다.

+0

. 감사! – user3474335

+0

물어봐야 할 것 : 당신이 이미 의심했다면, 물어보기 전에 왜 시도하지 않았습니까? – Benesh

+0

어떻게 수정해야합니까? – user3474335

관련 문제