Muxx


  • 首页

  • 归档

  • 标签

  • 关于

  • 搜索

数据结构:二叉搜索树

发表于 2020-01-15

基本概念

二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树),它或者是一棵空树,或者是具有下列性质的二叉树:

  • 若左子树不空,则左子树上所有结点的值均小于它的根结构的值
  • 若右子树不空,则右子树上所有结点的值均大于它的根结点的值
  • 它的左右子树也分别是二叉查找树

规律:
一棵BST节点数为l,索引从0开始。对于索引为i的节点,

  • 二叉排序树进行中序遍历可得到一个有序序列
  • 其左子树节点索引为2i+1,右子树节点索引为2i+2,
  • 父节点索引为(i-1)/2
  • 最后一个非叶子节点索引为l/2-1

解释说明: 构造二叉排序树不是为了排序,而是为了提高查找和插入的速度。在一个有序数据集上的查找速度总是要快于无序的数据集的,而且二叉排序树这种链式存储结构,也有利于插入和删除的速度。

操作

新建

1
2
3
4
5
6
7
8
9
10
11
12
func generate(arr []int)*TreeNode{
var root *TreeNode
for _,v:=range arr{
if root==nil{
root = &TreeNode{Val:v}
}else{
insert(root,&TreeNode{Val:v})
}
}
return root
}

查找

1
2
3
4
5
6
7
8
9
10
11
12
13
func search(root *TreeNode,key int)*TreeNode{
if root==nil{
return root
}
if root.Val==key{
return root
}else if key<root.Val{
return search(root.Left,key)
}else{
return search(root.Right,key)
}
}

插入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
func insert(root *TreeNode,node *TreeNode){
if root.Val==node.Val{
return
}
if node.Val<root.Val{
if root.Left==nil{
root.Left=node
}else{
insert(root.Left,node)
}
}else{
if root.Right==nil{
root.Right=node
}else{
insert(root.Right,node)
}
}
}

删除

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
func delete(root *TreeNode,key int)*TreeNode{
if root==nil{
return root
}
if root.Val==key{
if root.Left==nil && root.Right ==nil{
return nil
}else if root.Left==nil{
return root.Right
}else if root.Right ==nil{
return root.Left
}else{
successor:=findMinNode(root.Right)
successor_copy:=&TreeNode{Val:successor.Val}
successor_copy.Left = root.Left
successor_copy.Right = removeMinNode(root.Right)
return successor_copy
}
}else if root.Val>key{
root.Left = delete(root.Left,key)
return root
}else{
root.Right = delete(root.Right,key)
return root
}
}

常用逻辑块

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
//查找并返回BST最小的节点
func findMinNode(root *TreeNode)*TreeNode{
if root==nil{
return nil
}
for root.Left!=nil{
root = root.Left
}
return root
}
//删除BST最小节点,并返回新根节点
func removeMinNode(root *TreeNode)*TreeNode{
if root==nil{
return nil
}
//root.Left==nil时,root是最小的节点,删除root后返回root.Right
if root.Left==nil{
tmp:=root.Right
root.Right = nil //为什么要执行这个动作?
return tmp
}
//当root.Left!=nil时,对root左子树继续查找删除
root.Left = removeMinNode(root.Left)
return root
}
问题:如果不把待删除节点的左右节点设置为nil会怎么样?
如果不把待删除节点的左右节点设置为 nil ,也即是把 root.left = nil root.right = nil 这两行代码注释掉,也可以得到一个 Accept。
写这两行代码是出于面向对象的程序语言(Python 和 Java 都是)的垃圾回收机制(Garbage Collection,GC)的考虑。如果一个对象没有被其它对象引用,它会在合适的时候被垃圾回收机制回收,被垃圾回收机制回收即是真正从内存中删除了,语义上也没有必要保留这两个引用。
“垃圾回收机制”不在我能讲解的范围内,建议搜索该关键字获得相关的知识。欢迎讨论。

理解字节序

发表于 2019-12-14

简单理解

大端模式(Big-Endian),是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中,与人类阅读习惯一致
小端模式(Little-Endian),是指数据的高字节保存在内存的高地址中,而数据的低字节保存在内存的低地址中

阅读全文 »

elk笔记整理

发表于 2019-11-22

Elasticsearch入门

基本概念

文档
    elasticsearch是面向文档的,文档是所有可搜索数据的最小单位
    文档会被序列化成json格式
    每个文档都有一个unique id
    一篇文档包含一系列字段,类似数据库表中一条记录
    json文档,格式灵活,不需要预先定义格式
        字段的类型可以指定或通过es自动推算
        支持数组/支持嵌套
        ![2019101115707263107291.png](http://pic.aipp.vip/2019101115707263107291.png)
    文档的元数据 
        元数据用于标注文档的相关信息
        _index 文档所属的索引名
        _type 文档所属的类型名
        _id 文档唯一id
        _source 文档的原始json数据
        _all 整合所有字段内容到该字段,已被废除
        _version 文档的版本信息
        _score 相关性打分
阅读全文 »

避免使用bigkey

发表于 2019-11-14

摘要

redis bigkey即数据量大的key,比如字符串value值非常大,哈希、列表、集合、有序集合元素非常多等。由于其数据大小远大于其他key,容易造成内存不均、超时阻塞、网络流量拥塞等一系列问题

阅读全文 »

切片修改问题

发表于 2019-10-31

问题描述

1
2
3
4
5
6
7
8
9
func test(a [][]int) {
a = append(a, []int{1, 2, 3})
}
func main() {
a := [][]int{}
test(a)
fmt.Println(a) //0
}

执行完后a居然是空?切片内部不是存放了指针吗?

阅读全文 »

logstash无法写ES的灵异事件

发表于 2019-10-24

问题描述

快晚饭时,突然QQ群被同时@,”xxx,为啥ES查不到新的数据了?是Logstash挂了?”
邪门,ES平稳运行这么长时间咋还说挂就挂,是不是logstash有啥问题?打开logstash日志看到了密密麻麻的错误信息

1
2
3
4
5
6
...
[2019-10-24T10:51:15,601][ERROR][logstash.outputs.elasticsearch] Attempted to send a bulk request to elasticsearch' but Elasticsearch appears to be unreachable or down! {:error_message=>"Elasticsearch Unreachable: [http://127.0.0.1:9200/][Manticore::SocketTimeout] Read timed out", :class=>"LogStash::Outputs::ElasticSearch::HttpClient::Pool::HostUnreachableError", :will_retry_in_seconds=>2}
[2019-10-24T10:51:15,929][WARN ][logstash.outputs.elasticsearch] Marking url as dead. Last error: [LogStash::Outputs::ElasticSearch::HttpClient::Pool::HostUnreachableError] Elasticsearch Unreachable: [http://127.0.0.1:9200/][Manticore::SocketTimeout] Read timed out {:url=>http://127.0.0.1:9200/, :error_message=>"Elasticsearch Unreachable: [http://127.0.0.1:9200/][Manticore::SocketTimeout] Read timed out", :error_class=>"LogStash::Outputs::ElasticSearch::HttpClient::Pool::HostUnreachableError"}
[2019-10-24T10:51:15,932][ERROR][logstash.outputs.elasticsearch] Attempted to send a bulk request to elasticsearch' but Elasticsearch appears to be unreachable or down! {:error_message=>"Elasticsearch Unreachable: [http://127.0.0.1:9200/][Manticore::SocketTimeout] Read timed out", :class=>"LogStash::Outputs::ElasticSearch::HttpClient::Pool::HostUnreachableError", :will_retry_in_seconds=>2}
[2019-10-24T10:51:15,959][WARN ][logstash.outputs.elasticsearch] Marking url as dead. Last error: [LogStash::Outputs::ElasticSearch::HttpClient::Pool::HostUnreachableError] Elasticsearch Unreachable: [http://127.0.0.1:9200/][Manticore::SocketTimeout] Read timed out {:url=>http://127.0.0.1:9200/, :error_message=>"Elasticsearch Unreachable: [http://127.0.0.1:9200/][Manticore::SocketTimeout] Read timed out", :error_class=>"LogStash::Outputs::ElasticSearch::HttpClient::Pool::HostUnreachableError"}
...
阅读全文 »

理解数据结构堆

发表于 2019-10-22

基本概念

满足所有父节点都大于(小于等于)子节点的完全二叉树就是堆
其中包含两点
1.完全二叉树
2.父节点大于子节点 大/小根堆

阅读全文 »

ip_conntrack: table full问题

发表于 2019-10-21

问题描述

线上wenb机访问量很大时发现网络丢包的情况,通过dmesg查看日志,发现如下信息

1
2
kernel: ip_conntrack: table full, dropping packet.
kernel: printk: 1 messages suppressed.
阅读全文 »
1…456…29
Mu

Mu

230 日志
53 标签
© 2021 Mu
由 Hexo 强力驱动
主题 - NexT.Pisces