public void InsertAsLCTest()
        {
            BinTree<int> tree = new BinTree<int>(new BinNode<int>(10));
            tree.InsertAsLC(tree.Root(), 15);

            Assert.IsTrue(tree.Root().LChild.Data == 15);
        }
        public void InsertAsRCTest()
        {
            BinTree<int> tree = new BinTree<int>(new BinNode<int>(10));
            tree.InsertAsRC(tree.Root(), 20);

            Assert.IsTrue(tree.Root().RChild.Data == 20);
        }
        static void Main(string[] args)
        {
            BinTreeNode<string> A = new BinTreeNode<string>("A");
            BinTreeNode<string> B = new BinTreeNode<string>("B");
            BinTreeNode<string> C = new BinTreeNode<string>("C");
            BinTreeNode<string> D = new BinTreeNode<string>("D");

            BinTree<string> BT = new BinTree<string>(A);
            BT.Insert(A, B, C);
            BT.Insert(B, D, null);
            Console.WriteLine("前序遍历:{0}", BT.PreOrderTraversal());
            Console.WriteLine("中序遍历:{0}", BT.MidOrderTraversal());
            Console.WriteLine("后序遍历:{0}", BT.PostOrderTraversal());
            Console.WriteLine("层次遍历:{0}", BT.LevelTraversal());

            BinTreeNode<string> Parent = BT.GetParent(D);
            BinTreeNode<string> LSibling = BT.GetLeftSibling(C);
            BinTreeNode<string> RSibling = BT.GetRightSibling(B);

            if (Parent != null)
                Console.WriteLine("结点{0}的Parent结点:{1}", D.Data, Parent.Data);
            else
                Console.WriteLine("结点{0}无Parent.", D.Data);

            if (LSibling != null)
                Console.WriteLine("结点{0}的左兄弟结点:{1}", C.Data, LSibling.Data);
            else
                Console.WriteLine("结点{0}无左兄弟结点.", C.Data);

            if (LSibling != null)
                Console.WriteLine("结点{0}的右兄弟结点:{1}", B.Data, RSibling.Data);
            else
                Console.WriteLine("结点{0}无右兄弟结点.", B.Data);

            BT.DeleteSubTree(D);
            Console.WriteLine("把结点{0}从二叉树中移除.", D.Data);
            Console.WriteLine("前序遍历:{0}", BT.PreOrderTraversal());
            Console.WriteLine("中序遍历:{0}", BT.MidOrderTraversal());
            Console.WriteLine("后序遍历:{0}", BT.PostOrderTraversal());
            Console.WriteLine("层次遍历:{0}", BT.LevelTraversal());

            BinTreeNode<string> E = BT.Search("A");
            if (E != null && E.LeftChild != null)
            {
                Console.WriteLine("寻找结点{0}的左孩子为{1}", E.Data, E.LeftChild.Data);
            }
            else
            {
                Console.WriteLine("未找到{0}结点或者{0}结点左孩子为null", A.Data);
            }

            Console.WriteLine("叶子结点的个数:{0}", BT.GetLeafCont());

            BT.Exchange();
            Console.WriteLine("前序遍历:{0}", BT.PreOrderTraversal());
            Console.WriteLine("中序遍历:{0}", BT.MidOrderTraversal());
            Console.WriteLine("后序遍历:{0}", BT.PostOrderTraversal());
            Console.WriteLine("层次遍历:{0}", BT.LevelTraversal());
        }
        public void SizeTest()
        {
            BinTree<int> tree = new BinTree<int>(new BinNode<int>(10));
            tree.InsertAsLC(tree.Root(), 15);
            Assert.IsTrue(tree.Size() == 2, "2");

            tree = new BinTree<int>(null);
            Assert.IsTrue(tree.Size() == 0);
        }
Beispiel #5
0
 public void Test()
 {
    BinTree<int> tree=new BinTree<int>();
    //Assert.AreEqual(1,tree.Size);
    var first=tree.InsertAsRoot(2);
    //tree.InsertAsLc(tree.Root, 1);
    //tree.InsertAsRc(tree.Root, 3);
    //tree.TravIn_I1(tree.Root,new Action<int>(i=>Console.WriteLine(i)));
 }
        public void TravInTest()
        {
            BinTree<int> tree = new BinTree<int>(new BinNode<int>(10));
            tree.InsertAsLC(tree.Root(), 15);
            tree.InsertAsLC(tree.Root().LChild, 16);
            tree.InsertAsRC(tree.Root().LChild, 17);
            tree.InsertAsRC(tree.Root(), 20);
            tree.InsertAsLC(tree.Root().RChild, 21);

            int sum = 0;
            tree.TravIn(o => sum += o);
            Assert.IsTrue(sum == 99);
        }
Beispiel #7
0
 public void TestBinTreeAddLeft()
 {
     var bintree = new BinTree<int>(8);
     bintree.AddLeft(5);
     bintree.AddLeft(6);
     bintree.AddLeft(7);
     bintree.AddLeft(8);
     bintree.AddLeft(9);
     Assert.IsTrue(bintree.Root.Left.Data == 5);
     Assert.IsTrue(bintree.Root.Left.Left.Data == 6);
     Assert.IsTrue(bintree.Root.Left.Left.Left.Data == 7);
     Assert.IsTrue(bintree.Root.Left.Left.Left.Left.Data == 8);
     Assert.IsTrue(bintree.Root.Left.Left.Left.Left.Left.Data == 9);
 }
Beispiel #8
0
        public void TestBinTreeHeigthDepth()
        {
            var bintree = new BinTree<int>(1);
            bintree.AddLeft(2);
            bintree.AddRight(3);
            bintree.AddLeft(4);
            bintree.AddRight(7);
            bintree.Root.Left.AddRight(bintree.Root.Left, new BinTreeNode<int>(5));
            bintree.Root.Right.AddLeft(bintree.Root.Right, new BinTreeNode<int>(6));

            var d = bintree.GettreeDepth();
            var h = bintree.GettreeHeigth();

            Assert.IsTrue(d == 3);
            Assert.IsTrue(h == 2);
        }
Beispiel #9
0
        public void TestBinTreeAddRight()
        {
            var bintree = new BinTree<int>(8);
            bintree.AddRight(5);
            bintree.AddRight(6);
            bintree.AddRight(7);
            bintree.AddRight(8);
            bintree.AddRight(9);
            Assert.IsTrue(bintree.Root.Right.Data == 5);
            Assert.IsTrue(bintree.Root.Right.Right.Data == 6);
            Assert.IsTrue(bintree.Root.Right.Right.Right.Data == 7);
            Assert.IsTrue(bintree.Root.Right.Right.Right.Right.Data == 8);
            Assert.IsTrue(bintree.Root.Right.Right.Right.Right.Right.Data == 9);
            var d = bintree.GettreeDepth();
            var h = bintree.GettreeHeigth();

            Assert.IsTrue(d == 6);
            Assert.IsTrue(h == 5);
        }
Beispiel #10
0
 public Zipper SetRight(BinTree binTree) => new Zipper(value, left, binTree, crumbs);
Beispiel #11
0
 public BinTree(BinTree <T> tree) : this(tree.Value, tree.Left, tree.Right)
 {
 }
Beispiel #12
0
 //$goals 9
 //$benchmark
 public void addTest(BinTree tree, int x)
 {
     if (tree!=null && tree.repOK()) {
       tree.add(x);
     }
 }
Beispiel #13
0
 public Zipper <T> SetRight(BinTree <T> binTree) => new Zipper <T>(value, left, binTree, crumbs);
Beispiel #14
0
 public static Zipper <T> FromTree(BinTree <T> tree) => new Zipper <T>(tree.Value, tree.Left, tree.Right, new List <BinTreeCrumb <T> >());
Beispiel #15
0
 public BinTree(T value, BinTree <T> left, BinTree <T> right)
 {
     Value = value;
     Left  = left;
     Right = right;
 }
 public void EmptyTest()
 {
     BinTree<int> tree = new BinTree<int>(new BinNode<int>(10));
     Assert.IsFalse(tree.Empty());
 }
Beispiel #17
0
 public bool IsSymmetric(TreeNode root)
 {
     return(BinTree.IsSymmetricUsingIterative(root));
 }
Beispiel #18
0
 public BinTree(int value, BinTree left, BinTree right)
 => (Value, Left, Right) = (value, left, right);
Beispiel #19
0
 public Fork(BinTree left, BinTree right)
 {
     Left  = left;
     Right = right;
 }
Beispiel #20
0
 public void GetLastNodeInStep()
 {
     Assert.AreEqual(6, BinTree.GetIndexLastNodeInStep(3));
     Assert.AreEqual(3, BinTree.GetIndexLastNodeInStep(2));
 }
Beispiel #21
0
 public void GetFirstNodeInStep()
 {
     Assert.AreEqual(4, BinTree.GetIndexFirstNodeInStep(3));
     Assert.AreEqual(2, BinTree.GetIndexFirstNodeInStep(2));
 }
 public void UpdateHeightTest()
 {
     BinTree<int> tree = new BinTree<int>(new BinNode<int>(10));
     tree.InsertAsLC(tree.Root(), 15);
     Assert.IsTrue(tree.Root().LChild.Height == 0, "lchild");
     tree.InsertAsLC(tree.Root().LChild, 16);
     tree.UpdateHeightAbove(tree.Root().LChild);
     Assert.IsTrue(tree.Root().LChild.Height == 1);
 }
 public void BinTreeTest()
 {
     BinTree<int> tree = new BinTree<int>(new BinNode<int>(10));
     Assert.IsTrue(tree != null);
 }
Beispiel #24
0
 public BinTreeRightCrumb(T value, BinTree <T> tree) : base(value, tree)
 {
 }
 public void HeightTest()
 {
     BinTree<int> tree = new BinTree<int>(new BinNode<int>(10));
     tree.InsertAsLC(tree.Root(), 15);
     Assert.IsTrue(tree.Height() == 1);
 }
Beispiel #26
0
 /// <summary>
 /// Merge basins using using MapWinGIS.Utils
 /// </summary>
 /// <param name="shed"></param>
 /// <param name="drainage"></param>
 /// <returns></returns>
 private static IFeature mergeBasinsByDrainage(IFeatureSet shed, BinTree drainage)
 {
     if (drainage == null) return null;
     IFeature left = mergeBasinsByDrainage(shed, drainage.left);
     IFeature right = mergeBasinsByDrainage(shed, drainage.right);
     IFeature lr = mergeAbuttingPolygons(left, right);
     IFeature outlet = shed.get_Shape(drainage.val);
     // check for multipart shape
     if (outlet.NumGeometries > 1)
     {
         Trace.WriteLine("Subbasin " + drainage.val.ToString() + " has " +
                                   outlet.NumGeometries.ToString() + " parts");
     }
     else
     {
         // check for anticlockwise polygon
         double area = SignedArea(outlet);
         if (area < 0)
         {
             Trace.WriteLine("Needed to reverse subbasin " + drainage.val.ToString());
             outlet = ReverseShape(outlet);
         }
     }
     return mergeAbuttingPolygons(lr, outlet);
 }
Beispiel #27
0
 public BinTreeCrumb(T value, BinTree <T> tree)
 {
     Value = value;
     Tree  = tree;
 }
		internal void SetCoderProperties(CoderPropID[] propIDs, object[] properties)
		{
			for (UInt32 i = 0; i < properties.Length; i++)
			{
				object prop = properties[i];
				switch (propIDs[i])
				{
					case CoderPropID.NumFastBytes:
					{
						if (!(prop is Int32))
							throw new InvalidParamException();
						Int32 numFastBytes = (Int32)prop;
						if (numFastBytes < 5 || numFastBytes > Base.kMatchMaxLen)
							throw new InvalidParamException();
						_numFastBytes = (UInt32)numFastBytes;
						break;
					}
					case CoderPropID.Algorithm:
					{
						/*
						if (!(prop is Int32))
							throw new InvalidParamException();
						Int32 maximize = (Int32)prop;
						_fastMode = (maximize == 0);
						_maxMode = (maximize >= 2);
						*/
						break;
					}
					case CoderPropID.MatchFinder:
					{
						if (!(prop is String))
							throw new InvalidParamException();
						EMatchFinderType matchFinderIndexPrev = _matchFinderType;
						int m = FindMatchFinder(((string)prop).ToUpper());
						if (m < 0)
							throw new InvalidParamException();
						_matchFinderType = (EMatchFinderType)m;
						if (_matchFinder != null && matchFinderIndexPrev != _matchFinderType)
							{
							_dictionarySizePrev = 0xFFFFFFFF;
							_matchFinder = null;
							}
						break;
					}
					case CoderPropID.DictionarySize:
					{
						const int kDicLogSizeMaxCompress = 30;
						if (!(prop is Int32))
							throw new InvalidParamException(); ;
						Int32 dictionarySize = (Int32)prop;
						if (dictionarySize < (UInt32)(1 << Base.kDicLogSizeMin) ||
							dictionarySize > (UInt32)(1 << kDicLogSizeMaxCompress))
							throw new InvalidParamException();
						_dictionarySize = (UInt32)dictionarySize;
						int dicLogSize;
						for (dicLogSize = 0; dicLogSize < (UInt32)kDicLogSizeMaxCompress; dicLogSize++)
							if (dictionarySize <= ((UInt32)(1) << dicLogSize))
								break;
						_distTableSize = (UInt32)dicLogSize * 2;
						break;
					}
					case CoderPropID.PosStateBits:
					{
						if (!(prop is Int32))
							throw new InvalidParamException();
						Int32 v = (Int32)prop;
						if (v < 0 || v > (UInt32)Base.kNumPosStatesBitsEncodingMax)
							throw new InvalidParamException();
						_posStateBits = (int)v;
						_posStateMask = (((UInt32)1) << (int)_posStateBits) - 1;
						break;
					}
					case CoderPropID.LitPosBits:
					{
						if (!(prop is Int32))
							throw new InvalidParamException();
						Int32 v = (Int32)prop;
						if (v < 0 || v > (UInt32)Base.kNumLitPosStatesBitsEncodingMax)
							throw new InvalidParamException();
						_numLiteralPosStateBits = (int)v;
						break;
					}
					case CoderPropID.LitContextBits:
					{
						if (!(prop is Int32))
							throw new InvalidParamException();
						Int32 v = (Int32)prop;
						if (v < 0 || v > (UInt32)Base.kNumLitContextBitsMax)
							throw new InvalidParamException(); ;
						_numLiteralContextBits = (int)v;
						break;
					}
					case CoderPropID.EndMarker:
					{
						if (!(prop is Boolean))
							throw new InvalidParamException();
						SetWriteEndMarkerMode((Boolean)prop);
						break;
					}
					default:
						throw new InvalidParamException();
				}
			}
		}
Beispiel #29
0
 //$goals 5
 //$benchmark
 public void findTest(BinTree tree, int x)
 {
     boolean ret_val;
     if (tree!=null && tree.repOK()) {
       ret_val = tree.find(x);
     }
 }
Beispiel #30
0
 public Zipper <T> SetLeft(BinTree <T> binTree) => new Zipper <T>(value, binTree, right, crumbs);
Beispiel #31
0
 /// <summary>
 /// Merge basins using IGeometry, and only converting each shape once
 /// </summary>
 /// <param name="shed"></param>
 /// <param name="drainage"></param>
 /// <returns></returns>
 private static IGeometry mergeBasinsByDrainageI(Shapefile shed, BinTree drainage)
 {
     if (drainage == null) return null;
     IGeometry left = mergeBasinsByDrainageI(shed, drainage.left);
     IGeometry right = mergeBasinsByDrainageI(shed, drainage.right);
     IFeature outlet = shed.get_Shape(drainage.val);
     // will this work?
     IGeometry outg = outlet.BasicGeometry as IGeometry;
     if (left == null)
     {
         if (right == null)
             return outg;
         else
             return right.Union(outg);
     }
     else
     {
         if (right == null)
             return left.Union(outg);
         else
             return left.Union(right.Union(outg));
     }
 }
Beispiel #32
0
 public static Zipper FromTree(BinTree tree) => new Zipper(tree.Value, tree.Left, tree.Right, new List <BinTreeCrumb>());
Beispiel #33
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="v">node value</param>
 /// <param name="l">left subtree</param>
 /// <param name="r">right subtree</param>
 public BinTree(int v, BinTree l, BinTree r)
 {
     val = v;
     left = l;
     right = r;
 }
Beispiel #34
0
 public BinTreeCrumb(int value, BinTree tree)
 {
     Value = value;
     Tree  = tree;
 }
		void Create()
		{
			if (_matchFinder == null)
			{
				BinTree bt = new BinTree();
				int numHashBytes = 4;
				if (_matchFinderType == EMatchFinderType.BT2)
					numHashBytes = 2;
				bt.SetType(numHashBytes);
				_matchFinder = bt;
			}
			_literalEncoder.Create(_numLiteralPosStateBits, _numLiteralContextBits);

			if (_dictionarySize == _dictionarySizePrev && _numFastBytesPrev == _numFastBytes)
				return;
			_matchFinder.Create(_dictionarySize, kNumOpts, _numFastBytes, Base.kMatchMaxLen + 1);
			_dictionarySizePrev = _dictionarySize;
			_numFastBytesPrev = _numFastBytes;
		}
Beispiel #36
0
 public BinTreeLeftCrumb(int value, BinTree tree) : base(value, tree)
 {
 }
Beispiel #37
0
 //$goals 17
 //$benchmark
 public void removeTest(BinTree tree, BinTreeNode z)
 {
     BinTreeNode ret_val;
     if (tree!=null && z!=null && tree.repOK()) {
       ret_val = tree.remove(z);
     }
 }
Beispiel #38
0
 public BinTreeRightCrumb(int value, BinTree tree) : base(value, tree)
 {
 }
Beispiel #39
0
 public Zipper SetLeft(BinTree binTree) => new Zipper(value, binTree, right, crumbs);
Beispiel #40
0
        /// <summary>
        /// Datos de las betas segun el percentil del valor del arbol (beta en 10% sup, 20% sup, ...)
        /// </summary>
        private static void BetaGraph1()
        {
            int    numberOfSteps     = 2000;
            double InitialStockPrice = 100;
            double Volatility        = 0.2;
            double StrikePrice       = 105;
            double TotalTimeYears    = 0.5;
            double RiskFree          = 0.03;
            double MarketRiskPremium = 0.06;
            double StockBeta         = 1;

            var tree = new CapmBinTree(numberOfSteps);

            tree.InitialStockPrice = InitialStockPrice;
            tree.Volatility        = Volatility;
            tree.StrikePrice       = StrikePrice;
            tree.TotalTimeYears    = TotalTimeYears;
            tree.RiskFree          = RiskFree;
            tree.MarketRiskPremium = MarketRiskPremium;
            tree.StockBeta         = StockBeta;

            Console.WriteLine("Calculating stock prices...");
            tree.CalculateStockPrices();

            Console.WriteLine("Calculating option prices...");
            tree.CalculateOptionPrices();

            Console.WriteLine("Outputing the betas...");

            int divisions = 70;

            using (var writer = new StreamWriter("beta_graph_1.csv", false))
            {
                writer.Write("Step");
                for (int i = 1; i <= divisions; i++)
                {
                    writer.Write(",n" + i);
                }
                writer.WriteLine();


                var indexes = new List <int>(divisions);
                for (int step = 1; step < numberOfSteps; step++)
                {
                    int nodeCount = BinTree.GetIndexLastNodeInStep(step) - BinTree.GetIndexFirstNodeInStep(step);
                    indexes.Clear();
                    for (int stp = 0; stp < divisions; stp++)
                    {
                        indexes.Add(nodeCount * stp / divisions);
                    }

                    writer.Write(step + ",");

                    int i = 0;
                    foreach (var node in tree.IterateNodesOfStep(step))
                    {
                        if (indexes.Contains(i))
                        {
                            if (node.OptionBeta <= 0)
                            {
                                break;
                            }
                            writer.Write(node.OptionBeta + ",");
                        }
                        i++;
                    }
                    writer.WriteLine();
                }
            }

            Console.WriteLine("Done. See: beta_graph_1.csv");
        }