tree.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package tree
  2. import "fmt"
  3. // TreeNode 表示树的节点
  4. type TreeNode struct {
  5. NodeID string
  6. Children []*TreeNode
  7. }
  8. // NewTreeNode 创建一个新的树节点
  9. func NewTreeNode(nodeID string) *TreeNode {
  10. return &TreeNode{NodeID: nodeID, Children: []*TreeNode{}}
  11. }
  12. // Insert 在指定节点 ID 下方插入一个新的节点
  13. func (node *TreeNode) Insert(parentID string, newNodeID string) bool {
  14. if node.NodeID == parentID {
  15. newNode := NewTreeNode(newNodeID)
  16. node.Children = append(node.Children, newNode)
  17. return true
  18. }
  19. for _, child := range node.Children {
  20. if child.Insert(parentID, newNodeID) {
  21. return true
  22. }
  23. }
  24. return false
  25. }
  26. // FindByID 按节点 ID 查找节点
  27. func (node *TreeNode) FindByID(targetID string) *TreeNode {
  28. if node.NodeID == targetID {
  29. return node
  30. }
  31. for _, child := range node.Children {
  32. foundNode := child.FindByID(targetID)
  33. if foundNode != nil {
  34. return foundNode
  35. }
  36. }
  37. return nil
  38. }
  39. // CountAllChildren 递归计算指定节点 ID 下所有子节点的数量(不包含节点本身)
  40. func (node *TreeNode) CountAllChildren(targetID string) int {
  41. if node.NodeID == targetID {
  42. return node.countAllChildrenHelper()
  43. }
  44. for _, child := range node.Children {
  45. count := child.CountAllChildren(targetID)
  46. if count != 0 {
  47. return count
  48. }
  49. }
  50. return 0
  51. }
  52. // countAllChildrenHelper 辅助函数,递归计算当前节点的所有子节点的数量
  53. func (node *TreeNode) countAllChildrenHelper() int {
  54. count := 0
  55. for _, child := range node.Children {
  56. count += 1 + child.countAllChildrenHelper()
  57. }
  58. return count
  59. }
  60. // CountDirectChildren 查询指定节点 ID 下的直接子节点个数
  61. func (node *TreeNode) CountDirectChildren(targetID string) int {
  62. if node.NodeID == targetID {
  63. return len(node.Children)
  64. }
  65. for _, child := range node.Children {
  66. count := child.CountDirectChildren(targetID)
  67. if count != 0 {
  68. return count
  69. }
  70. }
  71. return 0
  72. }
  73. func main() {
  74. // 创建根节点
  75. root := NewTreeNode("1")
  76. // 插入节点
  77. root.Insert("1", "2")
  78. root.Insert("1", "3")
  79. root.Insert("2", "4")
  80. root.Insert("2", "5")
  81. root.Insert("3", "6")
  82. root.Insert("3", "7")
  83. // 按节点 ID 查询节点
  84. node := root.FindByID("2")
  85. if node != nil {
  86. fmt.Printf("Found node with NodeID %d\n", node.NodeID)
  87. } else {
  88. fmt.Println("Node not found")
  89. }
  90. // 计算指定节点 ID 下所有子节点的数量
  91. count := root.CountAllChildren("1")
  92. fmt.Printf("Number of all nodes under NodeID 1: %d\n", count)
  93. // 再次查询直接子节点个数
  94. directChildrenCount := root.CountDirectChildren("3")
  95. fmt.Printf("Number of direct children under NodeID 1 after insertion: %d\n", directChildrenCount)
  96. }