Пример #1
0
        public string ConvertToTitle(int n)
        {
            Stack<char> s = new Stack<char> ();

            while (n > 0)
            {
                n--;
                int remainder = n % 26;
                s.Push ((char)(65 + remainder));
                n = (int) n / 26;
            }

            return string.Join("",s.ToList());
        }
Пример #2
0
        public List<int> PathTo(int s)
        {
            if (!HasPathTo(s)) {
                return null;
            }
            Stack<int> stack = new Stack<int> ();
            stack.Push (s);
            int temp = s;
            while (v != edgeTo[temp]) {
                stack.Push (edgeTo[temp]);
                temp = edgeTo [temp];
            }
            stack.Push (v);

            return stack.ToList();
        }
        private void SetChildren(QueryNode parent)
        {
            var args = new Stack<QueryNode>();
            // remove all nodes from expression until we find the parent
            while (this.filterExpression.Peek() != parent)
            {
                args.Push(this.filterExpression.Pop());
            }

            parent.SetChildren(args.ToList());
        }
Пример #4
0
        public void TreeLevelOrderPrintBottomUp(TreeNode root)
        {
            Queue<TreeNode> q = new Queue<TreeNode>();
            Stack<string> stack = new Stack<string> ();

            string tmp = "";

            int curCount = 1;
            int nextCount = 0;

            if (root == null) {
                return;
            }

            q.Enqueue (root);

            while (q.Count > 0) {
                var cur = q.Dequeue ();
                curCount -= 1;

                tmp += cur.val + " ";

                var left = cur.left;
                var right = cur.right;

                if (left != null) {
                    q.Enqueue (left);
                    nextCount += 1;
                }
                if (right != null) {
                    q.Enqueue (right);
                    nextCount += 1;
                }
                if (curCount == 0) {
                    stack.Push (tmp.TrimEnd(' '));
                    tmp = "";
                    curCount = nextCount;
                    nextCount = 0;
                }

            }

            foreach (var item in stack.ToList()) {
                Console.WriteLine (item);
            }
        }
Пример #5
0
            /// <summary>
            /// Получение текущих ребер для данного ветвления
            /// </summary>
            /// <param name="branch">ветвление</param>
            /// <returns>список ребер ветлений</returns>
            public List<Digraph.Edge> GetEdgesBranching(Branch branch)
            {
                var stack = new Stack<Digraph.Edge>();

                var current = branch;
                while (current.Parent != null)
                {
                    stack.Push(current.BranchingEdge);
                    current = current.Parent;
                }
                return stack.ToList();
            }
Пример #6
0
        // hunts for a patch and stores it in arraylist of routes.
        private void findPath(String origin, String dest, GraphNode<String> graphnode, GraphNode<String>previous, ref Stack<String> stak, ref ArrayList routes)
        {
            // one true condition
            if (graphnode.Value.ToLower().Equals(dest.ToLower()))
            {
                String[] path = stak.ToArray<String>();
                Array.Reverse(path);
                routes.Add(path);
                return;
            }

            // only can do this stuff if the current node is not the root node
            if (previous != null)
            {
                // check if its a cyclic graph back to the origin
                if (graphnode.Value.ToLower().Equals(origin.ToLower()))
                {
                    return;
                }

                // check if the current node only has on vertex back to its parent
                if ((graphnode.Neighbors.Count == 1) && (graphnode.Neighbors[0].Value.ToLower().Equals(previous.Value.ToLower())))
                {
                    return;
                }

                // deal with cyclic nodes by using history to detect.
                // remove newly added node and root node in temp list
                // for detection

                List<String> temp = stak.ToList<String>();

                temp.RemoveAt(0);
                temp.RemoveAt((temp.Count - 1));

                if (temp.Contains(graphnode.Value))
                {
                    return;
                }
            }

            // checks to see if node has no connections to any other nodes
            if ((graphnode.Neighbors.Count == 0) && (!graphnode.Value.ToLower().Equals(dest.ToLower())))
            {
                return;
            }

            foreach (GraphNode<String> node in graphnode.Neighbors)
            {
                // push stack to maintain history
                stak.Push(node.Value);
                if ((previous == null) || (!node.Value.ToLower().Equals(previous.Value.ToLower())))
                {
                    findPath(origin, dest, node, graphnode, ref stak, ref routes);
                }
                stak.Pop();

            }
            return;
        }