| 34 | } |
| 35 | |
| 36 | func TestDelete(t *testing.T) { |
| 37 | t.Run("Delete a node with no child", func(t *testing.T) { |
| 38 | bst := bt.NewBinarySearch[int]() |
| 39 | |
| 40 | bst.Push(90) |
| 41 | bst.Push(80) |
| 42 | bst.Push(80) |
| 43 | bst.Push(100) |
| 44 | |
| 45 | if !bst.Delete(100) { |
| 46 | t.Errorf("There is a node, whose value is 100") |
| 47 | } |
| 48 | |
| 49 | if bst.Delete(105) { |
| 50 | t.Errorf("There is no node, whose value is 105") |
| 51 | } |
| 52 | |
| 53 | root := bst.Root |
| 54 | if root.Key() != 90 { |
| 55 | t.Errorf("Root should have value = 90") |
| 56 | } |
| 57 | |
| 58 | if root.Left().Key() != 80 { |
| 59 | t.Errorf("Left child should have value = 80") |
| 60 | } |
| 61 | |
| 62 | if root.Right().(*bt.BSNode[int]) != nil { |
| 63 | t.Errorf("Right child should have value = nil") |
| 64 | } |
| 65 | |
| 66 | if bst.Depth() != 2 { |
| 67 | t.Errorf("Depth should have value = 2") |
| 68 | } |
| 69 | |
| 70 | bst.Delete(80) |
| 71 | |
| 72 | if root.Key() != 90 { |
| 73 | t.Errorf("Root should have value = 90") |
| 74 | } |
| 75 | |
| 76 | if root.Left().(*bt.BSNode[int]) != nil { |
| 77 | t.Errorf("Left child should have value = nil") |
| 78 | } |
| 79 | |
| 80 | if bst.Depth() != 1 { |
| 81 | t.Errorf("Depth should have value = 1") |
| 82 | } |
| 83 | }) |
| 84 | |
| 85 | t.Run("Delete a node with one child", func(t *testing.T) { |
| 86 | bst := bt.NewBinarySearch[int]() |
| 87 | |
| 88 | bst.Push(90) |
| 89 | bst.Push(80) |
| 90 | bst.Push(100) |
| 91 | bst.Push(70) |
| 92 | |
| 93 | if bst.Delete(102) { |