test: add unit test TestBSTree_IsSubTree

This commit is contained in:
dudaodong committed 2022-04-27 11:36:51 +08:00
1 parent 0d48778886
commit 61c7012e17
3 files changed
+14 -16

No files matched your search

+1 -3
View File
@@ -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)
}