Added generateDirectoryTree()

This function which generates a Directory Tree
from a given path string
for e.g
    if the String that is given is "home/what/dir" , "home/hello/foo"
	then it will return a FileNode ( Struct )
	which looks like this
	head{
	    [{
		[
		    {
			[ { [] dir } ]
			what
		    },
		    {
			[ { [] foo } ]
			hello
		    }
		]
		home
	    }]
	    "root"
	}
This commit is contained in:
aditya-K2 2021-10-11 11:42:00 +05:30
parent 94e86335b2
commit 99e0e8e615

View File

@ -2,6 +2,7 @@ package main
import ( import (
"strings" "strings"
"fmt"
) )
type FileNode struct{ type FileNode struct{
@ -19,29 +20,32 @@ func (f *FileNode) addChildNode(m FileNode){
f.children = append(f.children, m) f.children = append(f.children, m)
} }
func mapGenerator(fileList [] string) map[string][]string { func generateDirectoryTree(path [] string) *FileNode{
var a map[string][]string = make(map[string][]string)
for i := range(fileList){
a[strings.Split(fileList[i] , "/")[0]] = append(a[strings.Split(fileList[i] , "/")[0]], strings.Split(fileList[i] , "/")[1])
}
return a
}
func generateDirectoryTree(fileList [] string) *FileNode{
var head *FileNode = new(FileNode) var head *FileNode = new(FileNode)
for i:=0; i<len(fileList) ; i++ { var head1 *FileNode = head
separatedPaths := strings.Split(fileList [i] , "/") for i := range(path){
var currentNode *FileNode = new(FileNode) sepPaths := strings.Split(path[i], "/")
tempNode := currentNode for j := range(sepPaths){
for j := range(separatedPaths) { if(len(head.children) == 0){
if j != (len(separatedPaths)-1) { head.addChildren(sepPaths[j])
tempNode.addChildren(separatedPaths[j]) head = &(head.children[len(head.children) - 1])
tempNode = &tempNode.children[0]
} else { } else {
tempNode.addChildren(separatedPaths[j]) var headIsChanged = false
for k := range(head.children){
if head.children[k].path == sepPaths[j] {
head = &(head.children[k])
headIsChanged = true
break
} }
} }
head.addChildNode(*currentNode) if(!headIsChanged){
head.addChildren(sepPaths[j])
head = &(head.children[len(head.children) - 1])
} }
return head; }
}
head = head1
}
fmt.Println(head)
return head
} }