@@ -856,3 +856,123 @@ func TestTree_Contains(t *testing.T) {
856
856
assert .False (t , treeA .Contains (nB ), "node from tree B should not exist in node A" )
857
857
assert .True (t , treeB .Contains (nB ), "expected to find node B in tree B" )
858
858
}
859
+
860
+ func TestTree_Floor (t * testing.T ) {
861
+ tree := New [int , string , struct {}](func (a , b int ) bool {
862
+ return a < b
863
+ })
864
+
865
+ // Test with empty tree
866
+ n , found := tree .Floor (5 )
867
+ assert .False (t , found , "Floor in empty tree should return not found" )
868
+ assert .True (t , tree .IsNil (n ), "Floor in empty tree should return nil node" )
869
+
870
+ // Insert some values
871
+ tree .Insert (10 , "ten" )
872
+ tree .Insert (5 , "five" )
873
+ tree .Insert (15 , "fifteen" )
874
+ tree .Insert (3 , "three" )
875
+ tree .Insert (7 , "seven" )
876
+ tree .Insert (12 , "twelve" )
877
+ tree .Insert (17 , "seventeen" )
878
+
879
+ // Test exact matches
880
+ n , found = tree .Floor (10 )
881
+ assert .True (t , found , "Floor for existing key should be found" )
882
+ assert .Equal (t , 10 , tree .Key (n ), "Floor(10) should return node with key 10" )
883
+ assert .Equal (t , "ten" , tree .Value (n ), "Floor(10) should return node with value 'ten'" )
884
+
885
+ // Test where floor is less than key
886
+ n , found = tree .Floor (6 )
887
+ assert .True (t , found , "Floor(6) should find a node" )
888
+ assert .Equal (t , 5 , tree .Key (n ), "Floor(6) should return node with key 5" )
889
+ assert .Equal (t , "five" , tree .Value (n ), "Floor(6) should return node with value 'five'" )
890
+
891
+ // Test where floor is less than smallest key
892
+ n , found = tree .Floor (2 )
893
+ assert .False (t , found , "Floor(2) should not find a node" )
894
+ assert .True (t , tree .IsNil (n ), "Floor(2) should return nil node" )
895
+
896
+ // Test where floor is largest key
897
+ n , found = tree .Floor (20 )
898
+ assert .True (t , found , "Floor(20) should find a node" )
899
+ assert .Equal (t , 17 , tree .Key (n ), "Floor(20) should return node with key 17" )
900
+ assert .Equal (t , "seventeen" , tree .Value (n ), "Floor(20) should return node with value 'seventeen'" )
901
+ }
902
+
903
+ func TestTree_Ceiling (t * testing.T ) {
904
+ tree := New [int , string , struct {}](func (a , b int ) bool {
905
+ return a < b
906
+ })
907
+
908
+ // Test with empty tree
909
+ n , found := tree .Ceiling (5 )
910
+ assert .False (t , found , "Ceiling in empty tree should return not found" )
911
+ assert .True (t , tree .IsNil (n ), "Ceiling in empty tree should return nil node" )
912
+
913
+ // Insert some values
914
+ tree .Insert (10 , "ten" )
915
+ tree .Insert (5 , "five" )
916
+ tree .Insert (15 , "fifteen" )
917
+ tree .Insert (3 , "three" )
918
+ tree .Insert (7 , "seven" )
919
+ tree .Insert (12 , "twelve" )
920
+ tree .Insert (17 , "seventeen" )
921
+
922
+ // Test exact matches
923
+ n , found = tree .Ceiling (10 )
924
+ assert .True (t , found , "Ceiling for existing key should be found" )
925
+ assert .Equal (t , 10 , tree .Key (n ), "Ceiling(10) should return node with key 10" )
926
+ assert .Equal (t , "ten" , tree .Value (n ), "Ceiling(10) should return node with value 'ten'" )
927
+
928
+ // Test where ceiling is greater than key
929
+ n , found = tree .Ceiling (6 )
930
+ assert .True (t , found , "Ceiling(6) should find a node" )
931
+ assert .Equal (t , 7 , tree .Key (n ), "Ceiling(6) should return node with key 7" )
932
+ assert .Equal (t , "seven" , tree .Value (n ), "Ceiling(6) should return node with value 'seven'" )
933
+
934
+ // Test where ceiling is smallest key
935
+ n , found = tree .Ceiling (1 )
936
+ assert .True (t , found , "Ceiling(1) should find a node" )
937
+ assert .Equal (t , 3 , tree .Key (n ), "Ceiling(1) should return node with key 3" )
938
+ assert .Equal (t , "three" , tree .Value (n ), "Ceiling(1) should return node with value 'three'" )
939
+
940
+ // Test where key is larger than largest in tree
941
+ n , found = tree .Ceiling (20 )
942
+ assert .False (t , found , "Ceiling(20) should not find a node" )
943
+ assert .True (t , tree .IsNil (n ), "Ceiling(20) should return nil node" )
944
+ }
945
+
946
+ // TestTree_UncoveredSetMethods tests the uncovered set methods
947
+ func TestTree_UncoveredSetMethods (t * testing.T ) {
948
+ tree := New [int , string , struct {}](func (a , b int ) bool {
949
+ return a < b
950
+ })
951
+
952
+ // Create a test node
953
+ node , _ := tree .Insert (10 , "original value" )
954
+
955
+ // Test SetValue
956
+ tree .SetValue (node , "new value" )
957
+ assert .Equal (t , "new value" , tree .Value (node ), "SetValue should update the node's value" )
958
+
959
+ // Test SetLeft and SetRight
960
+ leftNode , _ := tree .Insert (5 , "left" )
961
+ rightNode , _ := tree .Insert (15 , "right" )
962
+
963
+ // Save original relationships
964
+ originalLeft := node .left
965
+ originalRight := node .right
966
+
967
+ // Manually change the relationships with SetLeft and SetRight
968
+ tree .SetLeft (node , rightNode ) // Intentionally incorrect for testing
969
+ tree .SetRight (node , leftNode ) // Intentionally incorrect for testing
970
+
971
+ // Verify the changes took effect
972
+ assert .Equal (t , rightNode , tree .Left (node ), "SetLeft should update the node's left child" )
973
+ assert .Equal (t , leftNode , tree .Right (node ), "SetRight should update the node's right child" )
974
+
975
+ // Restore original structure to avoid affecting other tests
976
+ tree .SetLeft (node , originalLeft )
977
+ tree .SetRight (node , originalRight )
978
+ }
0 commit comments