Exemplo n.º 1
0
        /// <summary>
        /// 后序遍历
        /// 先左节点,然后右节点,后根节点
        /// </summary>
        public void PostOrderNo()
        {
            if (tree == null)
            {
                return;
            }
            HashSet <Tree> visited = new HashSet <Tree>();

            System.Collections.Generic.Stack <Tree> stack = new System.Collections.Generic.Stack <Tree>();
            Tree node = tree;

            while (stack.Any())
            {
                node = stack.Peek();
                if (node != null)
                {
                    stack.Push(node);
                    node = node.Left;
                }
                else
                {
                    var item = stack.Peek();
                    if (item.Right != null && !visited.Contains(item.Right))
                    {
                        node = item.Right;
                    }
                    else
                    {
                        System.Console.Write(node.Data + " ");
                        visited.Add(item);
                        stack.Pop();
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 先需遍历
        /// 先根节点,然后左节点,后右节点
        /// </summary>
        public void Preorder()
        {
            if (tree == null)
            {
                return;
            }

            System.Collections.Generic.Stack <Tree> stack = new System.Collections.Generic.Stack <Tree>();
            Tree node = tree;

            while (node != null || stack.Any())
            {
                if (node != null)
                {
                    stack.Push(node);
                    System.Console.Write(node.Data + " ");
                    node = node.Left;
                }
                else
                {
                    var item = stack.Pop();
                    node = item.Right;
                }
            }
        }
Exemplo n.º 3
0
        public double calc(int amount, string fromUnit, string toUnit)
        {
            var from = (Units) Enum.Parse(typeof (Units), fromUnit.Replace("in", "inch"));
            var to = (Units) Enum.Parse(typeof (Units), toUnit.Replace("in", "inch"));

            var dgraf = new Graf<Units>();

            var ftVertex = new Vertex<Units>(Units.ft);
            var inVertex = new Vertex<Units>(Units.inch);
            var ydVertex = new Vertex<Units>(Units.yd);
            var miVertex = new Vertex<Units>(Units.mi);

            ftVertex.AddDirectedEdge(12, inVertex);
            inVertex.AddDirectedEdge(1D / 12, ftVertex);

            ydVertex.AddDirectedEdge(3, ftVertex);
            ftVertex.AddDirectedEdge(1D / 3, ydVertex);

            miVertex.AddDirectedEdge(1760, ydVertex);
            ydVertex.AddDirectedEdge(1D / 1760, miVertex);

            dgraf.AddVertex(ftVertex);
            dgraf.AddVertex(inVertex);
            dgraf.AddVertex(ydVertex);
            dgraf.AddVertex(miVertex);

            var source = dgraf.Vertexes.FirstOrDefault(vertex => vertex.Key == from);
            var target = dgraf.Vertexes.FirstOrDefault(vertex => vertex.Key == to);

            var vertexPath = dgraf.GetBreadthPath(source, target).ToList();

            var edgePath = new System.Collections.Generic.Stack<Edge<Units>>();
            if (target.Color != Vertex<Units>.VertexColor.Black)
                return 0;

            var currentVertex = target;
            while (currentVertex != null && currentVertex != source && currentVertex.Parent != null)
            {
                var currentEdge = currentVertex.Parent.Edges.FirstOrDefault(edge => edge.Target == currentVertex);
                edgePath.Push(currentEdge);
                currentVertex = currentVertex.Parent;
            }

            if (source == target)
                return amount;
            if (!edgePath.Any())
                return 0;

            double res = amount;
            double weigth = 1;
            foreach (var pathEdge in edgePath)
                    weigth *= pathEdge.Weigth;
            return res * weigth;
        }