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

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

Setting an index of a binary tree in go

问题

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

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

type BinarySearchNode struct {
	Left   *BinarySearchNode
	Right  *BinarySearchNode
	Parent *BinarySearchNode
	Data   int
	Index  int
}

func (BSN *BinarySearchNode) Insert(value int) {

	if BSN.Data > value {
		if BSN.Left == nil {
			BSN.Left = &BinarySearchNode{Data: value, Parent: BSN}
			return
		}
		BSN.Left.Insert(value)
		return
	}

	if BSN.Right == nil {
		BSN.Right = &BinarySearchNode{Data: value, Parent: BSN}
		return
	}
	BSN.Right.Insert(value)
}

func (BSN *BinarySearchNode) setIndex(node *BinarySearchNode, count int) int {
	if node == nil {
		return count
	}

	if node.Right == nil && node.Left == nil {
		node.Index = count
	}
	node.setIndex(node.Left, count+1)
	node.setIndex(node.Right, count+1)

	return count
}

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

英文:

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.


type BinarySearchNode struct {
	Left *BinarySearchNode
	Right *BinarySearchNode
	Parent *BinarySearchNode
	Data int
	Index int
}

func (BSN *BinarySearchNode) Insert(value int){
	
	if BSN.Data > value {
		if BSN.Left == nil{
			BSN.Left = &BinarySearchNode{Data:value, Parent:BSN}
			return
		}
		BSN.Left.Insert(value)
		return
	}

	if BSN.Right == nil{
		BSN.Right = &BinarySearchNode{Data:value, Parent: BSN}
		return
	}
	BSN.Right.Insert(value)
}

func (BSN *BinarySearchNode) setIndex(node *BinarySearchNode, count int)(int){
	if node == nil{
		return count
	}

	if node.Right == nil && node.Left == nil{
		node.Index = count
	}
	node.setIndex(node.Left, count+1)
	node.setIndex(node.Right, count+1)
	
	return count
}

答案1

得分: 1

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

package main

import (
	"fmt"
	"math"
)

// 二叉树中的单个节点
type Node struct {

	// 节点包含的值
	Value int

	// 节点的左子节点
	Left *Node

	// 节点的右子节点
	Right *Node
}

type BinaryTree struct {
	Root *Node
}

// 在二叉树中插入元素
func (tree *BinaryTree) Insert(value int) {
	if tree.Root == nil {
		node := new(Node)
		node.Value = value
		tree.Root = node
		return
	}
	current := tree.Root
	for {
		if value < current.Value {
			if current.Left == nil {

				node := new(Node)
				node.Value = value
				current.Left = node
				return
			} else {
				current = current.Left
			}

		} else {
			if current.Right == nil {

				node := new(Node)
				node.Value = value
				current.Right = node
				return
			} else {
				current = current.Right
			}
		}
	}
}

// 递归前序遍历
func (tree *BinaryTree) RecursivePreOrder(root *Node) {
	if root == nil {
		return
	}
	fmt.Println(root.Value)
	tree.RecursivePreOrder(root.Left)
	tree.RecursivePreOrder(root.Right)

}

// 迭代前序遍历
func (tree *BinaryTree) IterativePreOrder(root *Node) {
	stack := []*Node{}
	for {
		for root != nil {
			fmt.Println(root.Value)
			stack = append([]*Node{root}, stack...)
			root = root.Left
		}
		if len(stack) == 0 {
			break
		}
		root = stack[0]
		stack = stack[1:]
		root = root.Right
	}
}

// 递归中序遍历
func (tree *BinaryTree) RecursiveInOrder(root *Node) {
	if root == nil {
		return
	}
	tree.RecursiveInOrder(root.Left)
	fmt.Println(root.Value)
	tree.RecursiveInOrder(root.Right)

}

// 迭代中序遍历
func (tree *BinaryTree) IterativeInOrder(root *Node) {
	var stack []*Node
	for {
		for root != nil {
			stack = append([]*Node{root}, stack...)
			root = root.Left
		}
		if len(stack) == 0 {
			break
		}
		root = stack[0]
		stack = stack[1:]
		fmt.Println(root.Value)
		root = root.Right
	}
}

// 递归后序遍历
func (tree *BinaryTree) RecursivePostOrder(root *Node) {
	if root == nil {
		return
	}
	tree.RecursivePostOrder(root.Left)
	tree.RecursivePostOrder(root.Right)
	fmt.Println(root.Value)
}

// 迭代后序遍历
func (tree *BinaryTree) IterativePostOrder(root *Node) {
	stack := []*Node{}

	var previous *Node = nil
	for {
		for root != nil {
			stack = append([]*Node{root}, stack...)
			root = root.Left
		}
		for root == nil && len(stack) != 0 {
			root = stack[0]
			if root.Right == nil || root.Right == previous {
				fmt.Println(root.Value)
				stack = stack[1:]
				previous = root
				root = nil
			} else {
				root = root.Right
			}
		}
		if len(stack) == 0 {
			break
		}
	}
}

// 层序遍历
func (tree *BinaryTree) LevelOrder(root *Node) {
	// 用于执行广度优先遍历的队列
	queue := []*Node{}

	if root != nil {
		queue = append(queue, root)
	}
	for len(queue) > 0 {
		root = queue[0]
		queue = queue[1:]
		fmt.Println(root.Value)
		if root.Left != nil {
			queue = append(queue, root.Left)
		}
		if root.Right != nil {
			queue = append(queue, root.Right)
		}
	}
}

// 计算二叉树的大小(节点数量)
func (tree *BinaryTree) Size(root *Node) int {
	if root == nil {
		return 0
	}
	sum := tree.Size(root.Left) + 1 + tree.Size(root.Right)
	return sum
}

// 获取二叉树中指定索引的节点
func (tree *BinaryTree) ElementAt(root *Node, index int) *Node {
	if index > tree.Size(root)-1 {
		fmt.Println("索引不存在")
		return nil
	}
	leftSize := tree.Size(root.Left)
	if index == leftSize {
		return root
	} else if index < leftSize {
		return tree.ElementAt(root.Left, index)
	} else {
		return tree.ElementAt(root.Right, index-leftSize-1)
	}
}

// 计算二叉树的高度
func (tree *BinaryTree) Height(root *Node) int {
	if root == nil {
		return -1
	}
	return int(math.Max(float64(tree.Height(root.Left)), float64(tree.Height(root.Right)))) + 1
}

// 程序的入口点
func main() {
	tree := new(BinaryTree)
	tree.Insert(44)
	tree.Insert(55)
	tree.Insert(33)
	fmt.Println(tree.Height(tree.Root))

}

希望对你有所帮助!

英文:

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

package main
import (
&quot;fmt&quot;
&quot;math&quot;
)
// A single Node in a binary tree
type Node struct {
// Value contained by the node
Value int
// Left subnode of the node
Left *Node
// Right subnode of the node
Right *Node
}
type BinaryTree struct {
Root *Node
}
// inserting the element in the binary tree
func (tree *BinaryTree) Insert(value int) {
if tree.Root == nil {
node := new(Node)
node.Value = value
tree.Root = node
return
}
current := tree.Root
for {
if value &lt; current.Value {
if current.Left == nil {
node := new(Node)
node.Value = value
current.Left = node
return
} else {
current = current.Left
}
} else {
if current.Right == nil {
node := new(Node)
node.Value = value
current.Right = node
return
} else {
current = current.Right
}
}
}
}
// current left right
func (tree *BinaryTree) RecursivePreOrder(root *Node) {
if root == nil {
return
}
fmt.Println(root.Value)
tree.RecursivePreOrder(root.Left)
tree.RecursivePreOrder(root.Right)
}
func (tree *BinaryTree) IterativePreOrder(root *Node) {
stack := []*Node{}
for {
for root != nil {
fmt.Println(root.Value)
stack = append([]*Node{root}, stack...)
root = root.Left
}
if len(stack) == 0 {
break
}
root = stack[0]
stack = stack[1:]
root = root.Right
}
}
func (tree *BinaryTree) RecursiveInOrder(root *Node) {
if root == nil {
return
}
tree.RecursiveInOrder(root.Left)
fmt.Println(root.Value)
tree.RecursiveInOrder(root.Right)
}
func (tree *BinaryTree) IterativeInOrder(root *Node) {
var stack []*Node
for {
for root != nil {
stack = append([]*Node{root}, stack...)
root = root.Left
}
if len(stack) == 0 {
break
}
root = stack[0]
stack = stack[1:]
fmt.Println(root.Value)
root = root.Right
}
}
func (tree *BinaryTree) RecursivePostOrder(root *Node) {
if root == nil {
return
}
tree.RecursivePostOrder(root.Left)
tree.RecursivePostOrder(root.Right)
fmt.Println(root.Value)
}
func (tree *BinaryTree) IterativePostOrder(root *Node) {
stack := []*Node{}
var previous *Node = nil
for {
for root != nil {
stack = append([]*Node{root}, stack...)
root = root.Left
}
for root == nil &amp;&amp; len(stack) != 0 {
root = stack[0]
if root.Right == nil || root.Right == previous {
fmt.Println(root.Value)
stack = stack[1:]
previous = root
root = nil
} else {
root = root.Right
}
}
if len(stack) == 0 {
break
}
}
}
func (tree *BinaryTree) LevelOrder(root *Node) {
// a queue for performing level order traversal of breadth first traversal
queue := []*Node{}
if root != nil {
queue = append(queue, root)
}
for len(queue) &gt; 0 {
root = queue[0]
queue = queue[1:]
fmt.Println(root.Value)
if root.Left != nil {
queue = append(queue, root.Left)
}
if root.Right != nil {
queue = append(queue, root.Right)
}
}
}
func (tree *BinaryTree) Size(root *Node) int {
if root == nil {
return 0
}
sum := tree.Size(root.Left) + 1 + tree.Size(root.Right)
return sum
}
func (tree *BinaryTree) ElementAt(root *Node, index int) *Node {
if index &gt; tree.Size(root)-1 {
fmt.Println(&quot;Index doesnot exist&quot;)
return nil
}
leftSize := tree.Size(root.Left)
if index == leftSize {
return root
} else if index &lt; leftSize {
return tree.ElementAt(root.Left, index)
} else {
return tree.ElementAt(root.Right, index-leftSize-1)
}
}
func (tree *BinaryTree) Height(root *Node) int {
if root == nil {
return -1
}
return int(math.Max(float64(tree.Height(root.Left)), float64(tree.Height(root.Right)))) + 1
}
// starting point of the program
func main() {
tree := new(BinaryTree)
tree.Insert(44)
tree.Insert(55)
tree.Insert(33)
fmt.Println(tree.Height(tree.Root))
}

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:

确定