gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/treex/treex.go (about)

     1  package treex
     2  
     3  type ITreeNode interface {
     4  	AddChild(val *TreeNode)
     5  }
     6  
     7  type TreeNode struct {
     8  	// 编号
     9  	Id string
    10  
    11  	/**
    12  	 * 父节点
    13  	 */
    14  	Parent interface{}
    15  
    16  	/**
    17  	 * 标题
    18  	 */
    19  	Title string
    20  	/**
    21  	 * 图标
    22  	 */
    23  	Icon string
    24  
    25  	/**
    26  	 * 自定义数据
    27  	 */
    28  	Data interface{}
    29  
    30  	/**
    31  	 * 子节点
    32  	 */
    33  	Children []*TreeNode
    34  }
    35  
    36  func (n *TreeNode) AddChild(val *TreeNode) {
    37  	n.Children = append(n.Children, val)
    38  	val.Parent = n
    39  }
    40  
    41  func (n *TreeNode) GetChildById(id string) *TreeNode {
    42  	if len(n.Children) == 0 {
    43  		return nil
    44  	}
    45  	for i := 0; i < len(n.Children); i++ {
    46  		child := n.Children[i]
    47  		if child.Id == id {
    48  			return child
    49  		}
    50  	}
    51  	return nil
    52  }
    53  
    54  func (n *TreeNode) CreateChildId(child string) string {
    55  	if n.Id == "/" {
    56  		return "/" + child
    57  	}
    58  	return n.Id + "/" + child
    59  }
    60  
    61  /**
    62   * 如果返回true则继续
    63   * @param f
    64   */
    65  func (n *TreeNode) Walk(f func(node *TreeNode) bool) {
    66  	n._walk(f)
    67  }
    68  
    69  func (n *TreeNode) _walk(f func(node *TreeNode) bool) bool {
    70  	if !f(n) {
    71  		return false
    72  	}
    73  	if len(n.Children) > 0 {
    74  		for i := 0; i < len(n.Children); i++ {
    75  			child := n.Children[i]
    76  			if !child._walk(f) {
    77  				return false
    78  			}
    79  		}
    80  	}
    81  
    82  	return true
    83  }
    84  
    85  /**
    86   */
    87  func (n *TreeNode) FindById(id string) *TreeNode {
    88  	return n._findOne(func(node *TreeNode) bool {
    89  		return node.Id == id
    90  	})
    91  }
    92  
    93  /**
    94   * 如果返回true则继续
    95   * @param f
    96   */
    97  func (n *TreeNode) FindOne(f func(node *TreeNode) bool) *TreeNode {
    98  	return n._findOne(f)
    99  }
   100  
   101  func (n *TreeNode) _findOne(f func(node *TreeNode) bool) *TreeNode {
   102  	if f(n) {
   103  		return n
   104  	}
   105  
   106  	for i := 0; i < len(n.Children); i++ {
   107  		child := n.Children[i]
   108  		r := child._findOne(f)
   109  		if r != nil {
   110  			return r
   111  		}
   112  	}
   113  
   114  	return nil
   115  }
   116  
   117  /**
   118   * 如果返回true则继续
   119   * @param f
   120   */
   121  func (n *TreeNode) Find(f func(node *TreeNode) bool) []*TreeNode {
   122  	return n._find(f)
   123  }
   124  
   125  func (n *TreeNode) _find(f func(node *TreeNode) bool) (r []*TreeNode) {
   126  	if f(n) {
   127  		r = append(r, n)
   128  	}
   129  	for i := 0; i < len(n.Children); i++ {
   130  		child := n.Children[i]
   131  		result := child._find(f)
   132  		if len(result) > 0 {
   133  			r = append(r, result...)
   134  		}
   135  	}
   136  
   137  	return r
   138  }
   139  
   140  func (n *TreeNode) ToRoot() *TreeRoot {
   141  	root := NewTreeRoot()
   142  	root.Children = n.Children
   143  	return root
   144  }
   145  
   146  func NewTreeRoot() (obj *TreeRoot) {
   147  	obj = &TreeRoot{}
   148  	obj.Id = "/"
   149  	return
   150  }
   151  
   152  type TreeRoot struct {
   153  	TreeNode
   154  }