Golang下的数据结构与算法经典实现:二叉树、哈希、排序等!
在计算机科学领域,数据结构和算法是一对不可分割的概念。数据结构为算法提供了可操作的数据集合,而算法则为数据结构提供了具体的实现方式。对于程序员来说,掌握数据结构与算法是实现高效、优化程序的关键。在Golang语言中,二叉树、哈希、排序等数据结构和算法的经典实现是必须掌握的。
一、二叉树
二叉树是一种数据结构,它由节点和链接组成,节点包含一个值和两个子节点,链接将节点组合成树形结构。二叉树有很多种形式,但常见的是二叉搜索树和AVL树。
1、二叉搜索树
二叉搜索树是一种有序的二叉树,每个节点的左子树都小于它的值,右子树都大于它的值,这个性质使得二叉搜索树可以快速地搜索、插入和删除节点。
Golang下二叉搜索树的结构如下:
```
type Node struct {
Value int
Left *Node
Right *Node
}
```
实现插入节点的函数:
```
func InsertNode(root *Node, value int) *Node {
if root == nil {
return &Node{Value: value}
}
if value < root.Value {
root.Left = InsertNode(root.Left, value)
} else if value > root.Value {
root.Right = InsertNode(root.Right, value)
}
return root
}
```
实现查找节点的函数:
```
func SearchNode(root *Node, value int) *Node {
if root == nil {
return nil
}
if value == root.Value {
return root
} else if value < root.Value {
return SearchNode(root.Left, value)
} else {
return SearchNode(root.Right, value)
}
}
```
实现删除节点的函数:
```
func DeleteNode(root *Node, value int) *Node {
if root == nil {
return nil
}
if value == root.Value {
if root.Left == nil {
return root.Right
}
if root.Right == nil {
return root.Left
}
min := root.Right
for min.Left != nil {
min = min.Left
}
root.Value = min.Value
root.Right = DeleteNode(root.Right, min.Value)
} else if value < root.Value {
root.Left = DeleteNode(root.Left, value)
} else {
root.Right = DeleteNode(root.Right, value)
}
return root
}
```
2、AVL树
AVL树是一种自平衡二叉搜索树,它通过旋转操作保持树的平衡。AVL树的每个节点有一个平衡因子,这是它的左子树高度减去右子树高度的结果,范围在-1到1之间。
Golang下AVL树的结构如下:
```
type AVLNode struct {
Value int
Height int
Balance int
Left *AVLNode
Right *AVLNode
}
```
实现插入节点的函数:
```
func InsertAVLNode(root *AVLNode, value int) *AVLNode {
if root == nil {
return &AVLNode{Value: value, Height: 1}
}
if value < root.Value {
root.Left = InsertAVLNode(root.Left, value)
} else if value > root.Value {
root.Right = InsertAVLNode(root.Right, value)
} else {
return root
}
root.Height = max(getHeight(root.Left), getHeight(root.Right)) + 1
root.Balance = getBalance(root)
if root.Balance > 1 && value < root.Left.Value {
return rightRotate(root)
}
if root.Balance < -1 && value > root.Right.Value {
return leftRotate(root)
}
if root.Balance > 1 && value > root.Left.Value {
root.Left = leftRotate(root.Left)
return rightRotate(root)
}
if root.Balance < -1 && value < root.Right.Value {
root.Right = rightRotate(root.Right)
return leftRotate(root)
}
return root
}
```
实现查找节点的函数:
```
func SearchAVLNode(root *AVLNode, value int) *AVLNode {
if root == nil {
return nil
}
if root.Value == value {
return root
} else if value < root.Value {
return SearchAVLNode(root.Left, value)
} else {
return SearchAVLNode(root.Right, value)
}
}
```
实现删除节点的函数:
```
func DeleteAVLNode(root *AVLNode, value int) *AVLNode {
if root == nil {
return nil
}
if value < root.Value {
root.Left = DeleteAVLNode(root.Left, value)
} else if value > root.Value {
root.Right = DeleteAVLNode(root.Right, value)
} else {
if root.Left == nil {
return root.Right
} else if root.Right == nil {
return root.Left
}
min := root.Right
for min.Left != nil {
min = min.Left
}
root.Value = min.Value
root.Right = DeleteAVLNode(root.Right, min.Value)
}
root.Height = max(getHeight(root.Left), getHeight(root.Right)) + 1
root.Balance = getBalance(root)
if root.Balance > 1 && getBalance(root.Left) >= 0 {
return rightRotate(root)
}
if root.Balance > 1 && getBalance(root.Left) < 0 {
root.Left = leftRotate(root.Left)
return rightRotate(root)
}
if root.Balance < -1 && getBalance(root.Right) <= 0 {
return leftRotate(root)
}
if root.Balance < -1 && getBalance(root.Right) > 0 {
root.Right = rightRotate(root.Right)
return leftRotate(root)
}
return root
}
```
二、哈希
哈希表是一种以键为索引的数据结构,它将键映射到数组中的位置。哈希表的搜索、插入和删除时间复杂度都是常数级别的,这使它在处理大规模数据时具有很强的优势。
Golang下哈希表的结构如下:
```
type HashTable struct {
table map[int][]Pair
}
type Pair struct {
key string
value interface{}
}
```
实现哈希表的put函数:
```
func (ht *HashTable) Put(key string, value interface{}) {
index := hash(key)
if ht.table == nil {
ht.table = make(map[int][]Pair)
}
if ht.table[index] == nil {
ht.table[index] = make([]Pair, 0)
}
pairs := ht.table[index]
for i, pair := range pairs {
if pair.key == key {
pairs[i].value = value
ht.table[index] = pairs
return
}
}
ht.table[index] = append(pairs, Pair{key, value})
}
```
实现哈希表的get函数:
```
func (ht *HashTable) Get(key string) (interface{}, bool) {
index := hash(key)
pairs := ht.table[index]
for _, pair := range pairs {
if pair.key == key {
return pair.value, true
}
}
return nil, false
}
```
实现哈希表的delete函数:
```
func (ht *HashTable) Delete(key string) {
index := hash(key)
pairs := ht.table[index]
for i, pair := range pairs {
if pair.key == key {
ht.table[index] = append(pairs[:i], pairs[i+1:]...)
}
}
}
```
三、排序
排序算法是对一组数据进行重新排列的算法,通常用于将数据按照某种规律排列。常见的排序算法有冒泡排序、选择排序、插入排序、归并排序、快速排序等。
以快速排序为例,Golang下的实现如下:
```
func QuickSort(arr []int) []int {
if len(arr) == 0 {
return arr
}
pivot := arr[0]
left, right := 0, len(arr)-1
for i := 1; i < len(arr); i++ {
if arr[i] < pivot {
arr[i], arr[left] = arr[left], arr[i]
left++
} else {
arr[i], arr[right] = arr[right], arr[i]
right--
}
}
QuickSort(arr[:left])
QuickSort(arr[left+1:])
return arr
}
```
以上就是Golang下的经典数据结构与算法实现,通过学习和理解这些实现,可以提升我们的算法设计和编程能力。