コード例 #1
0
ファイル: Form1.cs プロジェクト: Anton-Ovch/Valtech_
        private void DrawTree(BinaryTreeNode node, int begin, int end,int height)
        {


            if (node != null)
            {

                int NewWidth = (begin + end) / 2;
                int NewHeight = height + 60;


                Pen pen = new Pen(Color.Black, 1);
                Rectangle rect1 = new Rectangle(NewWidth, height, 30, 30);
                graph.DrawEllipse(pen, rect1);
                graph.DrawString(node.Value.ToString(), DefaultFont, Brushes.Black, NewWidth + 5, height + 5);


                if (node.Left != null)
                {
                    graph.DrawLine(pen, NewWidth, height+15, (begin + NewWidth) / 2+15, height+60);
                    DrawTree(node.Left, begin, NewWidth, NewHeight);
                }
                if (node.Right != null)
                {
                    graph.DrawLine(pen, NewWidth + 30, height + 15, (NewWidth + end) / 2 , height + 60 + 15);
                    DrawTree(node.Right, NewWidth, end, NewHeight);
                }

            }

        }
コード例 #2
0
ファイル: BinaryTrees.cs プロジェクト: Anton-Ovch/Valtech_
        private void AddTo(BinaryTreeNode node, int value)
        {
            if (node.Value > value)
            {
                if (node.Left == null)
                {
                    node.Left = new BinaryTreeNode(value);
                }
                else
                {
                    AddTo(node.Left, value);
                }
            }
            else
            {
                if (node.Right == null)
                {
                    node.Right = new BinaryTreeNode(value);
                }
                else
                {
                    AddTo(node.Right, value);
                }
            }

        }
コード例 #3
0
ファイル: BinaryTrees.cs プロジェクト: Anton-Ovch/Valtech_
        public void Add(int value)
        {

            if (head == null)
            {

                head = new BinaryTreeNode(value);
            }
            else
            {
                AddTo(head, value);
            }
            count++;

        }
コード例 #4
0
 /// <summary>
 /// Clears out the contents of the binary tree.
 /// </summary>
 public void Clear()
 {
     root = null;
 }
コード例 #5
0
ファイル: BinaryTrees.cs プロジェクト: Anton-Ovch/Valtech_
        public void Del(int value)
        {
            BinaryTreeNode current;
            BinaryTreeNode parent;

            current = FindWithParent(value, out parent);
            if (current == null)
            {

                return ;
            }
            count--;
            if (current.Right == null)
            {

                if (parent == null)
                {
                    head = current.Left;
                }
                else
                {
       

                    if (parent.Value > current.Value)
                    {
                        parent.Left = current.Left;
                    }
                    else
                    {
                        parent.Right = current.Left;
                    }
                }

            }
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;
                if (parent == null)
                {
                    head = current.Right;
                }
                else
                {
                    if (parent.Value > current.Value)
                    {
                        parent.Left = current.Right;
                    }
                    else
                    {
                        parent.Right = current.Right;
                    }
         

                }

            }
            else
            {
                BinaryTreeNode leftmost = current.Right.Left;
                BinaryTreeNode leftmostParent = current.Right;
                while (leftmost.Left != null)
                {
                    leftmostParent = leftmost;
                    leftmost = leftmost.Left;

                }
                leftmostParent.Left = leftmost.Right;
                leftmost.Left = current.Left;
                leftmost.Right = current.Right;
                if (parent == null)
                {
                    head = leftmost;
                }
                else
                {
                    if (parent.Value > current.Value)
                    {
                        parent.Left = leftmost;
                    }
                    else
                    {
                        parent.Right = leftmost;
                    }

         
                }
            }
   

        }
コード例 #6
0
ファイル: BinaryTrees.cs プロジェクト: Anton-Ovch/Valtech_
 private void ToArrayHelper(BinaryTreeNode node, List<int> res)
 {
     if (node != null)
     {
         ToArrayHelper(node.Left, res);
         res.Add(node.Value);
         ToArrayHelper(node.Right, res);
     }
 }
コード例 #7
0
ファイル: BinaryTrees.cs プロジェクト: Anton-Ovch/Valtech_
        private void ReversHelper(BinaryTreeNode node)
        {
            if (node.Left != null || node.Right != null)
            {
                if (node.Left != null)
                {              
                    ReversHelper(node.Left);
                }
                if (node.Right != null)
                {             
                    ReversHelper(node.Right);
                }

                var tmp = node.Left;
                node.Left = node.Right;
                node.Right = tmp;
            }
        }
コード例 #8
0
ファイル: BinaryTrees.cs プロジェクト: Anton-Ovch/Valtech_
 private int GetWidth(BinaryTreeNode three, int level)
 {
     if (three == null)
         return 0;
     if (level == 1)
         return 1;
     else if (level > 1)
         return GetWidth(three.Left, level - 1) + GetWidth(three.Right, level - 1);
     else 
         return 1;
 }
コード例 #9
0
ファイル: BinaryTrees.cs プロジェクト: Anton-Ovch/Valtech_
        public void Clear()
        {
            head = null;
            count = 0;

        }
コード例 #10
0
ファイル: BinaryTrees.cs プロジェクト: Anton-Ovch/Valtech_
        private void FindHeight(BinaryTreeNode node, ref int maxHeight)
        {

            if (node.Left != null || node.Right != null)
            {
                if (node.Left != null)
                {
                    int brunchHeight = 1;
                    FindHeight(node.Left, ref brunchHeight);
                    brunchHeight++;
                    if (brunchHeight > maxHeight)
                        maxHeight = brunchHeight;
                }
                if (node.Right != null)
                {
                    int brunchHeight = 1;
                    FindHeight(node.Right, ref brunchHeight);
                    brunchHeight++;
                    if (brunchHeight > maxHeight)
                        maxHeight = brunchHeight;
                }

            }
            //if (node.Left != null || node.Right != null)
            //{
            //    if (node.Left != null)
            //    {

            //        FindHeight(node.Left, ref brunchHeight, ref maxHeight);
            //        brunchHeight++;
            //        if (brunchHeight > maxHeight)
            //            maxHeight = brunchHeight;
            //    }
            //    if (node.Right != null)
            //    {

            //        FindHeight(node.Right, ref brunchHeight, ref maxHeight);
            //        brunchHeight++;
            //        if (brunchHeight > maxHeight)
            //            maxHeight = brunchHeight;
            //    }

            //}
        }
コード例 #11
0
ファイル: BinaryTrees.cs プロジェクト: Anton-Ovch/Valtech_
 private void FindCountOfNodes(BinaryTreeNode node, ref int res)
 {
     if (node.Left != null || node.Right != null)
     {
         if (node.Left != null)
         {
             FindCountOfNodes(node.Left, ref res);
         }
         if (node.Right != null)
         {
             FindCountOfNodes(node.Right, ref res);
         }
         res++;
     }
 }
コード例 #12
0
ファイル: BinaryTrees.cs プロジェクト: Anton-Ovch/Valtech_
        private BinaryTreeNode FindWithParent(int value, out BinaryTreeNode parent)
        {
            BinaryTreeNode current = head;
            parent = null;
            while (current != null)
            {

                if (current.Value > value)
                {
                    parent = current;
                       current = current.Left;
                }
                else if (current.Value < value)
                {
                        parent = current;
                        current = current.Right;
                }
                else
                {
                    break;
                }



                //int result = current.CompareTo(value);
                //if (result > 0)
                //{
                //    parent = current;
                //    current = current.Left;
                //}
                //else if (result < 0)
                //{
                //    parent = current;
                //    current = current.Right;
                //}
                //else
                //{
                //    break;
                //}
            }
            return current;
        }
コード例 #13
0
 /// <summary>
 /// Removes the contents of the BST
 /// </summary>
 public void Clear()
 {
     root  = null;
     count = 0;
 }
コード例 #14
0
        /// <summary>
        /// Attempts to remove the specified data element from the BST.
        /// </summary>
        /// <param name="data">The data to remove from the BST.</param>
        /// <returns>True if the element is found in the tree, and removed; false if the element is not
        /// found in the tree.</returns>
        public bool Remove(T data)
        {
            // first make sure there exist some items in this tree
            if (root == null)
            {
                return(false);       // no items to remove
            }
            // Now, try to find data in the tree
            BinaryTreeNode <T> current = root, parent = null;
            int result = comparer.Compare(current.Value, data);

            while (result != 0)
            {
                if (result > 0)
                {
                    // current.Value > data, if data exists it's in the left subtree
                    parent  = current;
                    current = current.Left;
                }
                else if (result < 0)
                {
                    // current.Value < data, if data exists it's in the right subtree
                    parent  = current;
                    current = current.Right;
                }

                // If current == null, then we didn't find the item to remove
                if (current == null)
                {
                    return(false);
                }
                else
                {
                    result = comparer.Compare(current.Value, data);
                }
            }

            // At this point, we've found the node to remove
            count--;

            // We now need to "rethread" the tree
            // CASE 1: If current has no right child, then current's left child becomes
            //         the node pointed to by the parent
            if (current.Right == null)
            {
                if (parent == null)
                {
                    root = current.Left;
                }
                else
                {
                    result = comparer.Compare(parent.Value, current.Value);
                    if (result > 0)
                    {
                        // parent.Value > current.Value, so make current's left child a left child of parent
                        parent.Left = current.Left;
                    }
                    else if (result < 0)
                    {
                        // parent.Value < current.Value, so make current's left child a right child of parent
                        parent.Right = current.Left;
                    }
                }
            }
            // CASE 2: If current's right child has no left child, then current's right child
            //         replaces current in the tree
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;

                if (parent == null)
                {
                    root = current.Right;
                }
                else
                {
                    result = comparer.Compare(parent.Value, current.Value);
                    if (result > 0)
                    {
                        // parent.Value > current.Value, so make current's right child a left child of parent
                        parent.Left = current.Right;
                    }
                    else if (result < 0)
                    {
                        // parent.Value < current.Value, so make current's right child a right child of parent
                        parent.Right = current.Right;
                    }
                }
            }
            // CASE 3: If current's right child has a left child, replace current with current's
            //          right child's left-most descendent
            else
            {
                // We first need to find the right node's left-most child
                BinaryTreeNode <T> leftmost = current.Right.Left, lmParent = current.Right;
                while (leftmost.Left != null)
                {
                    lmParent = leftmost;
                    leftmost = leftmost.Left;
                }

                // the parent's left subtree becomes the leftmost's right subtree
                lmParent.Left = leftmost.Right;

                // assign leftmost's left and right to current's left and right children
                leftmost.Left  = current.Left;
                leftmost.Right = current.Right;

                if (parent == null)
                {
                    root = leftmost;
                }
                else
                {
                    result = comparer.Compare(parent.Value, current.Value);
                    if (result > 0)
                    {
                        // parent.Value > current.Value, so make leftmost a left child of parent
                        parent.Left = leftmost;
                    }
                    else if (result < 0)
                    {
                        // parent.Value < current.Value, so make leftmost a right child of parent
                        parent.Right = leftmost;
                    }
                }
            }

            // Clear out the values from current
            current.Left = current.Right = null;
            current      = null;

            return(true);
        }