github.com/richardwilkes/toolbox@v1.121.0/collection/redblack/tree_test.go (about) 1 // Copyright (c) 2016-2024 by Richard A. Wilkes. All rights reserved. 2 // 3 // This Source Code Form is subject to the terms of the Mozilla Public 4 // License, version 2.0. If a copy of the MPL was not distributed with 5 // this file, You can obtain one at http://mozilla.org/MPL/2.0/. 6 // 7 // This Source Code Form is "Incompatible With Secondary Licenses", as 8 // defined by the Mozilla Public License, version 2.0. 9 10 package redblack_test 11 12 import ( 13 "cmp" 14 "testing" 15 16 "github.com/richardwilkes/toolbox/check" 17 "github.com/richardwilkes/toolbox/collection/redblack" 18 ) 19 20 func TestRedBlackTree(t *testing.T) { 21 rbt := redblack.New[int, int](cmp.Compare[int]) 22 check.Equal(t, 0, rbt.Count()) 23 24 rbt.Insert(10, 10) 25 check.Equal(t, 1, rbt.Count()) 26 27 result, ok := rbt.Get(10) 28 check.True(t, ok) 29 check.Equal(t, 10, result) 30 31 rbt.Remove(10) 32 check.Equal(t, 0, rbt.Count()) 33 34 rbt.Insert(10, 10) 35 rbt.Insert(5, 5) 36 rbt.Insert(15, 15) 37 check.Equal(t, 3, rbt.Count()) 38 39 var values []int 40 rbt.Traverse(func(_, value int) bool { 41 values = append(values, value) 42 return true 43 }) 44 check.Equal(t, 3, len(values)) 45 check.Equal(t, []int{5, 10, 15}, values) 46 47 rbt.Insert(10, 10) 48 check.Equal(t, 4, rbt.Count()) 49 50 values = nil 51 rbt.Traverse(func(_, value int) bool { 52 values = append(values, value) 53 return true 54 }) 55 check.Equal(t, 4, len(values)) 56 check.Equal(t, []int{5, 10, 10, 15}, values) 57 58 values = nil 59 rbt.ReverseTraverse(func(_, value int) bool { 60 values = append(values, value) 61 return true 62 }) 63 check.Equal(t, 4, len(values)) 64 check.Equal(t, []int{15, 10, 10, 5}, values) 65 66 rbt.Remove(7) 67 check.Equal(t, 4, rbt.Count()) 68 69 rbt.Remove(10) 70 check.Equal(t, 3, rbt.Count()) 71 72 rbt.Remove(10) 73 check.Equal(t, 2, rbt.Count()) 74 75 values = nil 76 rbt.Traverse(func(_, value int) bool { 77 values = append(values, value) 78 return true 79 }) 80 check.Equal(t, 2, len(values)) 81 check.Equal(t, []int{5, 15}, values) 82 83 for i := -10; i < 21; i++ { 84 rbt.Insert(i, i) 85 } 86 check.Equal(t, 33, rbt.Count()) 87 88 result, ok = rbt.Get(-3) 89 check.True(t, ok) 90 check.Equal(t, -3, result) 91 92 _, ok = rbt.Get(-11) 93 check.False(t, ok) 94 95 values = nil 96 rbt.TraverseStartingAt(30, func(_, value int) bool { 97 values = append(values, value) 98 return true 99 }) 100 check.Equal(t, 0, len(values)) 101 102 values = nil 103 rbt.TraverseStartingAt(20, func(_, value int) bool { 104 values = append(values, value) 105 return true 106 }) 107 check.Equal(t, 1, len(values)) 108 check.Equal(t, []int{20}, values) 109 110 values = nil 111 rbt.TraverseStartingAt(18, func(_, value int) bool { 112 values = append(values, value) 113 return true 114 }) 115 check.Equal(t, 3, len(values)) 116 check.Equal(t, []int{18, 19, 20}, values) 117 118 values = nil 119 rbt.ReverseTraverseStartingAt(-20, func(_, value int) bool { 120 values = append(values, value) 121 return true 122 }) 123 check.Equal(t, 0, len(values)) 124 125 values = nil 126 rbt.ReverseTraverseStartingAt(-10, func(_, value int) bool { 127 values = append(values, value) 128 return true 129 }) 130 check.Equal(t, 1, len(values)) 131 check.Equal(t, []int{-10}, values) 132 133 values = nil 134 rbt.ReverseTraverseStartingAt(-8, func(_, value int) bool { 135 values = append(values, value) 136 return true 137 }) 138 check.Equal(t, 3, len(values)) 139 check.Equal(t, []int{-8, -9, -10}, values) 140 }