在Go语言中设置二叉树的索引。

huangapple go评论121阅读模式
英文:

Setting an index of a binary tree in go

问题

我正在尝试自学数据结构和算法,并且正在努力找出向二叉树添加索引的最佳方法。

我猜最好的方法是修改中序遍历操作,但我不确定如何实现并在每次插入和删除函数之后链接它。

  1. type BinarySearchNode struct {
  2. Left *BinarySearchNode
  3. Right *BinarySearchNode
  4. Parent *BinarySearchNode
  5. Data int
  6. Index int
  7. }
  8. func (BSN *BinarySearchNode) Insert(value int) {
  9. if BSN.Data > value {
  10. if BSN.Left == nil {
  11. BSN.Left = &BinarySearchNode{Data: value, Parent: BSN}
  12. return
  13. }
  14. BSN.Left.Insert(value)
  15. return
  16. }
  17. if BSN.Right == nil {
  18. BSN.Right = &BinarySearchNode{Data: value, Parent: BSN}
  19. return
  20. }
  21. BSN.Right.Insert(value)
  22. }
  23. func (BSN *BinarySearchNode) setIndex(node *BinarySearchNode, count int) int {
  24. if node == nil {
  25. return count
  26. }
  27. if node.Right == nil && node.Left == nil {
  28. node.Index = count
  29. }
  30. node.setIndex(node.Left, count+1)
  31. node.setIndex(node.Right, count+1)
  32. return count
  33. }

以上是你提供的代码部分的翻译。

英文:

Im trying to teach myself data structures & algos and I am trying to figure out the best way to add an index to a binary tree

Im guessing the best way to do this is to modify an in orderTraversal operation but im not exactly sure how I would implement this and chain that on after every insert and delete function.

  1. type BinarySearchNode struct {
  2. Left *BinarySearchNode
  3. Right *BinarySearchNode
  4. Parent *BinarySearchNode
  5. Data int
  6. Index int
  7. }
  8. func (BSN *BinarySearchNode) Insert(value int){
  9. if BSN.Data > value {
  10. if BSN.Left == nil{
  11. BSN.Left = &BinarySearchNode{Data:value, Parent:BSN}
  12. return
  13. }
  14. BSN.Left.Insert(value)
  15. return
  16. }
  17. if BSN.Right == nil{
  18. BSN.Right = &BinarySearchNode{Data:value, Parent: BSN}
  19. return
  20. }
  21. BSN.Right.Insert(value)
  22. }
  23. func (BSN *BinarySearchNode) setIndex(node *BinarySearchNode, count int)(int){
  24. if node == nil{
  25. return count
  26. }
  27. if node.Right == nil && node.Left == nil{
  28. node.Index = count
  29. }
  30. node.setIndex(node.Left, count+1)
  31. node.setIndex(node.Right, count+1)
  32. return count
  33. }

答案1

得分: 1

我有一段用于在golang中实现二叉树的代码,可能对你有帮助。

  1. package main
  2. import (
  3. "fmt"
  4. "math"
  5. )
  6. // 二叉树中的单个节点
  7. type Node struct {
  8. // 节点包含的值
  9. Value int
  10. // 节点的左子节点
  11. Left *Node
  12. // 节点的右子节点
  13. Right *Node
  14. }
  15. type BinaryTree struct {
  16. Root *Node
  17. }
  18. // 在二叉树中插入元素
  19. func (tree *BinaryTree) Insert(value int) {
  20. if tree.Root == nil {
  21. node := new(Node)
  22. node.Value = value
  23. tree.Root = node
  24. return
  25. }
  26. current := tree.Root
  27. for {
  28. if value < current.Value {
  29. if current.Left == nil {
  30. node := new(Node)
  31. node.Value = value
  32. current.Left = node
  33. return
  34. } else {
  35. current = current.Left
  36. }
  37. } else {
  38. if current.Right == nil {
  39. node := new(Node)
  40. node.Value = value
  41. current.Right = node
  42. return
  43. } else {
  44. current = current.Right
  45. }
  46. }
  47. }
  48. }
  49. // 递归前序遍历
  50. func (tree *BinaryTree) RecursivePreOrder(root *Node) {
  51. if root == nil {
  52. return
  53. }
  54. fmt.Println(root.Value)
  55. tree.RecursivePreOrder(root.Left)
  56. tree.RecursivePreOrder(root.Right)
  57. }
  58. // 迭代前序遍历
  59. func (tree *BinaryTree) IterativePreOrder(root *Node) {
  60. stack := []*Node{}
  61. for {
  62. for root != nil {
  63. fmt.Println(root.Value)
  64. stack = append([]*Node{root}, stack...)
  65. root = root.Left
  66. }
  67. if len(stack) == 0 {
  68. break
  69. }
  70. root = stack[0]
  71. stack = stack[1:]
  72. root = root.Right
  73. }
  74. }
  75. // 递归中序遍历
  76. func (tree *BinaryTree) RecursiveInOrder(root *Node) {
  77. if root == nil {
  78. return
  79. }
  80. tree.RecursiveInOrder(root.Left)
  81. fmt.Println(root.Value)
  82. tree.RecursiveInOrder(root.Right)
  83. }
  84. // 迭代中序遍历
  85. func (tree *BinaryTree) IterativeInOrder(root *Node) {
  86. var stack []*Node
  87. for {
  88. for root != nil {
  89. stack = append([]*Node{root}, stack...)
  90. root = root.Left
  91. }
  92. if len(stack) == 0 {
  93. break
  94. }
  95. root = stack[0]
  96. stack = stack[1:]
  97. fmt.Println(root.Value)
  98. root = root.Right
  99. }
  100. }
  101. // 递归后序遍历
  102. func (tree *BinaryTree) RecursivePostOrder(root *Node) {
  103. if root == nil {
  104. return
  105. }
  106. tree.RecursivePostOrder(root.Left)
  107. tree.RecursivePostOrder(root.Right)
  108. fmt.Println(root.Value)
  109. }
  110. // 迭代后序遍历
  111. func (tree *BinaryTree) IterativePostOrder(root *Node) {
  112. stack := []*Node{}
  113. var previous *Node = nil
  114. for {
  115. for root != nil {
  116. stack = append([]*Node{root}, stack...)
  117. root = root.Left
  118. }
  119. for root == nil && len(stack) != 0 {
  120. root = stack[0]
  121. if root.Right == nil || root.Right == previous {
  122. fmt.Println(root.Value)
  123. stack = stack[1:]
  124. previous = root
  125. root = nil
  126. } else {
  127. root = root.Right
  128. }
  129. }
  130. if len(stack) == 0 {
  131. break
  132. }
  133. }
  134. }
  135. // 层序遍历
  136. func (tree *BinaryTree) LevelOrder(root *Node) {
  137. // 用于执行广度优先遍历的队列
  138. queue := []*Node{}
  139. if root != nil {
  140. queue = append(queue, root)
  141. }
  142. for len(queue) > 0 {
  143. root = queue[0]
  144. queue = queue[1:]
  145. fmt.Println(root.Value)
  146. if root.Left != nil {
  147. queue = append(queue, root.Left)
  148. }
  149. if root.Right != nil {
  150. queue = append(queue, root.Right)
  151. }
  152. }
  153. }
  154. // 计算二叉树的大小(节点数量)
  155. func (tree *BinaryTree) Size(root *Node) int {
  156. if root == nil {
  157. return 0
  158. }
  159. sum := tree.Size(root.Left) + 1 + tree.Size(root.Right)
  160. return sum
  161. }
  162. // 获取二叉树中指定索引的节点
  163. func (tree *BinaryTree) ElementAt(root *Node, index int) *Node {
  164. if index > tree.Size(root)-1 {
  165. fmt.Println("索引不存在")
  166. return nil
  167. }
  168. leftSize := tree.Size(root.Left)
  169. if index == leftSize {
  170. return root
  171. } else if index < leftSize {
  172. return tree.ElementAt(root.Left, index)
  173. } else {
  174. return tree.ElementAt(root.Right, index-leftSize-1)
  175. }
  176. }
  177. // 计算二叉树的高度
  178. func (tree *BinaryTree) Height(root *Node) int {
  179. if root == nil {
  180. return -1
  181. }
  182. return int(math.Max(float64(tree.Height(root.Left)), float64(tree.Height(root.Right)))) + 1
  183. }
  184. // 程序的入口点
  185. func main() {
  186. tree := new(BinaryTree)
  187. tree.Insert(44)
  188. tree.Insert(55)
  189. tree.Insert(33)
  190. fmt.Println(tree.Height(tree.Root))
  191. }

希望对你有所帮助!

英文:

I have a piece of code for the implementation of binary tree in golang. It may help you.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;math&quot;
  5. )
  6. // A single Node in a binary tree
  7. type Node struct {
  8. // Value contained by the node
  9. Value int
  10. // Left subnode of the node
  11. Left *Node
  12. // Right subnode of the node
  13. Right *Node
  14. }
  15. type BinaryTree struct {
  16. Root *Node
  17. }
  18. // inserting the element in the binary tree
  19. func (tree *BinaryTree) Insert(value int) {
  20. if tree.Root == nil {
  21. node := new(Node)
  22. node.Value = value
  23. tree.Root = node
  24. return
  25. }
  26. current := tree.Root
  27. for {
  28. if value &lt; current.Value {
  29. if current.Left == nil {
  30. node := new(Node)
  31. node.Value = value
  32. current.Left = node
  33. return
  34. } else {
  35. current = current.Left
  36. }
  37. } else {
  38. if current.Right == nil {
  39. node := new(Node)
  40. node.Value = value
  41. current.Right = node
  42. return
  43. } else {
  44. current = current.Right
  45. }
  46. }
  47. }
  48. }
  49. // current left right
  50. func (tree *BinaryTree) RecursivePreOrder(root *Node) {
  51. if root == nil {
  52. return
  53. }
  54. fmt.Println(root.Value)
  55. tree.RecursivePreOrder(root.Left)
  56. tree.RecursivePreOrder(root.Right)
  57. }
  58. func (tree *BinaryTree) IterativePreOrder(root *Node) {
  59. stack := []*Node{}
  60. for {
  61. for root != nil {
  62. fmt.Println(root.Value)
  63. stack = append([]*Node{root}, stack...)
  64. root = root.Left
  65. }
  66. if len(stack) == 0 {
  67. break
  68. }
  69. root = stack[0]
  70. stack = stack[1:]
  71. root = root.Right
  72. }
  73. }
  74. func (tree *BinaryTree) RecursiveInOrder(root *Node) {
  75. if root == nil {
  76. return
  77. }
  78. tree.RecursiveInOrder(root.Left)
  79. fmt.Println(root.Value)
  80. tree.RecursiveInOrder(root.Right)
  81. }
  82. func (tree *BinaryTree) IterativeInOrder(root *Node) {
  83. var stack []*Node
  84. for {
  85. for root != nil {
  86. stack = append([]*Node{root}, stack...)
  87. root = root.Left
  88. }
  89. if len(stack) == 0 {
  90. break
  91. }
  92. root = stack[0]
  93. stack = stack[1:]
  94. fmt.Println(root.Value)
  95. root = root.Right
  96. }
  97. }
  98. func (tree *BinaryTree) RecursivePostOrder(root *Node) {
  99. if root == nil {
  100. return
  101. }
  102. tree.RecursivePostOrder(root.Left)
  103. tree.RecursivePostOrder(root.Right)
  104. fmt.Println(root.Value)
  105. }
  106. func (tree *BinaryTree) IterativePostOrder(root *Node) {
  107. stack := []*Node{}
  108. var previous *Node = nil
  109. for {
  110. for root != nil {
  111. stack = append([]*Node{root}, stack...)
  112. root = root.Left
  113. }
  114. for root == nil &amp;&amp; len(stack) != 0 {
  115. root = stack[0]
  116. if root.Right == nil || root.Right == previous {
  117. fmt.Println(root.Value)
  118. stack = stack[1:]
  119. previous = root
  120. root = nil
  121. } else {
  122. root = root.Right
  123. }
  124. }
  125. if len(stack) == 0 {
  126. break
  127. }
  128. }
  129. }
  130. func (tree *BinaryTree) LevelOrder(root *Node) {
  131. // a queue for performing level order traversal of breadth first traversal
  132. queue := []*Node{}
  133. if root != nil {
  134. queue = append(queue, root)
  135. }
  136. for len(queue) &gt; 0 {
  137. root = queue[0]
  138. queue = queue[1:]
  139. fmt.Println(root.Value)
  140. if root.Left != nil {
  141. queue = append(queue, root.Left)
  142. }
  143. if root.Right != nil {
  144. queue = append(queue, root.Right)
  145. }
  146. }
  147. }
  148. func (tree *BinaryTree) Size(root *Node) int {
  149. if root == nil {
  150. return 0
  151. }
  152. sum := tree.Size(root.Left) + 1 + tree.Size(root.Right)
  153. return sum
  154. }
  155. func (tree *BinaryTree) ElementAt(root *Node, index int) *Node {
  156. if index &gt; tree.Size(root)-1 {
  157. fmt.Println(&quot;Index doesnot exist&quot;)
  158. return nil
  159. }
  160. leftSize := tree.Size(root.Left)
  161. if index == leftSize {
  162. return root
  163. } else if index &lt; leftSize {
  164. return tree.ElementAt(root.Left, index)
  165. } else {
  166. return tree.ElementAt(root.Right, index-leftSize-1)
  167. }
  168. }
  169. func (tree *BinaryTree) Height(root *Node) int {
  170. if root == nil {
  171. return -1
  172. }
  173. return int(math.Max(float64(tree.Height(root.Left)), float64(tree.Height(root.Right)))) + 1
  174. }
  175. // starting point of the program
  176. func main() {
  177. tree := new(BinaryTree)
  178. tree.Insert(44)
  179. tree.Insert(55)
  180. tree.Insert(33)
  181. fmt.Println(tree.Height(tree.Root))
  182. }

huangapple
  • 本文由 发表于 2022年3月13日 03:28:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/71452529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定