1
+ @kwdef mutable struct BTreeNode{T}
2
+ degree:: Int
3
+ keys:: Vector{T} = T[]
4
+ children:: Vector{BTreeNode{T}} = BTreeNode{T}[]
5
+ parent:: Union{Nothing, BTreeNode{T}} = nothing
6
+ end
7
+
8
+ reachMaxKeys (node:: BTreeNode ) = length (node. keys) >= node. degree
9
+ isempty (node:: BTreeNode ) = length (node. keys) == 0
10
+ isleaf (node:: BTreeNode ) = length (node. children) == 0
11
+ isroot (node:: BTreeNode ) = isnothing (node. parent)
12
+
13
+ function rootof (node:: BTreeNode ):: BTreeNode
14
+ current = node
15
+ while ! isroot (current)
16
+ current = current. parent
17
+ end
18
+
19
+ return current
20
+ end
21
+
22
+ # TODO insert at root, return the root of node
23
+ # 在当前的 node 中插入数据
24
+ # data 表示要插入的数据
25
+ # nodeOfDataInRight 是在 split! 时不为空值,他这个时候表示分裂出的新节点
26
+ function insert! (node:: BTreeNode{T} , data:: T , nodeOfDataInRight:: Union{Nothing, BTreeNode{T}} ):: BTreeNode{T} where T
27
+ index = (Iterators. takewhile (x -> x < data, node. keys) |> collect |> length) + 1
28
+ insert! (node. keys, index, data)
29
+
30
+ if ! isnothing (nodeOfDataInRight)
31
+ insert! (node. children, index + 1 , nodeOfDataInRight)
32
+ nodeOfDataInRight. parent = node
33
+ end
34
+
35
+ root = if ! reachMaxKeys (node)
36
+ node
37
+ else
38
+ split! (node)
39
+ end
40
+
41
+ return rootof (root)
42
+ end
43
+
44
+ # TODO split this node
45
+ # has been reachMaxKeys
46
+ function split! (node:: BTreeNode{T} ):: BTreeNode{T} where T
47
+ # 将 node.keys 分割成两部分,
48
+ # 中间的元素是 upIndex, 我们取右边的 keys[upIndex + 1 : end] 来作为新节点的元素,
49
+ # 将 children[upIndex + 2 : end] 来作为新节点的字节点元素
50
+ upIndex = ceil (Int, node. degree / 2 )
51
+ up = node. keys[upIndex]
52
+
53
+ rightKeys = node. keys[upIndex + 1 : end ]
54
+ rightChildren = node. children[upIndex + 2 : end ]
55
+
56
+ # 将冗余的数据去除
57
+ deleteat! (node. keys, upIndex : length (node. keys))
58
+ deleteat! (node. children, upIndex + 2 : length (node. children))
59
+
60
+ # 新建一个节点作为新节点
61
+ rightNode = BTreeNode {T} (degree = node. degree, keys = rightKeys, children = rightChildren)
62
+
63
+ for child in rightChildren
64
+ child. parent = rightNode
65
+ end
66
+
67
+ if isroot (node) # 如果这个节点是根节点,新建一个节点作为新的根节点
68
+ parent = BTreeNode {T} (degree = node. degree)
69
+ push! (parent. keys, up)
70
+ push! (parent. children, node, rightNode)
71
+
72
+ node. parent = parent
73
+ rightNode. parent = parent
74
+
75
+ return parent
76
+ else # 不然的话,将 up 的值插入到 node.parent 中的 keys 中
77
+ insert! (node. parent, up, rightNode)
78
+
79
+ return node. parent
80
+ end
81
+ end
82
+
83
+ function printnode (io:: IO , node:: BTreeNode , depth:: Int )
84
+ for _ in 1 : depth
85
+ print (io, " | " )
86
+ end
87
+
88
+ if depth > 0
89
+ print (io, " |-----" )
90
+ end
91
+
92
+ print (io, node. keys)
93
+ println (io)
94
+ for child in node. children
95
+ printnode (io, child, depth + 1 )
96
+ end
97
+ end
98
+
99
+ show (io:: IO , node:: BTreeNode ) = printnode (io, node, 0 )
100
+ print (io:: IO , node:: BTreeNode ) = show (io, node)
101
+
102
+ function findnode (node:: BTreeNode{T} , target:: T ):: BTreeNode{T} where T
103
+ if isempty (node)
104
+ return node
105
+ end
106
+
107
+ index = 1
108
+ while index <= length (node. keys) && node. keys[index] <= target
109
+ if node. keys[index] == target
110
+ return node
111
+ end
112
+
113
+ index += 1
114
+ end
115
+
116
+ return if isleaf (node)
117
+ node
118
+ else
119
+ findnode (node. children[index], target)
120
+ end
121
+ end
0 commit comments