Exemplo n.º 1
0
 public override void Add(Product product)
 {
     if (Root == null)
     {
         Root = new IntBinarySearchTreeNode(product);;
     }
     this.Root.InsertNode(product);
     if (Root.Balance < -1 || Root.Balance > 1)
     {
         BinarySearchTreeNode fakeParent = new IntBinarySearchTreeNode {
             Left = Root
         };
         Balancer.ReBalance(fakeParent, Root);
         Root = fakeParent.Left;
     }
 }
Exemplo n.º 2
0
        public override bool InsertNode(Product product)
        {
            switch (type)
            {
            case "Name":
            {
                if (String.Compare(product.Name, Products[0].Name, true) == 0)
                {
                    if (Products.Exists(x => x == product))
                    {
                        return(false);
                    }
                    AddItem(product);
                    return(true);
                }
                else if (String.Compare(product.Name, Products[0].Name, true) < 0)
                {
                    if (Left == null)
                    {
                        Left = new StringBinarySearchTreeNode(product, type);
                        if (Right == null)
                        {
                            Balance = -1;
                        }
                        else
                        {
                            Balance = 0;
                        }
                    }
                    else
                    {
                        if (Left.InsertNode(product))
                        {
                            if (Left.Balance < -1 || Left.Balance > 1)
                            {
                                Balancer.ReBalance(this, Left);
                            }
                            else
                            {
                                Balance--;
                            }
                        }
                    }
                }
                else
                {
                    if (Right == null)
                    {
                        Right = new StringBinarySearchTreeNode(product, type);
                        if (Left == null)
                        {
                            Balance = 1;
                        }
                        else
                        {
                            Balance = 0;
                        }
                    }
                    else
                    {
                        if (Right.InsertNode(product))
                        {
                            if (Right.Balance < -1 || Right.Balance > 1)
                            {
                                Balancer.ReBalance(this, Right);
                            }
                            else
                            {
                                Balance++;
                            }
                        }
                    }
                }
                if (Balance != 0)
                {
                    return(true);
                }
            }
            break;

            case "TradeMark":
            default:
            {
                if (String.Compare(product.TradeMark, Products[0].TradeMark, true) == 0)
                {
                    if (Products.Exists(x => x == product))
                    {
                        return(false);
                    }
                    AddItem(product);
                    return(true);
                }
                else if (String.Compare(product.TradeMark, Products[0].TradeMark, true) < 0)
                {
                    if (Left == null)
                    {
                        Left = new StringBinarySearchTreeNode(product, type);
                        if (Right == null)
                        {
                            Balance = -1;
                        }
                        else
                        {
                            Balance = 0;
                        }
                    }
                    else
                    {
                        if (Left.InsertNode(product))
                        {
                            if (Left.Balance < -1 || Left.Balance > 1)
                            {
                                Balancer.ReBalance(this, Left);
                            }
                            else
                            {
                                Balance--;
                            }
                        }
                    }
                }
                else
                {
                    if (Right == null)
                    {
                        Right = new StringBinarySearchTreeNode(product, type);
                        if (Left == null)
                        {
                            Balance = 1;
                        }
                        else
                        {
                            Balance = 0;
                        }
                    }
                    else
                    {
                        if (Right.InsertNode(product))
                        {
                            if (Right.Balance < -1 || Right.Balance > 1)
                            {
                                Balancer.ReBalance(this, Right);
                            }
                            else
                            {
                                Balance++;
                            }
                        }
                    }
                }
            }
                if (Balance != 0)
                {
                    return(true);
                }
                break;
            }
            return(false);
        }
Exemplo n.º 3
0
        public override bool InsertNode(Product product)
        {
            if (product.SalePrice == (decimal)Products[0].SalePrice)
            {
                if (Products.Exists(x => x == product))
                {
                    return(false);
                }
                AddItem(product);
                return(true);
            }
            else if (product.SalePrice < (decimal)Products[0].SalePrice)
            {
                if (Left == null)
                {
                    Left = new IntBinarySearchTreeNode(product);
                    if (Right == null)
                    {
                        Balance = -1;
                    }
                    else
                    {
                        Balance = 0;
                    }
                }
                else
                {
                    if (Left.InsertNode(product))
                    {
                        if (Left.Balance < -1 || Left.Balance > 1)
                        {
                            Balancer.ReBalance(this, Left);
                        }
                        else
                        {
                            Balance--;
                        }
                    }
                }
            }
            else
            {
                if (Right == null)
                {
                    Right = new IntBinarySearchTreeNode(product);
                    if (Left == null)
                    {
                        Balance = 1;
                    }
                    else
                    {
                        Balance = 0;
                    }
                }
                else
                {
                    if (Right.InsertNode(product))
                    {
                        if (Right.Balance < -1 || Right.Balance > 1)
                        {
                            Balancer.ReBalance(this, Right);
                        }
                        else
                        {
                            Balance++;
                        }
                    }
                }
            }

            if (Balance != 0)
            {
                return(true);
            }
            return(false);
        }