btree: rename Tree.t into t.degree
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Antoine Bartuccio 2019-07-30 18:20:47 +02:00
parent 8dcadb219d
commit 48e43c10eb
Signed by: klmp200
GPG Key ID: E7245548C53F904B

View File

@ -13,7 +13,7 @@ type Tree struct {
// Node is a Node of a Btree // Node is a Node of a Btree
type Node struct { type Node struct {
numberOfKeys int // The number of keys really stored numberOfKeys int // The number of keys really stored
t int // The value of t dependes upon disk blok size degree int // The value of degree dependes upon disk blok size
isLeaf bool isLeaf bool
keys []int keys []int
children []*Node children []*Node
@ -30,14 +30,14 @@ func NewBtree(t int) *Tree {
} }
} }
func newNode(t int, isLeaf bool) *Node { func newNode(degree int, isLeaf bool) *Node {
return &Node{ return &Node{
numberOfKeys: 0, numberOfKeys: 0,
t: t, degree: degree,
isLeaf: isLeaf, isLeaf: isLeaf,
keys: make([]int, 2*t-1), keys: make([]int, 2*degree-1),
children: make([]*Node, 2*t), children: make([]*Node, 2*degree),
} }
} }
@ -164,7 +164,7 @@ func (n *Node) search(k int) *Node {
} }
func (n *Node) isFull() bool { func (n *Node) isFull() bool {
return n.numberOfKeys == 2*n.t-1 return n.numberOfKeys == 2*n.degree-1
} }
func (n *Node) insertNonFull(k int) { func (n *Node) insertNonFull(k int) {
@ -212,24 +212,24 @@ func (n *Node) insertNonFull(k int) {
func (n *Node) splitChild(i int, y *Node) { func (n *Node) splitChild(i int, y *Node) {
// Create a new node that will store (t-1) keys of y // Create a new node that will store (t-1) keys of y
z := newNode(y.t, y.isLeaf) z := newNode(y.degree, y.isLeaf)
z.numberOfKeys = n.t - 1 z.numberOfKeys = n.degree - 1
// Copy the last (t-1) keys of y to z // Copy the last (t-1) keys of y to z
for j := 0; j < n.t-1; j++ { for j := 0; j < n.degree-1; j++ {
z.keys[j] = y.keys[j+n.t] z.keys[j] = y.keys[j+n.degree]
if !y.isLeaf { if !y.isLeaf {
z.children[j] = y.children[j+n.t] z.children[j] = y.children[j+n.degree]
} }
} }
// Copy the last t children of y to z // Copy the last t children of y to z
if !y.isLeaf { if !y.isLeaf {
z.children[n.t-1] = y.children[2*n.t-1] z.children[n.degree-1] = y.children[2*n.degree-1]
} }
// Reduce the number of keys in y // Reduce the number of keys in y
y.numberOfKeys = n.t - 1 y.numberOfKeys = n.degree - 1
// Since this node is going to have a new child, create space for it // Since this node is going to have a new child, create space for it
for j := n.numberOfKeys; j >= i+1; j-- { for j := n.numberOfKeys; j >= i+1; j-- {
@ -246,7 +246,7 @@ func (n *Node) splitChild(i int, y *Node) {
} }
// Copy the middle key of y to this node // Copy the middle key of y to this node
n.keys[i] = y.keys[n.t-1] n.keys[i] = y.keys[n.degree-1]
// Increment the count of keys in this node // Increment the count of keys in this node
n.numberOfKeys++ n.numberOfKeys++
@ -286,7 +286,7 @@ func (n *Node) remove(k int) error {
} }
// If the child where is the key has less than t keys, wi fill it // If the child where is the key has less than t keys, wi fill it
if n.children[index].numberOfKeys < n.t { if n.children[index].numberOfKeys < n.degree {
n.fill(index) n.fill(index)
} }
@ -320,7 +320,7 @@ func (n *Node) removeFromNonLeaf(index int) error {
// If the child that precedes k has at least t keys, // If the child that precedes k has at least t keys,
// find the predecessor of k in the subtree and replace k with it // find the predecessor of k in the subtree and replace k with it
// Recursively delete the predecessor in the child // Recursively delete the predecessor in the child
if n.children[index].numberOfKeys >= n.t { if n.children[index].numberOfKeys >= n.degree {
pred := n.getPred(index) pred := n.getPred(index)
n.keys[index] = pred n.keys[index] = pred
return n.children[index].remove(pred) return n.children[index].remove(pred)
@ -329,7 +329,7 @@ func (n *Node) removeFromNonLeaf(index int) error {
// If the child has less than t keys, examine children[index+1] // If the child has less than t keys, examine children[index+1]
// If it has at least t keys, find the successor of k in this subtree // If it has at least t keys, find the successor of k in this subtree
// Replace k by its successor and recursively delete the successor in the subtree // Replace k by its successor and recursively delete the successor in the subtree
if n.children[index+1].numberOfKeys >= n.t { if n.children[index+1].numberOfKeys >= n.degree {
succ := n.getSucc(index) succ := n.getSucc(index)
n.keys[index] = succ n.keys[index] = succ
return n.children[index+1].remove(succ) return n.children[index+1].remove(succ)
@ -371,13 +371,13 @@ func (n *Node) getSucc(index int) int {
func (n *Node) fill(index int) { func (n *Node) fill(index int) {
// If the previous child has more than t-1 keys, borrow a key from that child // If the previous child has more than t-1 keys, borrow a key from that child
if index != 0 && n.children[index-1].numberOfKeys >= n.t { if index != 0 && n.children[index-1].numberOfKeys >= n.degree {
n.borrowFromPrev(index) n.borrowFromPrev(index)
return return
} }
// If the next child has more than t-1 keys, borrow a key from that child // If the next child has more than t-1 keys, borrow a key from that child
if index != n.numberOfKeys && n.children[index+1].numberOfKeys >= n.t { if index != n.numberOfKeys && n.children[index+1].numberOfKeys >= n.degree {
n.borrowFromNext(index) n.borrowFromNext(index)
return return
} }
@ -466,19 +466,19 @@ func (n *Node) merge(index int) {
sibling := n.children[index+1] sibling := n.children[index+1]
// Pulls a key from the current node ande insert it into the (t-1)th position // Pulls a key from the current node ande insert it into the (t-1)th position
child.keys[n.t-1] = n.keys[index] child.keys[n.degree-1] = n.keys[index]
// Copies the keys from children[index+1] to children[index] at the end // Copies the keys from children[index+1] to children[index] at the end
for i := 0; i < sibling.numberOfKeys; i++ { for i := 0; i < sibling.numberOfKeys; i++ {
child.keys[i+n.t] = sibling.keys[i] child.keys[i+n.degree] = sibling.keys[i]
if !child.isLeaf { if !child.isLeaf {
child.children[i+n.t] = sibling.children[i] child.children[i+n.degree] = sibling.children[i]
} }
} }
// Copies the child pointers from C[index+1] to children[index] // Copies the child pointers from C[index+1] to children[index]
if !child.isLeaf { if !child.isLeaf {
child.children[sibling.numberOfKeys+n.t] = sibling.children[sibling.numberOfKeys] child.children[sibling.numberOfKeys+n.degree] = sibling.children[sibling.numberOfKeys]
} }
// Moves all keys after index in the current node one step before // Moves all keys after index in the current node one step before