Пример #1
0
        public static NTree <PathInfo> GetDirectoryTree(string path, out ulong FileCount, out ulong DirectoryCount)
        {
            var cur_dir = path;
            var bfsQ    = new Queue <NNode <PathInfo> >();
            var root    = new NNode <PathInfo>(new PathInfo {
                Path = path
            });
            var cur_node = root;

            bfsQ.Enqueue(root);
            ulong dirCounter = 1, fileCounter = 0;

            while (bfsQ.Count > 0)
            {
                cur_node = bfsQ.Dequeue();
                var subdirs = FastFileOperations.GetDirectoriesAndFiles(cur_node.Value.Path);
                foreach (var sd in subdirs)
                {
                    var child = new NNode <PathInfo>(sd);
                    bfsQ.Enqueue(child);
                    cur_node.AddChild(child);
                    dirCounter++;
                }
            }
            var dirTree = new NTree <PathInfo>(root);

            FileCount      = fileCounter;
            DirectoryCount = dirCounter;
            return(dirTree);
        }
Пример #2
0
 public void RemoveNode(NNode <T> node)
 {
     if (node == Root)
     {
         this.Root = null;
     }
     else
     {
         node.Parent.RemoveChild(node);
     }
 }
Пример #3
0
        public void BFS(Func <NNode <T>, object> func, NNode <T> startNode)
        {
            Queue <NNode <T> > bfsQ = new Queue <NNode <T> >();

            bfsQ.Enqueue(Root);
            while (bfsQ.Count != 0)
            {
                var curr = bfsQ.Dequeue();
                func(curr);
                foreach (var n in curr.Children)
                {
                    bfsQ.Enqueue(n);
                }
            }
        }
Пример #4
0
        public void DFS(Func <NNode <T>, object> func, NNode <T> startNode, VisitingOrder visitingOrder)
        {
            HashSet <NNode <T> > visitedNodes = new HashSet <NNode <T> >();
            Stack <NNode <T> >   dfsStack     = new Stack <NNode <T> >();
            var curr = Root;

            while (curr != null)
            {
                //if (visitingOrder == VisitingOrder.Pre)
                //{
                //    if (!curr.Visited)
                //    {
                //        func(curr);
                //        curr.Visited = true;
                //    }
                //}

                NNode <T> next = (curr.Children.Count == 0) ? null : curr.Children.FirstOrDefault(p => !visitedNodes.Contains(p));

                if (next == null)
                {
                    if (visitingOrder == VisitingOrder.Post)
                    {
                        func(curr);
                        visitedNodes.Add(curr);
                    }
                    if (dfsStack.Count != 0)
                    {
                        next = dfsStack.Pop();
                    }
                }
                else
                {
                    dfsStack.Push(curr);
                }
                curr = next;
            }
        }
Пример #5
0
 public NTree(NNode <T> root)
 {
     Root = root;
 }
Пример #6
0
 public void RemoveChild(NNode <T> child)
 {
     child.Parent = null;
     _children.Remove(child);
     Nodes--;
 }
Пример #7
0
 public void AddChild(NNode <T> child)
 {
     child.Parent = this;
     _children.Add(child);
     Nodes++;
 }