| 10 | ) |
| 11 | |
| 12 | func TestTreeGetOrHas(t *testing.T) { |
| 13 | helper := func(tree TestTree[int], nums []int) { |
| 14 | tree.Push(nums...) |
| 15 | for _, num := range nums { |
| 16 | if !tree.Has(num) { |
| 17 | t.Errorf("Error with Has or Push method") |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | min, _ := tree.Min() |
| 22 | max, _ := tree.Max() |
| 23 | |
| 24 | if _, ok := tree.Get(min - 1); ok { |
| 25 | t.Errorf("Error with Get method") |
| 26 | } |
| 27 | |
| 28 | if _, ok := tree.Get(max + 1); ok { |
| 29 | t.Errorf("Error with Get method") |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | lens := []int{100, 1_000, 10_000, 100_000} |
| 34 | for _, ll := range lens { |
| 35 | nums := rand.Perm(ll) |
| 36 | t.Run("Test Binary Search Tree", func(t *testing.T) { |
| 37 | bsTree := bt.NewBinarySearch[int]() |
| 38 | helper(bsTree, nums) |
| 39 | }) |
| 40 | |
| 41 | t.Run("Test Red-Black Tree", func(t *testing.T) { |
| 42 | rbTree := bt.NewRB[int]() |
| 43 | helper(rbTree, nums) |
| 44 | }) |
| 45 | |
| 46 | t.Run("Test AVL Tree", func(t *testing.T) { |
| 47 | avlTree := bt.NewAVL[int]() |
| 48 | helper(avlTree, nums) |
| 49 | }) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | func TestTreePreOrder(t *testing.T) { |
| 54 | t.Run("Test for Binary-Search Tree", func(t *testing.T) { |