Ejemplo n.º 1
0
        private static void CheckBCountToRoot(RedBlackTree <int> rbTree)
        {
            List <BinNode <int> > list = rbTree.TraverseLevel(rbTree.Root);
            int count = 0;

            for (int i = 0; i < list.Count; ++i)
            {
                BinNode <int> node = list[i];
                if (null != node.LeftChild || null != node.RightChild)
                {
                    continue;
                }
                int bCount = BCountToRoot(node);
                if (count <= 0 && bCount > 0)
                {
                    count = bCount;
                }

                if (count > 0 && bCount != count)
                {
                    Console.WriteLine("CheckBCountToRoot:Error :" + node.Element.ToString());
                }
                Console.WriteLine(node.Element.ToString() + "   " + count);
            }
        }
Ejemplo n.º 2
0
        private static void CheckRR(RedBlackTree <int> rbTree)
        {
            Console.WriteLine();
            List <BinNode <int> > list = rbTree.TraverseLevel(rbTree.Root);

            for (int i = 0; i < list.Count; ++i)
            {
                BinNode <int> node = list[i];
                if (node.ParentNode != null && node.Color == Color.Red && node.ParentNode.Color == Color.Red)
                {
                    Console.WriteLine("CheckRR Error Error Error Error Error Error Error Error Error Error ");
                }
            }
        }
Ejemplo n.º 3
0
        private static void CheckRemoveWithRealRemove(RedBlackTree <int> rbTree, List <int> dataList, int index)
        {
            Console.WriteLine();

            List <BinNode <int> > list = rbTree.TraverseLevel(rbTree.Root);

            for (int i = 0; i < dataList.Count; ++i)
            {
                int           value = dataList[i];
                BinNode <int> node  = list.Find((a) => { return(a.Element.ToString().CompareTo(value.ToString()) == 0); });
                if (null == node)
                {
                    Console.WriteLine();
                    //Console.WriteLine("实际少了:" + value + "     " + (value == dataList[index]));
                    if (value != dataList[index])
                    {
                        Console.WriteLine("CheckRemoveWithRealRemove Error: Remove:" + dataList[index] + "    realRemove:" + value);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public static void Test()
        {
            RedBlackTree <int> rbTree = new RedBlackTree <int>();

            List <int> dataList = new List <int>();

            int[] arr = new int[] { 38, 10, 8, 15, 3, 25, 6, 28, 0, 30, 2, 33, 1, 36, 7, 9, 40, 55, 66, 77, 17, 20, 19, 21, 12, 13, 16, 22, };
            for (int i = 0; i < arr.Length; ++i)
            {
                Console.WriteLine();
                Console.WriteLine("Insert:" + arr[i]);
                rbTree.Insert(arr[i]);
                dataList.Add(arr[i]);

                CheckRR(rbTree);
                CheckBCountToRoot(rbTree);

                //Console.WriteLine();
                //Console.WriteLine();
                //Console.WriteLine();
                //Console.WriteLine();
                //Console.WriteLine();
            }
            BinTreeLogHelper <int> .Log(rbTree.Root, true, false);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            {
                List <BinNode <int> > list = rbTree.TraverseLevel(rbTree.Root);
                for (int i = 0; i < list.Count; ++i)
                {
                    BinNode <int> node = list[i];
                    Console.WriteLine(node.Element.ToString() + "  height:" + node.Height);
                }
            }

            //rbTree.Remove(7);
            //BinTreeLogHelper<int>.Log(rbTree.Root, true, false);

            int index = 0;

            while (dataList.Count > 0)
            {
                Random random = new Random();
                index = random.Next(0, 10000) % dataList.Count;
                //index = 0;
                //if (dataList[index] != 0 )
                //{
                //    ++index;
                //    continue;
                //}
                Console.WriteLine("Remove:" + dataList[index]);
                rbTree.Remove(dataList[index]);

                BinTreeLogHelper <int> .Log(rbTree.Root, true, false);

                Console.WriteLine();

                CheckRR(rbTree);
                Console.WriteLine();

                CheckBCountToRoot(rbTree);
                Console.WriteLine();

                CheckRemoveWithRealRemove(rbTree, dataList, index);

                dataList.RemoveAt(index);

                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine();
            }
        }