示例#1
0
        //利用插入顺序表示构造树
        public static CDFTree <char> Create(string input)
        {
            var result = new CDFTree <char>();

            foreach (var k in input)
            {
                if (k >= 'A' && k <= 'Z')
                {
                    result.Add(k);
                }
            }
            return(result);
        }
        static void Main(string[] args)
        {
            //var mySeries = new SortedList<DateTime, double>();
            //mySeries.Add(new DateTime(2011, 01, 1), 10);
            //mySeries.Add(new DateTime(2011, 01, 2), 25);
            //mySeries.Add(new DateTime(2011, 01, 3), 30);
            //mySeries.Add(new DateTime(2011, 01, 4), 45);
            //mySeries.Add(new DateTime(2011, 01, 5), 50);
            //mySeries.Add(new DateTime(2011, 01, 6), 65);

            //var calcs = new calculations();
            //var avg = calcs.MovingAverage(mySeries, 3);
            //foreach (var item in avg)
            //{
            //    Console.WriteLine("{0} {1}", item.Key, item.Value);
            //}



            // I find these codes on the web to see how Rx works.
            // Rx 中  Observable 类对 IObservable 进行了扩展,增加了一些静态,例如代码中的Interval的方法,可以供user使用。
            // Subscribe方法 不在 Rx 的命名空间之下,但是也是采用同样的方法实现了对 IObservable 的方法扩展。

            //IObservable<long> source =Observable.Interval(TimeSpan.FromSeconds(1));

            //IDisposable subscription1 = source.Subscribe(
            //                x => Console.WriteLine("Observer 1: OnNext: {0}", x),
            //                ex => Console.WriteLine("Observer 1: OnError: {0}", ex.Message),
            //                () => Console.WriteLine("Observer 1: OnCompleted"));

            //IDisposable subscription2 = source.Subscribe(
            //                x => Console.WriteLine("Observer 2: OnNext: {0}", x),
            //                ex => Console.WriteLine("Observer 2: OnError: {0}", ex.Message),
            //                () => Console.WriteLine("Observer 2: OnCompleted"));

            //Console.WriteLine("Press any key to unsubscribe");
            //subscription1.Dispose();
            //subscription2.Dispose();


            // To test observable.Range
            // testObservableRange();

            // To test Observable.Create()
            //    testObservableCreate();

            //  To test observable.Never
            //   testObservableNever();

            //     testObservableRepeat();

            //      testObservableReturn();

            // TestNever
            //     var reslt=Observable.Never<int>().Subscribe(new myob());

            // Now I can debug this code[  Observable.Range(1, 2).Aggregate((x, y) => x = x + y)], but still I can't understand the inner logic of Observable.subScribe().
            // For the reason that I see no calls for subscribe or run.Is there some problems with my debug settings??
            // There is no problem with my debug setting. The reason is that I dont call subscribe() really!!
            // Observable.Range(1, 2).Aggregate((x, y) => x = x + y).FirstOrDefault(). Debug this code will be helpful for understanding!!!

            //      Observable.Range(1, 2).Aggregate((x, y) => x = x + y).FirstOrDefault();
            //     Console.ReadLine();


            //     testBufferWithBoundries();


            /// TO TEST IEnumerable MOVEAVERAGE
            // testMoveAverageIEnumerable();

            // Move Average with RX
            //  testMoveAverageWithRx();

            // This is another example that uses Rx to implement the functin of MoveAverage
            // Note that Asympotic time complexity is O(n)
            //testMoveAverageWithRx2();

            // This is a example to finish the moveAverage function with Observable object.
            //moveAverageWithObservable();

            // There begin the function to get the empircial distribution.


            // Use the original red-black tree to coculate the CDF.
            // testECDF();

            // Use the extend data structure to get CDF.
            CDFTree <int> tree = new CDFTree <int>();

            int[] array = { 3, 10, 7, 15, 14, 35, 24, 33, 23, 21, 36, 36, 33 };
            // int[] array = { 3, 10, 7, 11, 9, 4, 2};
            for (int i = 0; i < array.Length; i++)
            {
                tree.Add(array[i], 1);
            }

            //var element = tree.getTreeInOrderWalk();
            //foreach (var elem in element)
            //{
            //    Console.Write(elem + " ");
            //}
            //Console.WriteLine();


            var list  = tree.getTreeInLayer();
            int colum = 0;

            Console.WriteLine("Root");
            foreach (var item in list)
            {
                if (item.key == default(int))
                {
                    Console.WriteLine("Column {0}", ++colum);
                }
                else if (item.isRed)
                {
                    //  subTreeSize is used to see whether we get the right subtree number
                    Console.WriteLine("   " + item.key + " Red" + " subTreeSize: " + item.subtreesize);
                }
                else
                {
                    Console.WriteLine("   " + item.key + " Black" + " subTreeSize: " + item.subtreesize);
                }
            }

            // To test CDF fand ICDF

            for (int j = 0; j < 10; j++)
            {
                Console.WriteLine(tree.CDF(j));
            }


            //TestCDFTree("");
            //TestCDFTree("A");
            //TestCDFTree("A+B");
            //TestCDFTree("B+A");
            //TestCDFTree("D[BF][ACEG]");
            //TestCDFTree("B[AD][CF][EG]");
            //TestCDFTree("F[DG][BE][AC]");
            //TestCDFTree("B[AF][DG][CE]");
            //TestCDFTree("F[BG][AD][CE]");
            //Random r = new Random();
            //for (int i = 0; i < 10; i++)
            //{
            //    int n = r.Next(0, 64);
            //    char[] cs = new char[n];
            //    for (int j = 0; j < n; j++)
            //        cs[j] = (char)('A' + r.Next(26));
            //    TestCDFTree(new string(cs));
            //}

            Console.ReadLine();
        }