github.com/mymmsc/gox@v1.3.33/util/examples/avltree/avltree.go (about) 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 avl "github.com/mymmsc/gox/util/avltree" 10 ) 11 12 // AVLTreeExample to demonstrate basic usage of AVLTree 13 func main() { 14 tree := avl.NewWithIntComparator() // empty(keys are of type int) 15 16 tree.Put(1, "x") // 1->x 17 tree.Put(2, "b") // 1->x, 2->b (in order) 18 tree.Put(1, "a") // 1->a, 2->b (in order, replacement) 19 tree.Put(3, "c") // 1->a, 2->b, 3->c (in order) 20 tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order) 21 tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order) 22 tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order) 23 24 fmt.Println(tree) 25 // 26 // AVLTree 27 // │ ┌── 6 28 // │ ┌── 5 29 // └── 4 30 // │ ┌── 3 31 // └── 2 32 // └── 1 33 34 _ = tree.Values() // []interface {}{"a", "b", "c", "d", "e", "f"} (in order) 35 _ = tree.Keys() // []interface {}{1, 2, 3, 4, 5, 6} (in order) 36 37 tree.Remove(2) // 1->a, 3->c, 4->d, 5->e, 6->f (in order) 38 fmt.Println(tree) 39 // 40 // AVLTree 41 // │ ┌── 6 42 // │ ┌── 5 43 // └── 4 44 // └── 3 45 // └── 1 46 47 tree.Clear() // empty 48 tree.Empty() // true 49 tree.Size() // 0 50 }