Skip to content

Commit

Permalink
fix: rb string
Browse files Browse the repository at this point in the history
  • Loading branch information
raffOps committed Nov 20, 2023
1 parent a4d5570 commit e3bfe50
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/redblacktree/redblacktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (tree *RedBlackTree[T]) String() string {
}

func stringHelper[T cmp.Ordered](tree *RedBlackTree[T], level int) string {
if tree == nil {
if tree.IsEmpty() {
return ""
}

Expand Down
19 changes: 14 additions & 5 deletions cmd/redblacktree/redblacktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,26 @@ func getStringTestCases() []stringTestCase {

// test case 1: root without parent

node := &RedBlackTree[int]{value: &nodeValue[int]{1}}
node := &RedBlackTree[int]{
value: &nodeValue[int]{1},
leftNode: NewRedBlackTree[int](),
rightNode: NewRedBlackTree[int](),
}
want := "\nvalue: 1, \ncolor: black, \nleftNode: , \nrightNode: "
testCases = append(testCases, stringTestCase{tree: node, want: want})

// test case 2: root with parent

node0 := &RedBlackTree[int]{
value: &nodeValue[int]{1},
value: &nodeValue[int]{1},
leftNode: NewRedBlackTree[int](),
rightNode: NewRedBlackTree[int](),
}

node2 := &RedBlackTree[int]{
value: &nodeValue[int]{3},
value: &nodeValue[int]{3},
leftNode: NewRedBlackTree[int](),
rightNode: NewRedBlackTree[int](),
}

node1 := &RedBlackTree[int]{
Expand All @@ -137,8 +145,9 @@ func getStringTestCases() []stringTestCase {
}

node3 := &RedBlackTree[int]{
value: &nodeValue[int]{4},
leftNode: node1,
value: &nodeValue[int]{4},
leftNode: node1,
rightNode: NewRedBlackTree[int](),
}

want = "\nvalue: 4, \ncolor: black, \nleftNode: \n\tvalue: 2, \n\tcolor: black, \n\tleftNode: \n\t\tvalue: 1, \n\t\tcolor: black, \n\t\tleftNode: , \n\t\trightNode: , \n\trightNode: \n\t\tvalue: 3, \n\t\tcolor: black, \n\t\tleftNode: , \n\t\trightNode: , \nrightNode: "
Expand Down

0 comments on commit e3bfe50

Please sign in to comment.