• 算法-二叉树前中后层遍历


    1. package main
    2. import (
    3. "container/list"
    4. "fmt"
    5. )
    6. type Node struct {
    7. data any
    8. left *Node
    9. right *Node
    10. }
    11. type Tree struct {
    12. root *Node
    13. }
    14. // 创建一颗二叉搜索树left < root < right(条件)
    15. func (t *Tree) createTree(root *Node, data interface{}) *Node {
    16. if root == nil {
    17. node := &Node{
    18. data: data,
    19. left: nil,
    20. right: nil,
    21. }
    22. return node
    23. }
    24. // 比较时,需要具体的类型
    25. if data.(int) < root.data.(int) {
    26. root.left = t.createTree(root.left, data)
    27. } else {
    28. root.right = t.createTree(root.right, data)
    29. }
    30. return root
    31. }
    32. // 前序遍历
    33. func (t *Tree) QDisplay(root *Node) {
    34. if root == nil {
    35. return
    36. }
    37. fmt.Println(root.data)
    38. t.QDisplay(root.left)
    39. t.QDisplay(root.right)
    40. }
    41. // 非递归前序遍历
    42. func (t *Tree) NoQDisplay(root *Node) {
    43. if root == nil {
    44. return
    45. }
    46. curr := root
    47. st := list.New()
    48. st.PushBack(curr)
    49. for curr != nil || st.Len() != 0 {
    50. t := st.Back()
    51. st.Remove(t)
    52. m := t.Value.(*Node)
    53. fmt.Println(m.data)
    54. if m.right != nil {
    55. st.PushBack(m.right)
    56. }
    57. if m.left != nil {
    58. st.PushBack(m.left)
    59. }
    60. }
    61. }
    62. // 中序遍历
    63. func (t *Tree) ZDisplay(root *Node) {
    64. if root == nil {
    65. return
    66. }
    67. t.ZDisplay(root.left)
    68. fmt.Println(root.data)
    69. t.ZDisplay(root.right)
    70. }
    71. // 中序遍历非递归,左 - 根 - 右
    72. func (t *Tree) NoZDisplay(root *Node) {
    73. if root == nil {
    74. return
    75. }
    76. // 借助栈结构,先进后出
    77. curr := root
    78. stack := list.New()
    79. for curr != nil || stack.Len() != 0 {
    80. if curr != nil {
    81. // 所有的左子树放入栈中
    82. stack.PushBack(curr)
    83. curr = curr.left
    84. } else {
    85. tmp := stack.Back()
    86. stack.Remove(tmp)
    87. if tmp.Value != nil {
    88. fmt.Println(tmp.Value.(*Node).data)
    89. curr = tmp.Value.(*Node).right // 让当前出栈的节点的右子树进栈
    90. }
    91. }
    92. }
    93. }
    94. // 后序遍历
    95. func (t *Tree) HDisplay(root *Node) {
    96. if root == nil {
    97. return
    98. }
    99. t.HDisplay(root.left)
    100. t.HDisplay(root.right)
    101. fmt.Println(root.data)
    102. }
    103. // 后序遍历非递归
    104. // 后续遍历解决的问题
    105. // 当给定一个节点时,输出该节点的所有祖先
    106. // 输出根结点到叶子节点的所有路径
    107. // 求每条路径上的节点值之和
    108. func (t *Tree) NoHDisplay(root *Node) {
    109. if root == nil {
    110. return
    111. }
    112. var pre *Node = nil
    113. stack := list.New()
    114. for root != nil || stack.Len() != 0 {
    115. // 将左右左子树存放到栈中
    116. for root != nil {
    117. stack.PushBack(root)
    118. root = root.left
    119. }
    120. // 取出节点
    121. tmp := stack.Back()
    122. root = tmp.Value.(*Node)
    123. stack.Remove(tmp)
    124. // 当前节点如果右节点为空,或者等于上次访问的节点,输出该节点,比把已经输出的节点打上标记
    125. if root.right == nil || root.right == pre {
    126. fmt.Println(root.data)
    127. pre = root
    128. root = nil // 右子树访问了,重新下一轮的从栈那元素
    129. } else {
    130. // 如果不能输出,重新入栈,再对当前节点的右子数重复入栈过程
    131. stack.PushBack(root)
    132. root = root.right
    133. }
    134. }
    135. }
    136. // 按层遍历
    137. func (t *Tree) CDisplay(root *Node) {
    138. if root == nil {
    139. return
    140. }
    141. queue := list.New()
    142. queue.PushBack(root)
    143. for queue.Len() != 0 {
    144. tmp := queue.Front()
    145. queue.Remove(tmp)
    146. cur := tmp.Value.(*Node)
    147. fmt.Println(cur.data)
    148. if cur.left != nil {
    149. queue.PushBack(cur.left)
    150. }
    151. if cur.right != nil {
    152. queue.PushBack(cur.right)
    153. }
    154. }
    155. }
    156. func main() {
    157. t := &Tree{
    158. root: nil,
    159. }
    160. number := []int{1, 4, 3, 2}
    161. // 创建一棵树
    162. for _, val := range number {
    163. t.root = t.createTree(t.root, val)
    164. }
    165. //
    166. // IntMap := make(map[int]int, 0)
    167. // for i := 0; i < 20; i++ {
    168. // num := rand.Int() % 10
    169. // if _, ok := IntMap[num]; ok {
    170. // continue
    171. // }
    172. // t.root = t.createTree(t.root, num)
    173. // IntMap[num] = num
    174. // }
    175. t.QDisplay(t.root)
    176. fmt.Println("-----------")
    177. t.NoQDisplay(t.root)
    178. // t.ZDisplay(t.root)
    179. // fmt.Println("-----------")
    180. // t.NoZDisplay(t.root)
    181. //
    182. // t.HDisplay(t.root)
    183. // fmt.Println("-----------")
    184. // t.NoHDisplay(t.root)
    185. //
    186. // t.CDisplay(t.root)
    187. }

  • 相关阅读:
    计算机设计大赛 深度学习的智能中文对话问答机器人
    java反射大白话
    如何计算 R 中的基尼系数(附示例)
    将 N 叉树编码为二叉树
    JavaScript|JavaScript 高级语法——详细汇总
    最近在对接电商供应链,说说开放平台API接口
    纯跟踪算法仿真
    DML操作
    关于浅克隆和深克隆入门理解
    unity解码4k图片过慢,使用turbojpeg加速,使用opencl加速,使用libjpeg,使用v4l2
  • 原文地址:https://blog.csdn.net/qq_26105397/article/details/126283235