예제 #1
0
        /// <summary>
        /// 删除
        /// </summary>
        public override bool Remove(T t)
        {
            if (null == Root)
            {
                return(false);
            }
            BinNode <T> node = Search(t);

            //若树空或目标不存在,则无法删除
            if (null == node || node.Element.CompareTo(t) != 0)
            {
                return(false);
            }

            BinTreeLogHelper <T> .Log(Root, false, false);

            Console.WriteLine();

            BinNode <T> tempRoot = Root; //assert: 经search()后节点e已被伸展至树根

            if (!Root.HasLChild())       //若无左子树,则直接删除
            {
                Root = Root.RightChild;
                if (null != Root)
                {
                    Root.ParentNode = null;
                }
            }
            else if (!Root.HasRChild())  //若无右子树,也直接删除
            {
                Root = Root.LeftChild;
                if (null != Root)
                {
                    Root.ParentNode = null;
                }
            }
            else
            { //若左右子树同时存在,则
                BinNode <T> lTree = Root.LeftChild;
                lTree.ParentNode = null;
                Root.LeftChild   = null;  //暂时将左子树切除
                Root             = Root.RightChild;
                Root.ParentNode  = null;  //只保留右子树
                Search(tempRoot.Element); //以原树根为目标,做一次(必定失败的)查找
                ///// assert: 至此,右子树中最小节点必伸展至根,且(因无雷同节点)其左子树必空,于是
                Root.LeftChild   = lTree;
                lTree.ParentNode = Root; //只需将原左子树接回原位即可
            }

            if (null != Root)
            {
                UpdateHeight(Root); //此后,若树非空,则树根的高度需要更新
            }
            return(true);           //返回成功标志
        }
예제 #2
0
        public static void Test()
        {
            AVLTree <int> aVLTree = new AVLTree <int>();

            int[] arr = new int[] { 10, 8, 15, 17, 20, 19, 21, 12, 13, 6, 9, 16, 22, };
            for (int i = 0; i < arr.Length; ++i)
            {
                Console.WriteLine("Insert:" + arr[i]);
                aVLTree.Insert(arr[i]);
                Console.WriteLine("===============================================");
                BinTreeLogHelper <int> .Log(aVLTree.Root, false, false);

                Console.WriteLine("===============================================");
                List <BinNode <int> > list = aVLTree.TraverseLevel(aVLTree.Root);
                Console.WriteLine();

                for (int n = 0; n < list.Count; ++n)
                {
                    BinNode <int> node = list[n];
                    Console.WriteLine(list[n].Element.ToString() + "   heigh:" + list[n].Height);
                }
            }

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

            Console.WriteLine("===============================================");

            Console.WriteLine("Star Remove ===================================");
            for (int i = 0; i < arr.Length; ++i)
            {
                aVLTree.Remove(arr[i]);
                Console.WriteLine("===============================================");
                BinTreeLogHelper <int> .Log(aVLTree.Root, false, false);

                Console.WriteLine("===============================================");
                List <BinNode <int> > list = aVLTree.TraverseLevel(aVLTree.Root);
                Console.WriteLine();

                Console.WriteLine();
                for (int n = list.Count - 1; n >= 0; --n)
                {
                    BinNode <int> node  = list[n];
                    int           heigh = node.Height;
                    //aVLTree.UpdateHeight(node);
                    //if (heigh == node.Height)
                    //{
                    //    Console.WriteLine(list[n].Value.ToString() + "  heigh:" + list[n].Height + "    Error Error Error Error Error Error");
                    //}
                    Console.WriteLine(list[n].Element.ToString() + "  heigh:" + heigh + "   " + (node.Height == heigh));
                }
            }
        }
예제 #3
0
        //删除叶子节点失败
        private static void TestRemove(int[] arr, int removeIndex)
        {
            BSTree <int> bSTree = new BSTree <int>();

            for (int i = 0; i < arr.Length; ++i)
            {
                bSTree.Insert(arr[i]);
            }

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

            Console.WriteLine("Remove:" + arr[removeIndex]);
            bSTree.Remove(arr[removeIndex]);
            BinTreeLogHelper <int> .Log(bSTree.Root, false, false);

            Console.WriteLine();
            LogBinTreeCheck <int> .Check(bSTree.TraverseLevel(bSTree.Root));

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();
        }
예제 #4
0
        public static void Test()
        {
            SplayTree <int> splayTree = new SplayTree <int>();

            int[] arr = new int[] { 22, 10, 8, 15, 17, 20, 19, 21, 12, 13 };
            //int[] arr = new int[] { 10, 20, 8, 19, 15, 17, 21};
            //int[] arr = new int[] { 10, 20, 8, 19};
            for (int i = 0; i < arr.Length; ++i)
            {
                //Console.WriteLine("Insert:" + arr[i]);
                splayTree.Insert(arr[i]);
                //BinTreeLogHelper<int>.Log(splayTree.Root, true);
                //Console.WriteLine();

                List <BinNode <int> > list = new List <BinNode <int> >();
                LogBinTreeCheck <int> .Check(list);

                //
                //list = splayTree.TraverseLevel(splayTree.Root);
                //for (int j = 0; j < list.Count; ++j)
                //{
                //    BinNode<int> ttt = list[j];

                //    int data = ttt.Value;
                //    while (ttt.ParentNode != null)
                //    {
                //        ttt = ttt.ParentNode;
                //        if (data == ttt.Value)
                //        {
                //            int a = 0;
                //        }
                //    }
                //}
            }

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

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

            for (int i = 0; i < arr.Length; ++i)
            {
                Console.WriteLine("Remove:" + arr[i]);
                splayTree.Remove(arr[i]);
                BinTreeLogHelper <int> .Log(splayTree.Root, false, false);

                Console.WriteLine();
                List <BinNode <int> > list = splayTree.TraverseLevel(splayTree.Root);
                Console.WriteLine();
            }

            //BinNode<int> root = splayTree.Insert(8);
            //BinNode<int> node10 = new BinNode<int>(10);
            //root.InsertAsRc(node10);

            //BinNode<int> node20 = new BinNode<int>(20);
            //node10.InsertAsRc(node20);

            //BinTreeLogHelper<int>.Log(splayTree.Root, true);

            ////splayTree.Search(19);
            ////BinTreeLogHelper<int>.Log(splayTree.Root, true);

            //Console.WriteLine();
            //Console.WriteLine();
            //splayTree.Insert(19);
            //BinTreeLogHelper<int>.Log(splayTree.Root, true);
        }
예제 #5
0
        public static void Test()
        {
            List <BinNode <int> > list = new List <BinNode <int> >();

            BSTree <int> bsTree = new BSTree <int>();

            for (int i = 0; i < 9; ++i)
            {
                bsTree.Insert(i);
            }
            BinTreeLogHelper <int> .Log(bsTree.Root, false, false);

            //{
            //    int[] aaaRR = new int[] { 16, 9, 6, 22 };s
            //    BSTree<int> bSTree = new BSTree<int>();
            //    for (int i = 0; i < aaaRR.Length; ++i)
            //    {
            //        bSTree.Insert(aaaRR[i]);
            //    }
            //    BinTreeLogHelper<int>.Log(bSTree.Root, false);
            //    Console.WriteLine();
            //    list = bSTree.TraverseLevel(bSTree.Root);
            //    Console.WriteLine();

            //    for (int n = 0; n < list.Count; ++n)
            //    {
            //        BinNode<int> node = list[n];
            //        Console.WriteLine(list[n].Value.ToString() + "   heigh:" + list[n].Height + "   deep:" + list[n].Deep);
            //        int deep = -1;
            //        while (null != node)
            //        {
            //            ++deep;
            //            node = node.ParentNode;
            //        }
            //        if (deep != list[n].Deep)
            //        {
            //            Console.WriteLine(list[n].Value.ToString() + "  deep:" + list[n].Deep + "    Error Error Error Error Error Error");
            //        }
            //    }

            //    bSTree.Remove(6);
            //    BinTreeLogHelper<int>.Log(bSTree.Root, false);

            //    Console.WriteLine();
            //    list = bSTree.TraverseLevel(bSTree.Root);
            //    Console.WriteLine();

            //    for (int n = 0; n < list.Count; ++n)
            //    {
            //        BinNode<int> node = list[n];
            //        Console.WriteLine(list[n].Value.ToString() + "   heigh:" + list[n].Height + "   deep:" + list[n].Deep);
            //        int deep = -1;
            //        while (null != node)
            //        {
            //            ++deep;
            //            node = node.ParentNode;
            //        }
            //        if (deep != list[n].Deep)
            //        {
            //            Console.WriteLine(list[n].Value.ToString() + "  deep:" + list[n].Deep + "    Error Error Error Error Error Error");
            //        }
            //    }
            //}

            int[] arr = new int[] { 10, 8, 15, 17, 20, 19, 21, 12, 13, 6, 9, 16, 22, };
            //for (int i = 0; i < arr.Length; ++i)
            //{
            //    TestRemove(arr, i);
            //}

            {
                BSTree <int> bSTree = new BSTree <int>();
                for (int i = 0; i < arr.Length; ++i)
                {
                    bSTree.Insert(arr[i]);
                    //BinTreeLogHelper<int>.Log(bSTree.Root, false);

                    //Console.WriteLine();
                    //list = bSTree.TraverseLevel(bSTree.Root);
                    //Console.WriteLine();

                    //for (int n = 0; n < list.Count; ++n)
                    //{
                    //    BinNode<int> node = list[n];
                    //    Console.WriteLine(list[n].Value.ToString() + "   heigh:" + list[n].Height + "   deep:" + list[n].Deep);
                    //    int deep = -1;
                    //    while (null != node)
                    //    {
                    //        ++deep;
                    //        node = node.ParentNode;
                    //    }
                    //    if (deep != list[n].Deep)
                    //    {
                    //        Console.WriteLine(list[n].Value.ToString() + "  deep:" + list[n].Deep + "    Error Error Error Error Error Error");
                    //    }
                    //}

                    //list = bSTree.TraverseLevel(bSTree.Root);
                    //Console.WriteLine();

                    //for (int n = 0; n < list.Count; ++n)
                    //{
                    //    BinNode<int> node = list[n];
                    //    int heigh = node.Height;
                    //    //bSTree.UpdateHeight(node);
                    //    if (heigh != node.Height)
                    //    {
                    //        Console.WriteLine(list[n].Value.ToString() + "  heigh:" + list[n].Height + "    Error Error Error Error Error Error");
                    //    }
                    //}
                }
                BinTreeLogHelper <int> .Log(bSTree.Root, false, false);

                Console.WriteLine();
                Console.WriteLine();
                //list = bSTree.TraverseLevel(bSTree.Root);
                //for (int n = 0; n < list.Count; ++n)
                //{
                //    BinNode<int> node = list[n];
                //    int deep = -1;
                //    while (null != node)
                //    {
                //        ++deep;
                //        node = node.ParentNode;
                //    }
                //    if (deep != list[n].Deep)
                //    {
                //        Console.WriteLine(list[n].Value.ToString() + "  deep:" + list[n].Deep + "    Error Error Error Error Error Error");
                //    }
                //}
                Console.WriteLine();

                for (int i = 0; i < arr.Length; ++i)
                {
                    Console.WriteLine("Remove:" + arr[i]);
                    bSTree.Remove(arr[i]);
                    BinTreeLogHelper <int> .Log(bSTree.Root, false, false);

                    Console.WriteLine();
                    Console.WriteLine();
                    Console.WriteLine();
                    LogBinTreeCheck <int> .Check(bSTree.TraverseLevel(bSTree.Root));

                    Console.WriteLine();

                    list = bSTree.TraverseLevel(bSTree.Root);

                    Console.WriteLine();
                    for (int n = list.Count - 1; n >= 0; --n)
                    {
                        BinNode <int> node  = list[n];
                        int           heigh = node.Height;
                        //bSTree.UpdateHeight(node);
                        if (heigh == node.Height)
                        {
                            Console.WriteLine(list[n].Element.ToString() + "  heigh:" + list[n].Height + "    Error Error Error Error Error Error");
                        }
                    }
                    //for (int n = 0; n < list.Count; ++n)
                    //{
                    //    BinNode<int> node = list[n];
                    //    int heigh = node.Height;
                    //    bSTree.UpdateHeight(node);
                    //    if (heigh != node.Height)
                    //    {
                    //        Console.WriteLine(list[n].Value.ToString() + "  heigh:" + list[n].Height + "    Error Error Error Error Error Error");
                    //    }
                    //}
                    Console.WriteLine();
                }
            }

            //{
            //    BSTree<int> bSTree = new BSTree<int>();
            //    bSTree.Insert(10);
            //    BinTreeLogHelper<int>.Log(bSTree.Root, false);
            //    Console.WriteLine();

            //    bSTree.Remove(10);
            //    BinTreeLogHelper<int>.Log(bSTree.Root, false);
            //}
        }
예제 #6
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();
            }
        }