예제 #1
0
        /// <summary>
        /// Adds a node to the tree. If the node's topmost dir does not already exist, it is added after the tree Root.
        /// If the node's topmost dir is found inside the tree, we repeat the process for every subsequent node/dir
        /// until we can't find it or we reach the filename.
        /// </summary>
        /// <param name="node"></param>
        public void AddNode(INode node)
        {
            var          nodeChildren    = ((FileTreeNode)node).GetAllChildren();
            var          nodeToSearchFor = nodeChildren.First;
            FileTreeNode found           = SearchAllNodes(nodeToSearchFor.Value.Value, (FileTreeNode)Root);

            if (found == null)
            {
                Root.AddChild(node);
            }
            else
            {
                var locatedNode = found;
                while (found != null && nodeToSearchFor.Next != null)
                {
                    found = SearchNextLevel(nodeToSearchFor.Next.Value.Value, locatedNode);
                    if (found != null)
                    {
                        locatedNode = found;
                    }
                    nodeToSearchFor = nodeToSearchFor.Next;
                }
                if (locatedNode.Children.Count != 0)
                {
                    locatedNode.AddChild(nodeToSearchFor.Value);
                }
            }
        }
예제 #2
0
 private FileTreeNode SearchAllNodes(string valueToSearchFor, FileTreeNode nodeToSearch)
 {
     if (nodeToSearch.Value == valueToSearchFor)
     {
         return(nodeToSearch);
     }
     foreach (var child in nodeToSearch.Children)
     {
         var nodeFound = SearchAllNodes(valueToSearchFor, (FileTreeNode)child);
         if (nodeFound != null)
         {
             return(nodeFound);
         }
     }
     return(null);
 }
 public FileTreeNode(string value, FileTreeNode parent)
 {
     Value    = value;
     Parent   = parent;
     Children = new LinkedList <INode>();
 }
예제 #4
0
 private FileTreeNode SearchNextLevel(string valueToSearchFor, FileTreeNode nodeToSearch)
 {
     return((FileTreeNode)nodeToSearch.Children.FirstOrDefault(n => n.Value == valueToSearchFor));
 }
예제 #5
0
 public FileTree(FileTreeNode root)
 {
     Root = root;
 }