From 61c7012e175a83ecde2945f1a7c576e60a47e291 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 27 Apr 2022 11:36:51 +0800 Subject: [PATCH] test: add unit test TestBSTree_IsSubTree --- datastructure/tree/bstree.go | 4 +--- datastructure/tree/bstree_test.go | 22 +++++++++++----------- datastructure/tree/tree_internal.go | 4 ++-- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/datastructure/tree/bstree.go b/datastructure/tree/bstree.go index f7b0866..81ccaa5 100644 --- a/datastructure/tree/bstree.go +++ b/datastructure/tree/bstree.go @@ -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) } diff --git a/datastructure/tree/bstree_test.go b/datastructure/tree/bstree_test.go index 8120f00..79212ed 100644 --- a/datastructure/tree/bstree_test.go +++ b/datastructure/tree/bstree_test.go @@ -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() -} \ No newline at end of file + assert.Equal(true, superTree.HasSubTree(subTree)) + assert.Equal(false, subTree.HasSubTree(superTree)) +} diff --git a/datastructure/tree/tree_internal.go b/datastructure/tree/tree_internal.go index c87d2ab..a0c61fe 100644 --- a/datastructure/tree/tree_internal.go +++ b/datastructure/tree/tree_internal.go @@ -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 {