好好爱自己!

【转】二叉搜索树go实现

原文:http://www.xtgxiso.com/

 

二叉查找树,又叫二叉排序树,二叉搜索树,是一种有特定规则的二叉树,定义如下

  1. 它是一棵二叉树,或者是空树
  2. 左子树所有节点的值都小于它的根节点,右子树所有节点的值都大于它的根节点
  3. 左右子树也是一棵二叉查找树

 

 

二叉查找树可能退化为链表,也可能是一棵非常平衡的二叉树,查找,添加,删除元素的时间复杂度取决于树的高度h。

----------------------------

binary_tree.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
 
import (
    "fmt"
)
 
//二叉搜索树节点
type Searc2TreeNode struct {
    Value int
    Left  *Searc2TreeNode
    Right *Searc2TreeNode
}
 
//添加节点
func (n *Searc2TreeNode) Add(val int) {
    if val > n.Value {
        if n.Right == nil {
            n.Right = &Searc2TreeNode{Value: val}
        } else {
            n.Right.Add(val)
        }
    } else if val < n.Value {
        if n.Left == nil {
            n.Left = &Searc2TreeNode{Value: val}
        } else {
            n.Left.Add(val)
        }
    } else {
        fmt.Println(val, "已经存在")
    }
}
 
//遍历节点
func (n *Searc2TreeNode) View(vType string) {
    if vType != "first" && vType != "last" && vType != "mid" {
        fmt.Println("参数不合法")
    } else if vType == "first" { //先序遍历
        fmt.Print(n.Value, " ")
        if n.Left != nil {
            n.Left.View(vType)
        }
        if n.Right != nil {
            n.Right.View(vType)
        }
    } else if vType == "mid" { //中序遍历
        if n.Left != nil {
            n.Left.View(vType)
        }
        fmt.Print(n.Value, " ")
        if n.Right != nil {
            n.Right.View(vType)
        }
    } else { //后序遍历
        if n.Left != nil {
            n.Left.View(vType)
        }
 
        if n.Right != nil {
            n.Right.View(vType)
        }
        fmt.Print(n.Value, " ")
    }
}
 
//查找节点
func (n *Searc2TreeNode) Find(val int) *Searc2TreeNode {
    if n.Value == val {
        return n
    }
    if n.Left != nil && val < n.Value {
        return n.Left.Find(val)
    }
    if n.Right != nil && val > n.Value {
        return n.Right.Find(val)
    }
    return nil
}
 
//查找父节点
func (n *Searc2TreeNode) FindParent(val int) *Searc2TreeNode {
    if n.Left != nil && val < n.Value {
        if n.Left.Value == val {
            return n
        } else {
            return n.Left.FindParent(val)
        }
    }
    if n.Right != nil && val > n.Value {
        if n.Right.Value == val {
            return n
        } else {
            return n.Right.FindParent(val)
        }
    }
    return nil
}
 
//二叉搜索树
type Searc2Tree struct {
    Root *Searc2TreeNode
}
 
//添加节点
func (t *Searc2Tree) Add(val int) {
    if t.Root == nil {
        t.Root = &Searc2TreeNode{Value: val}
    } else {
        t.Root.Add(val)
    }
}
 
//遍历节点
func (t *Searc2Tree) View(vType string) {
    if t.Root != nil {
        t.Root.View(vType)
    } else {
        fmt.Println("root is empty")
    }
    fmt.Println("")
}
 
//查找节点
func (t *Searc2Tree) Find(val int) *Searc2TreeNode {
    if t.Root != nil {
        return t.Root.Find(val)
    } else {
        return nil
    }
}
 
//查找父节点
func (t *Searc2Tree) FindParent(val int) *Searc2TreeNode {
    if t.Root != nil {
        return t.Root.FindParent(val)
    } else {
        return nil
    }
}
 
//删除节点
func (t *Searc2Tree) Delete(val int) bool {
    if t.Root != nil {
        node := t.Find(val)
        if node == nil {
            return false
        } else {
            nodeParent := t.FindParent(val)
            if nodeParent == nil && node.Left == nil && node.Right == nil { //如果是根节点,且只有根节点
                t.Root = nil
                return true
            } else if node.Left == nil && node.Right == nil { //删除的节点有父亲节点,但没有子树
                if nodeParent.Left != nil && val == nodeParent.Left.Value { //左子树
                    nodeParent.Left = nil
                } else { //右子树
                    nodeParent.Right = nil
                }
                return true
            } else if node.Left != nil && node.Right != nil { //删除的节点下有两个子树,因为右子树的值都比左子树大,那么用右子树中的最小元素来替换删除的节点,这时二叉查找树的性质又满足了。
                // 找右子树中最小的值,一直往右子树的左边找
                minNode := node.Right
                for minNode.Left != nil {
                    minNode = minNode.Left
                }
                // 把最小的节点删掉
                t.Delete(minNode.Value)
                // 最小值的节点替换被删除节点
                node.Value = minNode.Value
                return true
            } else { //只有一个子树,那么该子树直接替换被删除的节点即可
                // 父亲为空,表示删除的是根节点,替换树根
                if nodeParent == nil {
                    if node.Left != nil {
                        t.Root = node.Left
                    } else {
                        t.Root = node.Right
                    }
                    return true
                }
                // 左子树不为空
                if node.Left != nil {
                    // 如果删除的是节点是父亲的左儿子,让删除的节点的左子树接班
                    if nodeParent.Left != nil && val == nodeParent.Left.Value {
                        nodeParent.Left = node.Left
                    } else {
                        nodeParent.Right = node.Left
                    }
                } else {
                    // 如果删除的是节点是父亲的左儿子,让删除的节点的右子树接班
                    if nodeParent.Left != nil && val == nodeParent.Left.Value {
                        nodeParent.Left = node.Right
                    } else {
                        nodeParent.Right = node.Right
                    }
                }
                return true
            }
        }
        return false
    } else {
        return false
    }
}
 
func main() {
    values := []int{5, 2, 7, 3, 6, 1, 4, 8, 9}
    tree := new(Searc2Tree)
    for _, v := range values {
        tree.Add(v)
    }
    tree.View("first")
    tree.View("mid")
    tree.View("last")
 
}

  

posted @   立志做一个好的程序员  阅读(116)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2019-11-08 [转]LINUX最大线程数及最大进程数
2018-11-08 Typescript declaration: Merge a class and an interface
2018-11-08 Declaration Merging with TypeScript
2018-11-08 Inner Classes with TypeScript
2018-11-08 c语言基础,\r, \n, \r\n
2016-11-08 php 命名空间
2016-11-08 好的习惯

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示