mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-06 21:52:28 +08:00
test: add unit test TestBSTree_IsSubTree
This commit is contained in:
@@ -77,7 +77,7 @@ func (t *BSTree[T]) Depth() int {
|
||||
return calculateDepth(t.root, 0)
|
||||
}
|
||||
|
||||
// IsSubTree checks if the tree `t`` has `subTree` or not
|
||||
// IsSubTree checks if the tree `t` has `subTree` or not
|
||||
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool {
|
||||
return hasSubTree(t.root, subTree.root, t.comparator)
|
||||
}
|
||||
@@ -90,11 +90,9 @@ func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T],
|
||||
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 {
|
||||
res = isSubTree(superTreeRoot, subTreeRoot, comparator)
|
||||
}
|
||||
|
||||
if !res {
|
||||
res = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator)
|
||||
}
|
||||
|
||||
if !res {
|
||||
res = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator)
|
||||
}
|
||||
|
||||
@@ -134,24 +134,24 @@ func TestBSTree_Depth(t *testing.T) {
|
||||
assert.Equal(bstree.Depth(), 4)
|
||||
}
|
||||
|
||||
|
||||
func TestBSTree_IsSubTree(t *testing.T) {
|
||||
assert := internal.NewAssert(t, "TestBSTree_IsSubTree")
|
||||
|
||||
superTree := NewBSTree(6, &intComparator{})
|
||||
superTree.InsertNode(7)
|
||||
superTree := NewBSTree(8, &intComparator{})
|
||||
superTree.InsertNode(4)
|
||||
superTree.InsertNode(5)
|
||||
superTree.InsertNode(2)
|
||||
superTree.InsertNode(6)
|
||||
superTree.InsertNode(9)
|
||||
superTree.InsertNode(4)
|
||||
|
||||
superTree.Print()
|
||||
|
||||
subTree1 := NewBSTree(3, &intComparator{})
|
||||
subTree1.InsertNode(5)
|
||||
subTree1.InsertNode(2)
|
||||
subTree1.InsertNode(4)
|
||||
subTree := NewBSTree(5, &intComparator{})
|
||||
subTree.InsertNode(4)
|
||||
subTree.InsertNode(6)
|
||||
|
||||
assert.Equal(true, superTree)
|
||||
subTree1.Print()
|
||||
subTree.Print()
|
||||
|
||||
}
|
||||
assert.Equal(true, superTree.HasSubTree(subTree))
|
||||
assert.Equal(false, subTree.HasSubTree(superTree))
|
||||
}
|
||||
|
||||
@@ -226,8 +226,8 @@ func isSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], com
|
||||
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) != 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && hasSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator)
|
||||
res := isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && isSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator)
|
||||
return res
|
||||
}
|
||||
|
||||
func max(a, b int) int {
|
||||
|
||||
Reference in New Issue
Block a user