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. 这道 ...
随机推荐
- QPS 和 TPS
- python while for 语句
while 语句: >>> x = >>> : ... x += ... print(x) ... for 语句: >>> words = ['a ...
- E. The Contest ( 简单DP || 思维 + 贪心)
传送门 题意: 有 n 个数 (1 ~ n) 分给了三个人 a, b, c: 其中 a 有 k1 个, b 有 k2 个, c 有 k3 个. 现在问最少需要多少操作,使得 a 中所有数 是 1 ~ ...
- [Luogu] 魔板
https://www.luogu.org/problemnew/show/P1275 #include <iostream> #include <cstdio> #inclu ...
- MySQL 取分组后每组的最新记录
修改<常用SQL之日期格式化和查询重复数据>中表test1的创建时间,修改后的测试数据如下: 以姓名分组后提取每组最新创建的记录: SELECT a.* FROM test1 AS a, ...
- 手写alert弹框(一)
采用原生的JavaScript, html代码 <meta name="viewport" content="width=device-width, initial ...
- NMS(非极大值抑制)实现
1.IOU计算 设两个边界框分别为A,B.A的坐标为Ax1,Ax2,Ay1,Ay2,且Ax1 < Ax2,Ay1 < Ay2.B和A类似. 则IOU为A∩B除以A∪B. 当两个边界框有重叠 ...
- Linux之破解root密码
破解CentOs6的root口令 进入单用户模式 重启后在下面界面按任意键 进入此界面后“ a ”在下面界面 输入1 进入单用户模式1之前有空格 按回车键进入命令行执行passwd命令直接修 ...
- SQLyog Enterprise Trial 试用期问题
SQLyog Enterprise Trial 是 SQLyog的试用版,有效期30天:试用期过后提示购买之后才能使用:解决办法:修改注册表(过期就得改比较麻烦,但暂时可以用,等有时间了再找其他办法) ...
- 【SpringBoot】整体下载大文件与分批下载大文件(利用MySql数据库的Limit实现)
在前文里谈过一次性从数据库取一个大结果集有可能导致outofMemory,当时的想法是分批去取回来,今天把它实现了,特地把代码分享出来: 工程下载:https://files.cnblogs.com/ ...