| 1 | // Copyright (c) 2015, Emir Pasic. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package main |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | |
| 10 | "github.com/emirpasic/gods/v2/trees/btree" |
| 11 | ) |
| 12 | |
| 13 | // BTreeExample to demonstrate basic usage of BTree |
| 14 | func main() { |
| 15 | tree := btree.New[int, string](3) // empty (keys are of type int) |
| 16 | |
| 17 | tree.Put(1, "x") // 1->x |
| 18 | tree.Put(2, "b") // 1->x, 2->b (in order) |
| 19 | tree.Put(1, "a") // 1->a, 2->b (in order, replacement) |
| 20 | tree.Put(3, "c") // 1->a, 2->b, 3->c (in order) |
| 21 | tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order) |
| 22 | tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order) |
| 23 | tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order) |
| 24 | tree.Put(7, "g") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f, 7->g (in order) |
| 25 | |
| 26 | fmt.Println(tree) |
| 27 | // BTree |
| 28 | // 1 |
| 29 | // 2 |
| 30 | // 3 |
| 31 | // 4 |
| 32 | // 5 |
| 33 | // 6 |
| 34 | // 7 |
| 35 | |
| 36 | _ = tree.Values() // []interface {}{"a", "b", "c", "d", "e", "f", "g"} (in order) |
| 37 | _ = tree.Keys() // []interface {}{1, 2, 3, 4, 5, 6, 7} (in order) |
| 38 | |
| 39 | tree.Remove(2) // 1->a, 3->c, 4->d, 5->e, 6->f (in order) |
| 40 | fmt.Println(tree) |
| 41 | // BTree |
| 42 | // 1 |
| 43 | // 3 |
| 44 | // 4 |
| 45 | // 5 |
| 46 | // 6 |
| 47 | |
| 48 | tree.Clear() // empty |
| 49 | tree.Empty() // true |
| 50 | tree.Size() // 0 |
| 51 | |
| 52 | // Other: |
| 53 | tree.Height() // gets the height of the tree |
| 54 | tree.Left() // gets the left-most (min) node |
| 55 | tree.LeftKey() // get the left-most (min) node's key |
| 56 | tree.LeftValue() // get the left-most (min) node's value |
| 57 | tree.Right() // get the right-most (max) node |
| 58 | tree.RightKey() // get the right-most (max) node's key |
| 59 | tree.RightValue() // get the right-most (max) node's value |
| 60 | } |