Skip to content

Commit

Permalink
fix reroot bug caused by failure to return after adding new root to tree
Browse files Browse the repository at this point in the history
  • Loading branch information
mcphailtom committed Oct 10, 2023
1 parent a4b4e64 commit 90cd2b3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tree/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func (c *testnode) SetParent(parent Node[*testnode, int]) {
c.parent = parent
}

func (c *testnode) UpdateNode(node Node[*testnode, int]) {
}

type TreeTestSuite struct {
suite.Suite
}
Expand Down
2 changes: 2 additions & 0 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func (t *Tree[T, id]) Insert(node Node[T, id]) error {
if err != nil {
if t.root.GetParentID() == node.GetID() { // parent does not exist but incoming node is parent of root
t.reroot(node)
t.nodeIndex.insert(node)
return nil
} else { // parent does not exist, do not add
return ParentNotFound
}
Expand Down
71 changes: 71 additions & 0 deletions tree/treeops_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package tree

func (tts *TreeTestSuite) TestNoParent() {
st, err := NewTree[*testnode, int]()
tts.Require().NoError(err, "Failed to initialize tree for testing")

nodes := []Node[*testnode, int]{
&testnode{id: 0, parentID: 0, name: "node0"},
&testnode{id: 1, parentID: 0, name: "node1"},
&testnode{id: 2, parentID: 0, name: "node2"},
&testnode{id: 3, parentID: 1, name: "node3"},
&testnode{id: 4, parentID: 1, name: "node4"},
&testnode{id: 5, parentID: 4, name: "node5"},
&testnode{id: 6, parentID: 5, name: "node6"},
&testnode{id: 7, parentID: 2, name: "node7"},
&testnode{id: 8, parentID: 2, name: "node8"},
&testnode{id: 9, parentID: 3, name: "node9"},
&testnode{id: 10, parentID: 3, name: "node10"},
&testnode{id: 11, parentID: 10, name: "node11"},
&testnode{id: 13, parentID: 12, name: "node13"},
}

for _, node := range nodes {
err := st.Insert(node)
if node.GetID() == 13 {
tts.Require().Error(err, "Node without parent should trigger error")
continue
}
tts.Require().NoError(err, "Failed to insert node into tree")
}
}

func (tts *TreeTestSuite) TestReroot() {
st, err := NewTree[*testnode, int]()
tts.Require().NoError(err, "Failed to initialize tree for testing")

nodes := []Node[*testnode, int]{
&testnode{id: 0, parentID: 12, name: "node0"},
&testnode{id: 1, parentID: 0, name: "node1"},
&testnode{id: 2, parentID: 0, name: "node2"},
&testnode{id: 3, parentID: 1, name: "node3"},
&testnode{id: 4, parentID: 1, name: "node4"},
&testnode{id: 5, parentID: 4, name: "node5"},
&testnode{id: 6, parentID: 5, name: "node6"},
&testnode{id: 7, parentID: 2, name: "node7"},
&testnode{id: 8, parentID: 2, name: "node8"},
&testnode{id: 9, parentID: 3, name: "node9"},
&testnode{id: 10, parentID: 3, name: "node10"},
&testnode{id: 11, parentID: 10, name: "node11"},
&testnode{id: 12, parentID: 27, name: "node13"},
}

for _, node := range nodes {
err := st.Insert(node)
tts.Require().NoError(err, "Failed to insert node into tree")
}

// start from root node (id: 0) without limit depth
c, err := st.Traverse(TraverseBreadthFirst, st.Root().GetID(), 0)
tts.Require().NoError(err, "Failed to traverse tree")

// Grab the resulting BFS output
var results []int
for node := range c {
results = append(results, node.GetID())
}

// Expected result should include all nodes in the tree
bfsExpectedOrder := []int{12, 0, 1, 2, 3, 4, 7, 8, 9, 10, 5, 11, 6}
tts.Equal(bfsExpectedOrder, results, "BFS traversal did not match expected order")
}

0 comments on commit 90cd2b3

Please sign in to comment.