Skip to content

Commit

Permalink
change trees and list data to int
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Tudose committed Aug 15, 2020
1 parent 2e11b8b commit b41063f
Show file tree
Hide file tree
Showing 24 changed files with 34 additions and 34 deletions.
2 changes: 1 addition & 1 deletion epi/bst_to_sorted_list/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func bstToDoublyLinkedListWrapper(t *tree.BSTNode) ([]int, error) {

v := make([]int, 0)
for list != nil {
v = append(v, list.Data.(int))
v = append(v, list.Data)
if list.Right != nil && list.Right.Left != list {
return nil, errors.New("list is ill-formed")
}
Expand Down
6 changes: 3 additions & 3 deletions epi/do_lists_overlap/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ func overlappingListsWrapper(l0 *list.Node, l1 *list.Node, common *list.Node, cy

commonNodes := make(map[int]bool)
for it := common; it != nil; it = it.Next {
if _, ok := commonNodes[it.Data.(int)]; ok {
if _, ok := commonNodes[it.Data]; ok {
break
}

commonNodes[it.Data.(int)] = true
commonNodes[it.Data] = true
}

result := OverlappingLists(l0, l1)
Expand All @@ -137,7 +137,7 @@ func overlappingListsWrapper(l0 *list.Node, l1 *list.Node, common *list.Node, cy
return errors.New("invalid result")
}

_, ok := commonNodes[result.Data.(int)]
_, ok := commonNodes[result.Data]
if !ok {
return errors.New("invalid result")
}
Expand Down
4 changes: 2 additions & 2 deletions epi/is_list_cyclic/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func hasCycleWrapper(head *list.Node, cycleIdx int) error {
cursor := head

for cursor.Next != nil {
if cursor.Data.(int) == cycleIdx {
if cursor.Data == cycleIdx {
cycleStart = cursor
}
cursor = cursor.Next
Expand All @@ -84,7 +84,7 @@ func hasCycleWrapper(head *list.Node, cycleIdx int) error {

if cycleIdx == -1 {
if result != nil {
return fmt.Errorf("expected no cycle, got %d", result.Data.(int))
return fmt.Errorf("expected no cycle, got %d", result.Data)
}
} else {
if result == nil {
Expand Down
2 changes: 1 addition & 1 deletion epi/kth_node_in_tree/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func convertToTreeWithSize(original *tree.BinaryTree) *BinaryTreeNode {
}

return &BinaryTreeNode{
Data: original.Data.(int),
Data: original.Data,
Left: left,
Right: right,
Size: 1 + lSize + rSize,
Expand Down
2 changes: 1 addition & 1 deletion epi/lowest_common_ancestor/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ func lcaWrapper(inputTree *tree.BinaryTreeNode, key0 int, key1 int) (int, error)
return 0, errors.New("result can not be nil")
}

return result.Data.(int), nil
return result.Data, nil
}
2 changes: 1 addition & 1 deletion epi/lowest_common_ancestor_close_ancestor/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ func lcaCloseWrapper(inputTree *tree.BinaryTree, key0 int, key1 int) (int, error
return 0, errors.New("result can not be nil")
}

return result.Data.(int), nil
return result.Data, nil
}
2 changes: 1 addition & 1 deletion epi/lowest_common_ancestor_in_bst/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ func findLCAWrapper(inputTree *tree.BSTNode, key0 int, key1 int) (int, error) {
return 0, errors.New("result can not be nil")
}

return result.Data.(int), nil
return result.Data, nil
}
2 changes: 1 addition & 1 deletion epi/lowest_common_ancestor_with_parent/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ func lcaWrapper(inputTree *tree.BinaryTree, key0 int, key1 int) (int, error) {
return 0, errors.New("result can not be nil")
}

return result.Data.(int), nil
return result.Data, nil
}
2 changes: 1 addition & 1 deletion epi/search_first_greater_value_in_bst/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestFindFirstGreaterThanK(t *testing.T) {

func findFirstGreaterThanKWrapper(tree *tree.BSTNode, k int) (int, error) {
if result := FindFirstGreaterThanK(tree, k); result != nil {
return result.Data.(int), nil
return result.Data, nil
}
return -1, nil
}
2 changes: 1 addition & 1 deletion epi/search_in_bst/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestSearchBST(t *testing.T) {

func searchBSTWrapper(tree *tree.BSTNode, key int) int {
if result := SearchBST(tree, key); result != nil {
return result.Data.(int)
return result.Data
}
return -1
}
2 changes: 1 addition & 1 deletion epi/search_in_list/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestSearchList(t *testing.T) {

func searchListWrapper(l *list.Node, key int) int {
if result := SearchList(l, key); result != nil {
return result.Data.(int)
return result.Data
}

return -1
Expand Down
2 changes: 1 addition & 1 deletion epi/sorted_list_to_bst/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func compareIterAndTree(tree *list.DoublyLinkedNode, it *iterator.Iterator) erro
next := it.Next()

if next != tree.Data {
return fmt.Errorf("expected value %d, got %d", next.(int), tree.Data.(int))
return fmt.Errorf("expected value %d, got %d", next.(int), tree.Data)
}

if err := compareIterAndTree(tree.Next, it); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion epi/successor_in_tree/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ func findSuccessorWrapper(inputTree *tree.BinaryTree, nodeIdx int) int {
return -1
}

return result.Data.(int)
return result.Data
}
4 changes: 2 additions & 2 deletions epi/tree_connect_leaves/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ func createListOfLeavesWrapper(tree *tree.BinaryTreeNode) ([]int, error) {
result := CreateListOfLeaves(tree)

for i, n := range result {
if n == nil || n.Data == nil {
if n == nil {
return nil, fmt.Errorf("result contains a nil node at index %d", i)
}
}

extractedRes := make([]int, len(result))
for i, n := range result {
extractedRes[i] = n.Data.(int)
extractedRes[i] = n.Data
}

return extractedRes, nil
Expand Down
2 changes: 1 addition & 1 deletion epi/tree_exterior/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func createOutputList(l []*tree.BinaryTreeNode) ([]int, error) {
if t == nil {
return nil, errors.New("result list contains nil")
}
output = append(output, t.Data.(int))
output = append(output, t.Data)
}

return output, nil
Expand Down
2 changes: 1 addition & 1 deletion epi/tree_right_sibling/binary_tree_node_with_next.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tree_right_sibling

type BinaryTreeNodeWithNext struct {
Data interface{}
Data int
Left, Right *BinaryTreeNodeWithNext
Next *BinaryTreeNodeWithNext
}
2 changes: 1 addition & 1 deletion epi/tree_right_sibling/solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func constructRightSiblingWrapper(tree *BinaryTreeNodeWithNext) [][]int {
level := make([]int, 0)
levelIter := levelStart
for levelIter != nil {
level = append(level, levelIter.Data.(int))
level = append(level, levelIter.Data)
levelIter = levelIter.Next
}
result = append(result, level)
Expand Down
2 changes: 1 addition & 1 deletion list/doubly_linked_node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package list

type DoublyLinkedNode struct {
Data interface{}
Data int
Prev, Next *DoublyLinkedNode
}
2 changes: 1 addition & 1 deletion list/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type Node struct {
Data interface{}
Data int
Next *Node
}

Expand Down
4 changes: 2 additions & 2 deletions list/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func ToArray(l *Node) []int {
panic("cycle detected")
}
seenNodes[cursor] = true
result = append(result, cursor.Data.(int))
result = append(result, cursor.Data)
}
return result
}
Expand Down Expand Up @@ -124,7 +124,7 @@ func DoublyLinkedNodeToSlice(l *DoublyLinkedNode) []int {
panic("cycle detected")
}
seenNodes[cursor] = true
result = append(result, cursor.Data.(int))
result = append(result, cursor.Data)
}
return result
}
4 changes: 2 additions & 2 deletions tree/binary_tree.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tree

type BinaryTree struct {
Data interface{}
Data int
Left, Right, Parent *BinaryTree
}

Expand All @@ -13,7 +13,7 @@ func (b *BinaryTree) String() string {
return s
}

func (b BinaryTree) GetData() interface{} {
func (b BinaryTree) GetData() int {
return b.Data
}

Expand Down
4 changes: 2 additions & 2 deletions tree/binary_tree_node.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tree

type BinaryTreeNode struct {
Data interface{}
Data int
Left, Right *BinaryTreeNode
}

Expand All @@ -13,7 +13,7 @@ func (b *BinaryTreeNode) String() string {
return s
}

func (b BinaryTreeNode) GetData() interface{} {
func (b BinaryTreeNode) GetData() int {
return b.Data
}

Expand Down
4 changes: 2 additions & 2 deletions tree/bst_node.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tree

type BSTNode struct {
Data interface{}
Data int
Left, Right *BSTNode
}

Expand All @@ -13,7 +13,7 @@ func (b *BSTNode) String() string {
return s
}

func (b BSTNode) GetData() interface{} {
func (b BSTNode) GetData() int {
return b.Data
}

Expand Down
6 changes: 3 additions & 3 deletions tree/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type TreeLike interface {
GetData() interface{}
GetData() int
GetLeft() TreeLike
GetRight() TreeLike
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func GenerateInorder(tree TreeLike) []int {
if initial {
initial = false
} else {
result = append(result, treeNode.GetData().(int))
result = append(result, treeNode.GetData())
treeNode = treeNode.GetRight()
}

Expand Down Expand Up @@ -204,7 +204,7 @@ func AssertTreeIsBST(tree TreeLike) error {
continue
}

value := node.Tree.GetData().(int)
value := node.Tree.GetData()

if !node.Range.contains(value) {
return fmt.Errorf(
Expand Down

0 comments on commit b41063f

Please sign in to comment.