コード例 #1
0
 private void Add(BTLNode data, BTLNode node, BTLNode parent)
 {
     if (node.value == data.value)
     {
         node.value      = data.value;
         node.leftNode   = data.leftNode;
         node.rightNode  = data.rightNode;
         node.parentNode = parent;
         return;
     }
     if (node.value > data.value)
     {
         if (node.leftNode == null)
         {
             node.leftNode = new BTLNode();
         }
         Add(data, node.leftNode, node);
     }
     else
     {
         if (node.rightNode == null)
         {
             node.rightNode = new BTLNode();
         }
         Add(data, node.rightNode, node);
     }
 }
コード例 #2
0
 private void Add(int value, BTLNode node, BTLNode parent)
 {
     if (node.value == value)
     {
         node.value      = value;
         node.parentNode = parent;
         return;
     }
     if (node.value > value)
     {
         if (node.leftNode == null)
         {
             node.leftNode = new BTLNode();
         }
         Add(value, node.leftNode, node);
     }
     else
     {
         if (node.rightNode == null)
         {
             node.rightNode = new BTLNode();
         }
         Add(value, node.rightNode, node);
     }
 }
コード例 #3
0
        private static List <int> ToArray(BTLNode tree)
        {
            if (tree == null)
            {
                return(new List <int>());
            }
            var result = ToArray(tree.leftNode);

            result.Add(tree.value);
            result.AddRange(ToArray(tree.rightNode));
            return(result);
        }
コード例 #4
0
        /// <summary>
        /// Tree counter
        /// </summary>
        /// <param name="node">Root node</param>
        /// <returns>count of elements in the tree</returns>
        private int CountElements(BTLNode node)
        {
            int count = 1;

            if (node.rightNode != null)
            {
                count += CountElements(node.rightNode);
            }
            if (node.leftNode != null)
            {
                count += CountElements(node.leftNode);
            }
            return(count);
        }
コード例 #5
0
 /// <summary>
 /// Check parental branch which contains currnt node
 /// </summary>
 /// <param name="node">current.Node node</param>
 /// <returns>Side</returns>
 private Side?MeForParent(BTLNode node)
 {
     if (node.parentNode == null)
     {
         return(null);
     }
     if (node.parentNode.leftNode == node)
     {
         return(Side.Left);
     }
     if (node.parentNode.rightNode == node)
     {
         return(Side.Right);
     }
     return(null);
 }
コード例 #6
0
        /// <summary>
        /// Find value in current.Node node
        /// </summary>
        /// <param name="value">Value for search</param>
        /// <param name="node">Node for search</param>
        /// <returns>Founded result</returns>
        public BTLNode Find(int value, BTLNode node)
        {
            if (node == null)
            {
                return(null);
            }

            if (node.value == value)
            {
                return(node);
            }
            if (node.value > value)
            {
                return(Find(value, node.leftNode));
            }
            return(Find(value, node.rightNode));
        }
コード例 #7
0
 private static List<int> ToArray(BTLNode tree)
 {
     if (tree == null) return new List<int>();
     var result = ToArray(tree.leftNode);
     result.Add(tree.value);
     result.AddRange(ToArray(tree.rightNode));
     return result;
 }
コード例 #8
0
 public BTLNode(BTLNode parent, int value)
 {
     this.parentNode = parent;
     this.value      = value;
 }
コード例 #9
0
 /// <summary>
 /// Tree counter
 /// </summary>
 /// <param name="node">Root node</param>
 /// <returns>count of elements in the tree</returns>
 private int CountElements(BTLNode node)
 {
     int count = 1;
     if (node.rightNode != null)
     {
         count += CountElements(node.rightNode);
     }
     if (node.leftNode != null)
     {
         count += CountElements(node.leftNode);
     }
     return count;
 }
コード例 #10
0
 public BTLNode(BTLNode parent, int value)
 {
     this.parentNode = parent;
     this.value = value;
 }
コード例 #11
0
 public BTLNode()
 {
     this.leftNode = null;
     this.rightNode = null;
     this.parentNode = null;
 }
コード例 #12
0
        public void Delete(BTLNode node)
        {
            if (node == null)
            {
                throw new NullReferenceException();
            }

            var me = MeForParent(node);
            if (node.leftNode == null && node.rightNode == null)
            {
                if (me == Side.Left)
                {
                    node.parentNode.leftNode = null;
                }
                else
                {
                    node.parentNode.rightNode = null;
                }
                return;
            }
            if (node.leftNode == null)
            {
                if (me == Side.Left)
                {
                    node.parentNode.leftNode = node.rightNode;
                }
                else
                {
                    node.parentNode.rightNode = node.rightNode;
                }

                node.rightNode.parentNode = node.parentNode;
                return;
            }
            if (node.rightNode == null)
            {
                if (me == Side.Left)
                {
                    node.parentNode.leftNode = node.leftNode;
                }
                else
                {
                    node.parentNode.rightNode = node.leftNode;
                }

                node.leftNode.parentNode = node.parentNode;
                return;
            }

            if (me == Side.Left)
            {
                node.parentNode.leftNode = node.rightNode;
            }
            if (me == Side.Right)
            {
                node.parentNode.rightNode = node.rightNode;
            }
            if (me == null)
            {
                var bufLeft = node.leftNode;
                var bufRightLeft = node.rightNode.leftNode;
                var bufRightRight = node.rightNode.rightNode;
                node.value = node.rightNode.value;
                node.rightNode = bufRightRight;
                node.leftNode = bufRightLeft;
                Add(bufLeft, node, node);
            }
            else
            {
                node.rightNode.parentNode = node.parentNode;
                Add(node.leftNode, node.rightNode, node.rightNode);
            }
        }
コード例 #13
0
        private void Add(BTLNode data, BTLNode node, BTLNode parent)
        {

            if (node.value == data.value)
            {
                node.value = data.value;
                node.leftNode = data.leftNode;
                node.rightNode = data.rightNode;
                node.parentNode = parent;
                return;
            }
            if (node.value > data.value)
            {
                if (node.leftNode == null) node.leftNode = new BTLNode();
                Add(data, node.leftNode, node);
            }
            else
            {
                if (node.rightNode == null) node.rightNode = new BTLNode();
                Add(data, node.rightNode, node);
            }
        }
コード例 #14
0
        private void Add(int value, BTLNode node, BTLNode parent)
        {

            if (node.value == value)
            {
                node.value = value;
                node.parentNode = parent;
                return;
            }
            if (node.value > value)
            {
                if (node.leftNode == null) node.leftNode = new BTLNode();
                Add(value, node.leftNode, node);
            }
            else
            {
                if (node.rightNode == null) node.rightNode = new BTLNode();
                Add(value, node.rightNode, node);
            }
        }
コード例 #15
0
 /// <summary>
 /// Check parental branch which contains currnt node
 /// </summary>
 /// <param name="node">current.Node node</param>
 /// <returns>Side</returns>
 private Side? MeForParent(BTLNode node)
 {
     if (node.parentNode == null) return null;
     if (node.parentNode.leftNode == node) return Side.Left;
     if (node.parentNode.rightNode == node) return Side.Right;
     return null;
 }
コード例 #16
0
 public BTLNode()
 {
     this.leftNode   = null;
     this.rightNode  = null;
     this.parentNode = null;
 }
コード例 #17
0
        public void Delete(BTLNode node)
        {
            if (node == null)
            {
                throw new NullReferenceException();
            }

            var me = MeForParent(node);

            if (node.leftNode == null && node.rightNode == null)
            {
                if (me == Side.Left)
                {
                    node.parentNode.leftNode = null;
                }
                else
                {
                    node.parentNode.rightNode = null;
                }
                return;
            }
            if (node.leftNode == null)
            {
                if (me == Side.Left)
                {
                    node.parentNode.leftNode = node.rightNode;
                }
                else
                {
                    node.parentNode.rightNode = node.rightNode;
                }

                node.rightNode.parentNode = node.parentNode;
                return;
            }
            if (node.rightNode == null)
            {
                if (me == Side.Left)
                {
                    node.parentNode.leftNode = node.leftNode;
                }
                else
                {
                    node.parentNode.rightNode = node.leftNode;
                }

                node.leftNode.parentNode = node.parentNode;
                return;
            }

            if (me == Side.Left)
            {
                node.parentNode.leftNode = node.rightNode;
            }
            if (me == Side.Right)
            {
                node.parentNode.rightNode = node.rightNode;
            }
            if (me == null)
            {
                var bufLeft       = node.leftNode;
                var bufRightLeft  = node.rightNode.leftNode;
                var bufRightRight = node.rightNode.rightNode;
                node.value     = node.rightNode.value;
                node.rightNode = bufRightRight;
                node.leftNode  = bufRightLeft;
                Add(bufLeft, node, node);
            }
            else
            {
                node.rightNode.parentNode = node.parentNode;
                Add(node.leftNode, node.rightNode, node.rightNode);
            }
        }
コード例 #18
0
        /// <summary>
        /// Find value in current.Node node
        /// </summary>
        /// <param name="value">Value for search</param>
        /// <param name="node">Node for search</param>
        /// <returns>Founded result</returns>
        public BTLNode Find(int value, BTLNode node)
        {
            if (node == null) return null;

            if (node.value == value) return node;
            if (node.value > value)
            {
                return Find(value, node.leftNode);
            }
            return Find(value, node.rightNode);
        }
コード例 #19
0
 public BinaryTreeLink(BTLNode rt)
 {
     this.rootNode.Node = rt;
     this.size = 0;
 }
コード例 #20
0
 public Link()
 {
     this.Node = new BTLNode();
 }
コード例 #21
0
 public BinaryTreeLink(BTLNode rt)
 {
     this.rootNode.Node = rt;
     this.size          = 0;
 }
コード例 #22
0
 public Link()
 {
     this.Node = new BTLNode();
 }