Ejemplo n.º 1
0
    public void SetOrderBST(string customerName, List <Item> orderItems)
    {
        int     orderId = customerName.GetHashCode();
        BSTNode node    = tree.Find(orderId);

        if (node == null)
        {
            Order order = new Order()
            {
                id         = orderId,
                clientName = customerName,
                items      = new List <Item>()
            };

            tree.Insert(orderId, order);
        }
        else
        {
            orderId = node.GetData <Order>().clientName.GetHashCode();
            node.SetData(new Order
            {
                id         = orderId,
                clientName = customerName,
                items      = orderItems
            }, orderId);
        }
        List <Order> nodes = new List <Order>();

        tree.PreorderTraversal(nodes);
        OrderDataManager.UpdateOrders(nodes);
    }
    public void SetItemBST(string itemName, int itemQuantity, float itemPrice, bool isOnSale, float salePercent)
    {
        int     itemID = itemName.GetHashCode();
        BSTNode node   = tree.Find(itemID);

        if (node == null)
        {
            Item item = new Item()
            {
                id          = itemID,
                name        = itemName,
                price       = itemPrice,
                quantity    = itemQuantity,
                isOnSale    = isOnSale,
                salePercent = salePercent
            };
            tree.Insert(itemID, item);
        }
        else
        {
            itemID = node.GetData <Item>().name.GetHashCode();
            node.SetData(new Item
            {
                id          = itemID,
                name        = node.GetData <Item>().name,
                price       = itemPrice,
                quantity    = itemQuantity,
                isOnSale    = isOnSale,
                salePercent = salePercent
            }, itemID);
        }
        List <Item> nodes = new List <Item>();

        tree.PreorderTraversal(nodes);
        StockDataManager.UpdateStock(nodes);
    }
Ejemplo n.º 3
0
        static void testBSTTree()
        {
            int treenodenum = 20;

            int[] vals = new int[treenodenum];
            for (int i = 0; i < treenodenum; i++)
            {
                vals[i] = random.Next(treenodenum);
            }

            Console.WriteLine("构建树节点:");
            printintarray(vals);
            Console.WriteLine("");

            BSTTree bsttree = new BSTTree();

            bsttree.Construct(vals);

            Console.WriteLine("先序遍历结果:");
            Console.WriteLine("递归调用先序遍历结果:");
            var sequence = bsttree.DLRSequence();

            printBinaryNode(sequence);
            Console.WriteLine("");

            Console.WriteLine("中序遍历结果:");
            sequence = bsttree.LDRSequence();
            printBinaryNode(sequence);
            Console.WriteLine("");


            for (int i = 0; i < treenodenum; i++)
            {
                var findval = random.Next(treenodenum);
                Console.WriteLine(String.Format("查找数字为:{0}", findval));
                var node = bsttree.Find(findval);
                if (node == null)
                {
                    Console.WriteLine(String.Format("未能查找到数字:{0}", findval));
                }
                else
                {
                    Console.WriteLine(String.Format("成功查找到数字:{0}", node.nodeValue));
                }

                if (node != null)
                {
                    var bstval = node.nodeValue;
                    Console.WriteLine(String.Format("测试删除节点:{0}", bstval));
                    var bok = bsttree.DelNode(node.nodeValue);
                    if (bok)
                    {
                        Console.WriteLine(String.Format("成功删除节点:{0}", bstval));
                    }
                    else
                    {
                        Console.WriteLine(String.Format("删除节点:{0} 失败", bstval));
                    }

                    Console.WriteLine("先序遍历结果:");
                    Console.WriteLine("递归调用先序遍历结果:");
                    sequence = bsttree.DLRSequence();
                    printBinaryNode(sequence);
                    Console.WriteLine("");

                    Console.WriteLine("中序遍历结果:");
                    sequence = bsttree.LDRSequence();
                    printBinaryNode(sequence);
                    Console.WriteLine("");
                }
            }
        }