mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-17 11:12: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)
|
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 {
|
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool {
|
||||||
return hasSubTree(t.root, subTree.root, t.comparator)
|
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 {
|
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 {
|
||||||
res = isSubTree(superTreeRoot, subTreeRoot, comparator)
|
res = isSubTree(superTreeRoot, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !res {
|
if !res {
|
||||||
res = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator)
|
res = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !res {
|
if !res {
|
||||||
res = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator)
|
res = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,24 +134,24 @@ func TestBSTree_Depth(t *testing.T) {
|
|||||||
assert.Equal(bstree.Depth(), 4)
|
assert.Equal(bstree.Depth(), 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func TestBSTree_IsSubTree(t *testing.T) {
|
func TestBSTree_IsSubTree(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestBSTree_IsSubTree")
|
assert := internal.NewAssert(t, "TestBSTree_IsSubTree")
|
||||||
|
|
||||||
superTree := NewBSTree(6, &intComparator{})
|
superTree := NewBSTree(8, &intComparator{})
|
||||||
superTree.InsertNode(7)
|
superTree.InsertNode(4)
|
||||||
superTree.InsertNode(5)
|
superTree.InsertNode(5)
|
||||||
superTree.InsertNode(2)
|
superTree.InsertNode(6)
|
||||||
|
superTree.InsertNode(9)
|
||||||
superTree.InsertNode(4)
|
superTree.InsertNode(4)
|
||||||
|
|
||||||
superTree.Print()
|
superTree.Print()
|
||||||
|
|
||||||
subTree1 := NewBSTree(3, &intComparator{})
|
subTree := NewBSTree(5, &intComparator{})
|
||||||
subTree1.InsertNode(5)
|
subTree.InsertNode(4)
|
||||||
subTree1.InsertNode(2)
|
subTree.InsertNode(6)
|
||||||
subTree1.InsertNode(4)
|
|
||||||
|
|
||||||
assert.Equal(true, superTree)
|
subTree.Print()
|
||||||
subTree1.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 {
|
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) != 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
res := isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && isSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator)
|
||||||
return isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && hasSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator)
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
func max(a, b int) int {
|
func max(a, b int) int {
|
||||||
|
|||||||
Reference in New Issue
Block a user