From 3c8dd525f4429a512595495caa80aad180ca05bd Mon Sep 17 00:00:00 2001 From: Daniel Fichtinger Date: Tue, 5 Nov 2024 00:22:37 -0500 Subject: [PATCH] node updates --- internal/tree/node.go | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/internal/tree/node.go b/internal/tree/node.go index 367f845..9025b3d 100644 --- a/internal/tree/node.go +++ b/internal/tree/node.go @@ -3,9 +3,10 @@ package tree // Node is a struct containing nodes of a file tree. type Node struct { // can be nil - Parent *Node - Name string - FileType string + Parent *Node + Name string + // Empty value mean directory + Ext string // cannot be nil; may have len 0 Children []*Node } @@ -14,7 +15,8 @@ type Node struct { // parent and children are optional and can be nil, // in which case Parent will be stored as nil, // but Children will be initialized as []*Node of len 0. -func NewNode(name string, ft string, parent *Node, children []*Node) *Node { +// If ext == "", the Node is a directory. +func NewNode(name string, ext string, parent *Node, children []*Node) *Node { var c []*Node if children == nil { c = make([]*Node, 0) @@ -23,7 +25,7 @@ func NewNode(name string, ft string, parent *Node, children []*Node) *Node { } n := &Node{ Name: name, - FileType: ft, + Ext: ext, Parent: parent, Children: c, } @@ -37,3 +39,12 @@ func (n *Node) IsRoot() bool { func (n *Node) IsTail() bool { return len(n.Children) == 0 } + +func (n *Node) IsDir() bool { + return n.Ext == "" +} + +// TODO: Implement recursive depth-first traversal to process a tree + +func Traverse(root *Node) { +}