golang实现二叉搜索树
数据结构
首先我们定义需要的数据结构。注意,TreeNode的左右节点都是TreeNode type的,而树只有一个Root数据域,为TreeNode type
type TreeNode struct {
Value int
Left *TreeNode
Right *TreeNode
}
type BinarySearchTree struct {
Root *TreeNode
}
Insert
向二叉搜索树中插入元素,首先要找到插入的位置,然后再插入。这里注意我们的实现方法为给TreeNode和BinarySearchTree这两个type添加方法。
需要注意给type添加方法的方式,同时还要注意,如果你要改变方法调用者,则需要使用指针
func (tree BinarySearchTree) Insert (v int) {
tree.Root.Insert(v)
}
func (node *TreeNode) Insert (v int){
if v < node.Value {
if node.Left != nil{
node.Left.Insert(v)
}else{
node.Left = &TreeNode{v, nil, nil}
}
}else {
if node.Right != nil{
node.Right.Insert(v)
}else{
node.Right = &TreeNode{v, nil, nil}
}
}
}
遍历
树的遍历有前序,后序,中序等等。这里以中序为例。注意slice与slice指针的不同
func (tree BinarySearchTree) InOrder() []int{
var res []int
tree.Root.InOrder(&res)
return res
}
func (node *TreeNode) InOrder(result *[]int) {
if node.Left != nil{
node.Left.InOrder(result)
}
*result = append(*result, node.Value)
if node.Right != nil{
node.Right.InOrder(result)
}
}
最大最小值
func (tree BinarySearchTree) FindMin() int {
node := tree.Root
for {
if node.Left != nil {
node = node.Left
}else{
return node.Value
}
}
}
func (tree BinarySearchTree) FindMax() int {
node := tree.Root
for {
if node.Right != nil {
node = node.Right
}else{
return node.Value
}
}
}
查找
func (tree BinarySearchTree) Contains(v int) bool {
node := tree.Root
for {
if node == nil{
return false
}else if (node.Value == v) {
return true
}else if (node.Value > v){
node = node.Left
}else{
node = node.Right
}
}
}
golang实现二叉搜索树的更多相关文章
- 二叉搜索树的最近公共祖先的golang实现
给定一个二叉搜索树, 找到该树中两个指定节点的最近公共祖先. 百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p.q,最近公共祖先表示为一个结点 x,满足 x 是 p.q 的祖先且 x ...
- 验证二叉搜索树的golang实现
给定一个二叉树,判断其是否是一个有效的二叉搜索树. 一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也是二叉搜索树. ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
随机推荐
- Codeforces 1221 F Choose a Square
题面 不知道大佬们怎么想的,反正我看到这种区间包含性质的并且score只和包含的区间与询问区间挂钩的题,马上就想到了扫描线23333 虽然革命方向无比正确,但却因为SB错误交了5次才 A. WA第一发 ...
- APIO2009 抢掠计划 Tarjan DAG-DP
APIO2009 抢掠计划 Tarjan spfa/DAG-DP 题面 一道\(Tarjan\)缩点水题.因为可以反复经过节点,所以把一个联通快中的所有路口看做一个整体,缩点后直接跑\(spfa\)或 ...
- hive安装运行hive报错通解
参考博文:https://blog.csdn.net/lsxy117/article/details/47703155 大部分问题还是hadoop的配置文件的问题: 修改配置文件hadoop/conf ...
- 省市县数据(txt)
下载地址:https://pan.baidu.com/s/1x1uR9KxzlxAbn5bgN6ZfrQ 提取码:ky8z
- jmeter如何使用正则表达式,如何提取token等想要的数据
使用正则表达式,提取你想要的任何数据, 例如要提取响应结果里的token字段及sex字段(响应内容为: "token":"83EEAA887F1D2F1AA1CDA9E1 ...
- spring cloud学习
https://gitee.com/ylimhhmily/SpringCloudTutorial/tree/master hystrix配置不生效问题https://blog.csdn.net/lvy ...
- Jmeter Web 性能测试入门 (四):一个小实例带你学会 Jmeter 脚本编写
测试场景: 模拟并发100个user,在TesterHome 站内搜索VV00CC 添加线程组 添加HTTP信息头管理器 添加HTTP Sampler 填写HTTP Sampler中的信息 添加监听器 ...
- https证书制作及springboot配置https
1.生成秘钥 openssl genrsa -out private.key 2048 2.生成用于申请请求的证书文件csr,一般会将该文件发送给CA机构进行认证,本例使用自签名证书 openssl ...
- Mac卸载mysql数据库
mac下的应用程序中一般是无法找到mysql的安装文件进行直接删除的,所以,通常需要打开命令行工具Terminal后,使用如下的命令进行删除: 1 sudo rm /usr/local/mysql2 ...
- Vue开发工具VS Code与调试
vscode安装 进入vscode官网(https://code.visualstudio.com/Download)vscode插件安装进入vscode官网插件商店(https://marketpl ...