コード例 #1
0
 public BinaryTree()
 {
     rootNode  = null;
     leaveNode = null;
 }
コード例 #2
0
 public BstBuilder()
 {
     rootNode   = null;
     parentNode = null;
 }
コード例 #3
0
 public BinaryNode(T data, BinaryNode <T> left, BinaryNode <T> right)
 {
     this.data  = data;
     this.left  = left;
     this.right = right;
 }
コード例 #4
0
ファイル: BinaryNode.cs プロジェクト: JRMishra/ImplementBST
 public BinaryNode(T data)
 {
     this.data = data;
     lChild    = null;
     rChild    = null;
 }
コード例 #5
0
        public bool Remove(T value)
        {
            var            current = Root;
            BinaryNode <T> parent  = null;

            while (current != null)
            {
                //Found it
                if (current.Value.CompareTo(value) == 0)
                {
                    // No left or right child
                    if (current.LeftChild == null && current.RightChild == null)
                    {
                        //are we at root
                        if (parent == null)
                        {
                            Root = null;
                        }
                        else
                        {
                            if (parent > current)
                            {
                                parent.LeftChild = null;
                            }
                            else
                            {
                                parent.RightChild = null;
                            }
                        }
                    }  // Has Left but no right
                    else if (current.LeftChild != null && current.RightChild == null)
                    {
                        if (parent == null)
                        {
                            Root = current.LeftChild;
                        }
                        else
                        {
                            if (parent > current)
                            {
                                parent.LeftChild = current.LeftChild;
                            }
                            else
                            {
                                parent.RightChild = current.RightChild;
                            }
                        }
                    } // Has Right but that right has no left
                    else if (current.RightChild != null && current.RightChild.LeftChild == null)
                    {
                        if (parent == null)
                        {
                            Root = current.RightChild;
                        }
                        else
                        {
                            if (parent > current)
                            {
                                parent.LeftChild = current.RightChild;
                            }
                            else
                            {
                                parent.RightChild = current.RightChild;
                            }
                        }
                    }  // Has Right and that right has left child



                    --Count;
                    return(true);
                }


                //Otherwise
                parent = current;
                if (current.Value.CompareTo(value) < 0)
                {
                    current = current.RightChild;
                }
                else if (current.Value.CompareTo(value) > 0)
                {
                    current = current.LeftChild;
                }
            }

            return(false);
        }
コード例 #6
0
 public BinaryNodeTree()
 {
     root = null;
 }
コード例 #7
0
        public void Insert(T value)
        {
            var binaryNode = new BinaryNode <T>(value);

            Insert(binaryNode);
        }
コード例 #8
0
 public virtual void Clear()
 {
     root = null;
 }
コード例 #9
0
        void Remove(BinaryNode <T> currentNode, BinaryNode <T> parentNode)
        {
            if (currentNode == null)
            {
                return;
            }

            if (currentNode.Right != null)
            {
                var leftMostNode       = FindLeftMostNode(currentNode.Right);
                var leftMostNodeParent = GetParentNode(leftMostNode);

                currentNode.Value = leftMostNode.Value;

                if (leftMostNodeParent != null && leftMostNode.Right == null)
                {
                    if (leftMostNodeParent.Equals(currentNode))
                    {
                        leftMostNodeParent.Right = null;
                    }
                    else
                    {
                        leftMostNodeParent.Left = null;
                    }
                }
                else
                {
                    Remove(leftMostNode, leftMostNodeParent);
                }
            }

            else if (currentNode.Left != null)
            {
                var leftChildValue = currentNode.Left.Value;
                Remove(currentNode.Left, currentNode);
                currentNode.Value = leftChildValue;
            }
            else
            {
                if (currentNode.Equals(Root))
                {
                    Root = null;
                    return;
                }
                var compareInt = currentNode.Value.CompareTo(parentNode.Value);
                switch (compareInt)
                {
                case (0):
                case (-1):
                    parentNode.Left = null;
                    return;

                case (1):
                    parentNode.Right = null;
                    return;

                default:
                    return;
                }
            }
        }
コード例 #10
0
        BinaryNode <T> GetParentNode(BinaryNode <T> nodeToFind, BinaryNode <T> currentNode, BinaryNode <T> parentNode)
        {
            if (currentNode == null)
            {
                return(null);
            }
            else if (currentNode.Equals(nodeToFind))
            {
                return(parentNode);
            }

            var intComparison = nodeToFind.Value.CompareTo(currentNode.Value);

            switch (intComparison)
            {
            case (0):
            case (-1):
                return(GetParentNode(nodeToFind, currentNode.Left, currentNode));

            case (1):
                return(GetParentNode(nodeToFind, currentNode.Right, currentNode));

            default:
                return(null);
            }
        }