From d8313c758e7f69413ff02e47ea39587ecce752b0 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 31 Jul 2019 15:56:52 +0200 Subject: [PATCH] btree: avoid duplication of keys --- btree/btree.go | 40 +++++++++++++++++++++++++--------------- btree/btree_test.go | 4 +++- main.go | 11 ++++------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/btree/btree.go b/btree/btree.go index b4269a7..fd04799 100644 --- a/btree/btree.go +++ b/btree/btree.go @@ -86,7 +86,11 @@ func (t *Tree) Search(k string) (string, error) { return "", fmt.Errorf("The key %s does not exist", k) } - return t.root.search(&data{key: k}) + if n, i := t.root.search(&data{key: k}); n != nil { + return n.keys[i].value, nil + } + + return "", fmt.Errorf("The key %s does not exist", k) } // Remove k in the tree @@ -123,11 +127,17 @@ func (t *Tree) Insert(key, value string) { // If the tree is empty if t.root == nil { t.root = newNode(t.degree, true) - t.root.insertKey(0, k) + t.root.writeKey(0, k) t.root.numberOfKeys = 1 return } + // Search for an existing key and replace it + if node, i := t.root.search(k); node != nil { + node.writeKey(i, k) + return + } + // If the tree is not empty if !t.root.isFull() { // If root is not full, insert in non full root @@ -158,8 +168,8 @@ func (t *Tree) Insert(key, value string) { // node methods -// insertKey at i place in the node -func (n *node) insertKey(i int, k *data) { +// writeKey at i place in the node +func (n *node) writeKey(i int, k *data) { n.keys[i] = k } @@ -192,7 +202,7 @@ func (n *node) visualize(prefix string, isEnd bool) { // traverse all nodes in a subtree rooted with this node func (n *node) traverse() { - // There are n entries and n+1 children, treverse trough n keys and n first children + // There are n entries and n+1 children, traverse trough n keys and n first children for i := 0; i < n.numberOfKeys; i++ { // If this is not a leaf, then traverse the subtree before printing the key if !n.isLeaf { @@ -208,7 +218,7 @@ func (n *node) traverse() { } // search k in the subtree rooted with this node -func (n *node) search(k *data) (string, error) { +func (n *node) search(k *data) (*node, int) { // Find the first entry greater than or equal to k i := 0 @@ -217,16 +227,16 @@ func (n *node) search(k *data) (string, error) { } // If the found key is equal to k, return this node - if n.keys[i] != nil && k.eq(n.keys[i]) { - return n.keys[i].value, nil + if i < n.numberOfKeys && k.eq(n.keys[i]) { + return n, i } // If the key is not found here and this is a leaf node if n.isLeaf { - return "", fmt.Errorf("The key %s does not exist", k.key) + return nil, -1 } - // Go to the approipriate child + // Go to the appropriate child return n.children[i].search(k) } @@ -249,24 +259,24 @@ func (n *node) insertNonFull(k *data) { } // Insert the new key at the found location - n.insertKey(i+1, k) + n.writeKey(i+1, k) n.numberOfKeys++ return } // If this is not a leaf - // Finds the child wich is going to have the new key + // Finds the child which is going to have the new key for i >= 0 && n.keys[i].gt(k) { i-- } - // Check if the found chird is full + // Check if the found child is full if n.children[i+1].isFull() { // If the child is full, then split it n.splitChild(i+1, n.children[i+1]) // After the split, the middle key of children[i] goes up and - // children[i] is splitted into two + // children[i] is splited into two // See which of those two is going to have the new key if n.keys[i+1].lt(k) { i++ @@ -352,7 +362,7 @@ func (n *node) remove(k *data) error { isInLastChild = true } - // 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, we fill it if n.children[index].numberOfKeys < n.degree { n.fill(index) } diff --git a/btree/btree_test.go b/btree/btree_test.go index c85c693..c569cb7 100644 --- a/btree/btree_test.go +++ b/btree/btree_test.go @@ -84,7 +84,7 @@ func ExampleTree_Traverse() { tree := NewBtree(3) toInsert := [][]string{ - []string{"a", "A"}, + []string{"a", "C"}, []string{"b", "B"}, []string{"c", "C"}, []string{"d", "D"}, @@ -106,6 +106,8 @@ func ExampleTree_Traverse() { tree.Insert(k[0], k[1]) } + tree.Insert("a", "A") + tree.Visualize() fmt.Println("") diff --git a/main.go b/main.go index 4d289ed..e37ade3 100644 --- a/main.go +++ b/main.go @@ -10,17 +10,14 @@ func main() { // Example program t := btree.NewBtree(3) - t.Insert("animal", "dog") + toInsert := [][]string{[]string{"climate", "change"}, []string{"machine", "doing"}, []string{"remember", "built"}, []string{"interest", "enjoy"}, []string{"no", "duty"}, []string{"contrast", "zoo"}, []string{"alike", "adventure"}, []string{"neck", "corn"}, []string{"union", "bark"}, []string{"elephant", "length"}, []string{"major", "light"}, []string{"interior", "camp"}, []string{"wind", "until"}, []string{"particles", "final"}, []string{"problem", "east"}, []string{"iron", "pattern"}, []string{"age", "personal"}, []string{"opportunity", "frog"}, []string{"goose", "sort"}, []string{"action", "effect"}, []string{"prevent", "stop"}, []string{"planned", "my"}, []string{"suit", "stood"}, []string{"orange", "forth"}, []string{"obtain", "door"}, []string{"list", "sink"}, []string{"official", "telephone"}, []string{"forest", "string"}, []string{"shade", "globe"}, []string{"city", "fighting"}, []string{"mixture", "line"}, []string{"mouth", "stomach"}, []string{"charge", "among"}, []string{"shorter", "spend"}, []string{"scale", "giant"}, []string{"spite", "image"}, []string{"evidence", "view"}, []string{"nobody", "hunter"}, []string{"cent", "recognize"}, []string{"everywhere", "avoid"}, []string{"handle", "influence"}, []string{"missing", "soft"}, []string{"strange", "seed"}, []string{"carry", "itself"}, []string{"route", "thought"}, []string{"about", "song"}, []string{"browserling", "compass"}, []string{"fort", "led"}, []string{"origin", "through"}, []string{"cover", "exciting"}, []string{"joined", "rod"}, []string{"dress", "highest"}, []string{"differ", "hall"}, []string{"slope", "discuss"}, []string{"fuel", "themselves"}, []string{"am", "final"}, []string{"straight", "constantly"}, []string{"eager", "carry"}, []string{"represent", "telephone"}, []string{"early", "something"}, []string{"young", "wagon"}, []string{"them", "late"}, []string{"space", "late"}, []string{"until", "industry"}, []string{"weigh", "president"}, []string{"fly", "power"}, []string{"average", "attempt"}, []string{"present", "rhyme"}, []string{"dear", "vowel"}, []string{"pay", "future"}, []string{"remarkable", "usual"}, []string{"brass", "type"}, []string{"blood", "began"}, []string{"size", "cat"}, []string{"around", "because"}, []string{"direction", "creature"}, []string{"lie", "blind"}, []string{"scientist", "effort"}, []string{"hardly", "dried"}, []string{"own", "fact"}, []string{"say", "direction"}, []string{"highest", "stronger"}, []string{"wait", "gently"}, []string{"old", "highway"}, []string{"eight", "four"}, []string{"active", "bread"}, []string{"square", "show"}, []string{"neighborhood", "cloud"}, []string{"flew", "separate"}, []string{"occur", "tower"}, []string{"forty", "heading"}, []string{"control", "move"}, []string{"carried", "interior"}, []string{"total", "found"}, []string{"strike", "nearby"}, []string{"mind", "talk"}, []string{"letter", "cost"}, []string{"theory", "stepped"}, []string{"remain", "fifteen"}, []string{"newspaper", "hot"}, []string{"beautiful", "arrive"}, []string{"compound", "stock"}, []string{"native", "putting"}, []string{"nodded", "sentence"}, []string{"more", "event"}, []string{"load", "nodded"}, []string{"aboard", "nation"}, []string{"continent", "work"}, []string{"buffalo", "locate"}, []string{"hunt", "outline"}, []string{"wealth", "till"}, []string{"smoke", "its"}, []string{"pair", "swim"}, []string{"necessary", "border"}, []string{"leaf", "label"}, []string{"round", "production"}, []string{"our", "wide"}, []string{"stairs", "trade"}, []string{"known", "plus"}, []string{"chose", "stronger"}, []string{"ear", "flame"}, []string{"pride", "spring"}, []string{"plates", "two"}, []string{"accurate", "sister"}, []string{"learn", "music"}, []string{"wrong", "gold"}, []string{"climate", "coffee"}, []string{"traffic", "fence"}, []string{"maybe", "aid"}, []string{"pilot", "spent"}, []string{"look", "silver"}, []string{"stretch", "book"}, []string{"back", "tea"}, []string{"wherever", "before"}, []string{"verb", "indeed"}, []string{"enemy", "bush"}, []string{"current", "may"}, []string{"be", "cover"}, []string{"task", "seldom"}, []string{"help", "typical"}, []string{"moon", "during"}, []string{"arm", "come"}, []string{"event", "shape"}, []string{"she", "needs"}, []string{"never", "nobody"}, []string{"couple", "corn"}, []string{"chance", "see"}, []string{"decide", "country"}, []string{"community", "adjective"}, []string{"none", "disappear"}, []string{"grandfather", "talk"}, []string{"white", "tank"}, []string{"attention", "early"}, []string{"sand", "score"}, []string{"speed", "friendly"}, []string{"however", "typical"}, []string{"mix", "medicine"}, []string{"support", "dozen"}, []string{"swim", "ahead"}, []string{"little", "most"}, []string{"still", "recent"}, []string{"anyone", "slipped"}, []string{"explore", "gold"}, []string{"coast", "thousand"}, []string{"written", "gather"}, []string{"heart", "somewhere"}, []string{"yard", "means"}, []string{"being", "at"}, []string{"pine", "repeat"}, []string{"kill", "rope"}, []string{"riding", "arrive"}, []string{"tune", "pile"}, []string{"nervous", "shake"}, []string{"married", "actual"}, []string{"desk", "speak"}, []string{"least", "hurried"}, []string{"coach", "guess"}, []string{"cutting", "cover"}, []string{"most", "establish"}, []string{"view", "back"}, []string{"duck", "tea"}, []string{"minute", "market"}, []string{"valley", "hurried"}, []string{"tea", "pressure"}, []string{"possible", "troops"}, []string{"ship", "dish"}, []string{"needle", "pour"}, []string{"creature", "death"}, []string{"behind", "swung"}, []string{"each", "circus"}, []string{"animal", "dog"}, []string{"potato", "fries"}, []string{"6", "number"}, []string{"12", "number"}, []string{"car", "ferrari"}, []string{"7", "number"}, []string{"plane", "airbus"}} + for _, k := range toInsert { + t.Insert(k[0], k[1]) + } s, _ := t.Search("animal") fmt.Println(s) - t.Insert("potato", "fries") - t.Insert("6", "number") - t.Insert("12", "number") - t.Insert("car", "ferrari") - t.Insert("7", "number") - t.Insert("plane", "airbus") t.Insert("animal", "cat") s, _ = t.Search("animal")