Example #1
0
 public VTree (int data)
 {
     this.data = data;
     left = null;
     right = null;
     parent = null;
 }
        public int LeafCount (VTree node)
       {
           if (node.Left != null && node.Right != null)
           {
               count = 2;
           }
           else if (node.Left != null || node.Right != null)
           {
               count = 1;
           }
           else
               count = 0;

           return count;
       }
Example #3
0
        public void Add(int data)
        {
            var node = new VTree(data);
            // Если первый пустой
            if (this.data == 0 && this.left == null && this.right == null && this.parent == null)
            {
                this.data = data;
                count++;
            }
            else
            {
                // Если уже существует первый
                if (node.Data < this.Data)
                {
                    if (this.Left == null)
                    {
                        this.Left = node;
                        left.parent = this;
                        count++;
                    }
                    else
                    {
                        left.Add(data);
                    }
                }
                else if (node.Data > this.Data)
                {
                    if (this.Right == null)
                    {
                        this.Right = node;
                        Right.parent = this;
                        count++;
                    }
                    else
                    {
                        right.Add(data);
                    }
                }
            }


        }
Example #4
0
 public VTree()
 {
     left = null;
     right = null;
     parent = null;
 }
Example #5
0
 private void Reverse(VTree node, List<int> bb)
 {
     if (bb.Count != count)
     {
         if (node.left == null && node.right == null)
         {
             bb.Add(node.data);
             var current = node;
             if (bb.Count != count)
             {
                 node.parent.right = null;
                 Reverse(current.parent, bb);
             }
         }
         else if (node.left != null && node.right == null)
         {
             bb.Add(node.data);
             var current = node;
             if (node.parent != null)
             {
                 node.parent.right = node.left;
             }
             else
             {
                 current = node.left;
                 current.parent = null;
                 Reverse(current, bb);
             }
             node.left.parent = node.parent;
             Reverse(current.parent, bb);
         }
         else
         {
             Reverse(node.right, bb);
         }
     }
 }
Example #6
0
 private int Width(VTree root, int width)
 {
     if (root == null)
     {
         return 0;
     }
     if (root.left == null && root.right == null)
     {
         width++;
     }
     int left = 1 + Width(root.left, width);
     int right = 1 + Width(root.right, width);
     return Math.Max(left, right);
 }
Example #7
0
 public int NodeSize(VTree node)
 {
     return Height(node);
 }
Example #8
0
 private int Height(VTree root)
 {
     if (root == null)
     {
         return 0;
     }
     int left = 1 + Height(root.left);
     int right = 1 + Height(root.right);
     return Math.Max(left, right);
 }
Example #9
0
 private VTree FindMin(VTree node)
 {
     VTree Current = node;
     while (Current.Left != null)
     {
         Current = Current.Left;
     }
     return Current;
 }
Example #10
0
        public VTree Find(int data)
        {
            VTree node = new VTree(data);
            if (this == null)
            {
                // вернуть эксепшен
            }
            else if (this.data == data)
            {
                return this;
            }
            else
            {
                if (data < this.Data)
                {
                    if (this.Left.data == data)
                    {
                        node = this.Left;
                    }
                    else
                    {
                        left.Find(data);
                    }
                }
                else if (data > this.Data)
                {
                    if (this.Right.data == data)
                    {

                        node = this.Right;
                    }
                    else
                    {
                        right.Find(data);
                    }
                }
            }
            return node;
        }
Example #11
0
 public void Delete(int data)
 {
     if (data < this.data)
     {
         this.left.Delete(data);
     }
     if (data > this.data)
     {
         this.right.Delete(data);
     }
     if (data == this.data)
     {
         // Нету детей
         if (this.right == null && this.left == null)
         {
             if (this.parent.left.data == data)
                 this.parent.left = null;
             else
                 this.parent.right = null;
         }
         // Нету детей с лева
         else if (this.left == null && this.right != null)
         {
             if (this.parent.left.data == data)
                 this.parent.left = this.right;
             else
                 this.parent.right = this.right;
         }
         // Нету детей с права
         else if (this.right == null && this.left != null)
         {
             if (this.parent.left.data == data)
                 this.parent.left = this.left;
             else
                 this.parent.right = this.left;
         }
         // есть оба дитя
         else
         {
             if (this.right.left == null)
             {
                 this.left.parent = this.right;
                 if (this.parent.left.data == data)
                     this.parent.left = this.right;
                 else
                     this.parent.right = this.right;
             }
             else
             {
                 // this.FindMin(this.right, ref node);
                 var node = this.FindMin(this);
                 this.data = node.data;
                 node.parent.left = null;
                 node = null;
             }
         }
     }
     count--;
 }
Example #12
0
        public void InOrder(VTree N, IVisitor V)
        {
            if (N == null)
            {
                return;
            }
            else
            {
                if (N.Left != null)
                {
                    InOrder(N.Left, V);
                }

                V.Visit(N);

                if (N.Right != null)
                {
                    InOrder(N.Right, V);
                }
            }
        }
Example #13
0
 public void Visit (VTree node)
  {
      LeafCount(node);
  }
Example #14
0
 public void Visit(VTree node)
 {
     nodes.Add(node);
 }