node updates
This commit is contained in:
parent
41bea2f4a5
commit
3c8dd525f4
1 changed files with 16 additions and 5 deletions
|
@ -3,9 +3,10 @@ package tree
|
||||||
// Node is a struct containing nodes of a file tree.
|
// Node is a struct containing nodes of a file tree.
|
||||||
type Node struct {
|
type Node struct {
|
||||||
// can be nil
|
// can be nil
|
||||||
Parent *Node
|
Parent *Node
|
||||||
Name string
|
Name string
|
||||||
FileType string
|
// Empty value mean directory
|
||||||
|
Ext string
|
||||||
// cannot be nil; may have len 0
|
// cannot be nil; may have len 0
|
||||||
Children []*Node
|
Children []*Node
|
||||||
}
|
}
|
||||||
|
@ -14,7 +15,8 @@ type Node struct {
|
||||||
// parent and children are optional and can be nil,
|
// parent and children are optional and can be nil,
|
||||||
// in which case Parent will be stored as nil,
|
// in which case Parent will be stored as nil,
|
||||||
// but Children will be initialized as []*Node of len 0.
|
// 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
|
var c []*Node
|
||||||
if children == nil {
|
if children == nil {
|
||||||
c = make([]*Node, 0)
|
c = make([]*Node, 0)
|
||||||
|
@ -23,7 +25,7 @@ func NewNode(name string, ft string, parent *Node, children []*Node) *Node {
|
||||||
}
|
}
|
||||||
n := &Node{
|
n := &Node{
|
||||||
Name: name,
|
Name: name,
|
||||||
FileType: ft,
|
Ext: ext,
|
||||||
Parent: parent,
|
Parent: parent,
|
||||||
Children: c,
|
Children: c,
|
||||||
}
|
}
|
||||||
|
@ -37,3 +39,12 @@ func (n *Node) IsRoot() bool {
|
||||||
func (n *Node) IsTail() bool {
|
func (n *Node) IsTail() bool {
|
||||||
return len(n.Children) == 0
|
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) {
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue