protected BinarySearchTreeNode Successor(BinarySearchTreeNode deletedNode)
        {
            BinarySearchTreeNode successorParent = deletedNode;
            BinarySearchTreeNode successor       = deletedNode;
            BinarySearchTreeNode current         = deletedNode.Right;

            while (current != null)
            {
                //swapping
                successorParent = successor;
                successor       = current;
                //iterating the loop
                current = current.Left;
            }
            if (successor != deletedNode.Right)
            {
                successorParent.Left = successor.Right;
                successor.Right      = deletedNode.Right;
            }
            return(successor);
        }
Exemple #2
0
        private BinarySearchTreeNode SearchString(BinarySearchTreeNode Node, string key)
        {
            if (Node == null)
            {
                return(null);
            }
            switch (type)
            {
            case "Name":
                if (String.Compare(Node.Products[0].Name, key, true) == 0)
                {
                    return(Node);
                }
                else if (String.Compare(Node.Products[0].Name, key, true) > 0)
                {
                    return(SearchString(Node.Left, key));
                }
                else
                {
                    return(SearchString(Node.Right, key));
                }

            case "TradeMark":
            default:
                if (String.Compare(Node.Products[0].TradeMark, key, true) == 0)
                {
                    return(Node);
                }
                else if (String.Compare(Node.Products[0].TradeMark, key, true) > 0)
                {
                    return(SearchString(Node.Left, key));
                }
                else
                {
                    return(SearchString(Node.Right, key));
                }
            }
        }
Exemple #3
0
 public static void ReBalance(BinarySearchTreeNode ParentNode, BinarySearchTreeNode Node)
 {
     // if left subtree is too high, and left child has a left child
     if (Node.Balance == -2 && Node.Left.Balance == -1)
     {
         RotateRight(ParentNode, Node);
     }
     // if right subtree is too high, and right child has a right child
     else if (Node.Balance == 2 && Node.Right.Balance == 1)
     {
         RotateLeft(ParentNode, Node);
     }
     // Left subtree is too high, and left child has a right child.
     else if (Node.Balance == -2 && Node.Left.Balance == 1)
     {
         RotateLeftRight(ParentNode, Node);
     }
     // Right subtree is too high, and right child has a left child.
     else if (Node.Balance == 2 && Node.Right.Balance == -1)
     {
         RotateRightLeft(ParentNode, Node);
     }
 }
Exemple #4
0
        public override bool Delete(Product product)
        {
            BinarySearchTreeNode current = Root;
            BinarySearchTreeNode parent  = Root;
            bool isLeft = true;

            while ((decimal)current.Products[0].SalePrice != product.SalePrice)
            {
                parent = current;
                if (product.SalePrice < (decimal)current.Products[0].SalePrice)
                {
                    isLeft  = true;
                    current = current.Left;
                }
                else
                {
                    isLeft  = false;
                    current = current.Right;
                }
                if (current == null)
                {
                    return(false);
                }
            }
            // If deleted node has no child
            if (current.Left == null && current.Right == null)
            {
                if (current == Root)
                {
                    Root = null;
                }
                else if (isLeft)
                {
                    parent.Left = null;
                }
                else
                {
                    parent.Right = null;
                }
            }
            //If deleted node has one child
            else if (current.Right == null)
            {
                if (current == Root)
                {
                    Root = current.Left;
                }
                else if (isLeft)
                {
                    parent.Left = current.Left;
                }
                else
                {
                    parent.Right = current.Left;
                }
            }
            else if (current.Left == null)
            {
                if (current == Root)
                {
                    Root = current.Right;
                }
                else if (isLeft)
                {
                    parent.Left = current.Right;
                }
                else
                {
                    parent.Right = current.Right;
                }
            }
            //If deleted node has two children
            else
            {
                BinarySearchTreeNode successor = Successor(current);
                if (current == Root)
                {
                    Root = successor;
                }
                else if (isLeft)
                {
                    parent.Left = successor;
                }
                else
                {
                    parent.Right = successor;
                }
                successor.Left = current.Left;
            }
            return(true);
        }
Exemple #5
0
 public IntBST(BinarySearchTreeNode Node) : base(Node)
 {
 }
 public BinarySearchTree(BinarySearchTreeNode Node)
 {
     Products  = new List <Product>();
     this.Root = Node;
 }
 private void Visitation(BinarySearchTreeNode Node)
 {
     Products.AddRange(Node.Products);
 }
Exemple #8
0
        public override bool Delete(Product product)
        {
            BinarySearchTreeNode current = Root;
            BinarySearchTreeNode parent  = Root;
            bool isLeft = true;

            switch (type)
            {
            case "Name":
                while (String.Compare(product.Name, current.Products[0].Name, true) != 0)
                {
                    parent = current;
                    if (String.Compare(product.Name, current.Products[0].Name, true) < 0)
                    {
                        isLeft  = true;
                        current = current.Left;
                    }
                    else
                    {
                        isLeft  = false;
                        current = current.Right;
                    }
                    if (current == null)
                    {
                        return(false);
                    }
                }
                break;

            case "TradeMark":
            default:
                while (String.Compare(product.TradeMark, current.Products[0].TradeMark, true) != 0)
                {
                    parent = current;
                    if (String.Compare(product.TradeMark, current.Products[0].TradeMark, true) < 0)
                    {
                        isLeft  = true;
                        current = current.Left;
                    }
                    else
                    {
                        isLeft  = false;
                        current = current.Right;
                    }
                    if (current == null)
                    {
                        return(false);
                    }
                }
                break;
            }
            // If deleted node has no child
            if (current.Left == null && current.Right == null)
            {
                if (current == Root)
                {
                    Root = null;
                }
                else if (isLeft)
                {
                    parent.Left = null;
                }
                else
                {
                    parent.Right = null;
                }
            }
            //If deleted node has one child
            else if (current.Right == null)
            {
                if (current == Root)
                {
                    Root = current.Left;
                }
                else if (isLeft)
                {
                    parent.Left = current.Left;
                }
                else
                {
                    parent.Right = current.Left;
                }
            }
            else if (current.Left == null)
            {
                if (current == Root)
                {
                    Root = current.Right;
                }
                else if (isLeft)
                {
                    parent.Left = current.Right;
                }
                else
                {
                    parent.Right = current.Right;
                }
            }
            //If deleted node has two children
            else
            {
                BinarySearchTreeNode successor = Successor(current);
                if (current == Root)
                {
                    Root = successor;
                }
                else if (isLeft)
                {
                    parent.Left = successor;
                }
                else
                {
                    parent.Right = successor;
                }
                successor.Left = current.Left;
            }
            return(true);
        }
Exemple #9
0
 public StringBST(BinarySearchTreeNode node, string type) : base(node)
 {
     this.type = type;
 }