//利用插入顺序表示构造树 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); }
private static void TestCDFTree(string testInput) { Console.WriteLine("Testing: {0}", testInput); string expectedOutput = SortedSet <char> .Serielize(SortedSet <char> .Create(testInput), k => k.ToString()); string testOutput = CDFTree <char> .Serielize(CDFTree <char> .Create(testInput), k => k.ToString()); Console.WriteLine("Expecting: {0}", expectedOutput); Console.WriteLine("Get: {0}", testOutput); if (testOutput.Equals(expectedOutput)) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Pass!"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Fail!"); } Console.ResetColor(); Console.WriteLine(); }
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(); }
//为了检验红黑树结构正确性编写了此测试伪代码 //首先约定树的简易表示方法(因为画树结构比较困难) //用单个字母表示树的节点 //为了方便讨论,约定树节点的表示字母的顺序和树节点键值顺序相同 //用括号表示树的层次 //下面用例子解释上面的概念 //A,可以表示一个节点,也可以表示根节点为A的树,且A的左节点和右节点未作限制,可以为空,也可以很复杂而省略未写 //(A)B,可以表示根节点为B的树,且B的左节点为A //A(B),可以表示根节点为A的树,且A的右节点为B //ABC,可以表示根节点为B的树,且B的左节点为A,B的右节点为C //(ABC)DE的根是D,D的左节点是树ABC,D的右节点是E //AB(CDE)的根是B,B的左节点是A,B的右节点是树CDE //值得注意的是,这种表示方法删除括号后获得的节点序列和中序遍历结果相同 //另外需要注意的是如果去掉括号后的序列未按字母顺序则原树不是合法的树 //新节点插入 //我们这里的插入为简单搜索插入,不做平衡处理 //这样,给定插入顺序就应该得到确定的树结构,我们用+表示插入,用N表示空树 //则应当有下面的测试可以编写 //N+A=>A //A+B=>A(B) //B+A=>(A)B //当连续插入且不影响输出树结构时,将连续插入且顺序可调的组用方括号括起并省略+ //B[AC]=>ABC //B[CA]=>ABC //B[AF][DG][CE]=>AB((CDE)FG) //D[BF][ACEG]=>(ABC)D(EFG) //不难发现,这种插入顺序的表示方法,如果每个顺序可调组我们按字母顺序列出,则整个表示和从根开始广度优先遍历的顺序相同 //左旋和右旋 //L()为左旋函数,输入为一个树旋转前的根节点,输出为该树左旋后的新根节点 //因此L(AB(CDE))=>(ABC)DE //与左旋函数类似,R()为右旋函数 //R((ABC)DE)=>AB(CDE) //平衡算法 //平衡算法的核心是要获得(ABC)D(EFG)的平衡形式 //L(AB(CD(EFG)))=>(ABC)D(EFG) //R(((ABC)DE)FG))=>(ABC)D(EFG) //L(R(AB((CDE)FG)))=>(ABC)D(EFG) //R(L((AB(CDE))FG))=>(ABC)D(EFG) //前两个是所谓zig-zig和zag-zag树,只需旋转一次即可平衡 //后两个是所谓zig-zag和zag-zig树,需要先旋转成zig-zig和zag-zag再旋转平衡 //按上面的方法表示树,节点字母用toString函数获得 //测试时T可以直接为string,即用字母作为键值 //这样通过比较字符串就可以直接比较树结构,非常方面,输出也比较直观 public static string Serielize(CDFTree <T> tree, Func <T, string> toString) { return(serielizeHelper(tree.root, toString).Item1); }