MCPcopy
hub / github.com/TheAlgorithms/Go / TestDelete

Function TestDelete

structure/tree/bstree_test.go:36–192  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

34}
35
36func 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) {

Callers

nothing calls this directly

Calls 11

PushMethod · 0.65
DeleteMethod · 0.65
KeyMethod · 0.65
LeftMethod · 0.65
RightMethod · 0.65
DepthMethod · 0.65
InOrderMethod · 0.65
MinMethod · 0.65
MaxMethod · 0.65
SuccessorMethod · 0.65
PredecessorMethod · 0.65

Tested by

no test coverage detected