From 0d48778886b84dce7e77344820eb901ea45731c2 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 27 Apr 2022 10:43:01 +0800 Subject: [PATCH] feat: add HasSubTree func for BSTree --- datastructure/tree/bstree.go | 12 ++++++------ datastructure/tree/bstree_test.go | 22 ++++++++++++++++++++++ datastructure/tree/tree_internal.go | 4 ++-- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/datastructure/tree/bstree.go b/datastructure/tree/bstree.go index e098a1d..f7b0866 100644 --- a/datastructure/tree/bstree.go +++ b/datastructure/tree/bstree.go @@ -78,25 +78,25 @@ func (t *BSTree[T]) Depth() int { } // IsSubTree checks if the tree `t`` has `subTree` or not -func (t *BSTree[T]) IsSubTree(subTree *BSTree[T]) bool { - return isSubTree(t.root, subTree.root, t.comparator) +func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool { + return hasSubTree(t.root, subTree.root, t.comparator) } -func isSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], +func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) bool { res := false if superTreeRoot != nil && subTreeRoot != nil { if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 { - res = hasSubTree(superTreeRoot, subTreeRoot, comparator) + res = isSubTree(superTreeRoot, subTreeRoot, comparator) } if !res { - res = isSubTree(superTreeRoot.Left, subTreeRoot, comparator) + res = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator) } if !res { - res = isSubTree(superTreeRoot.Right, subTreeRoot, comparator) + res = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator) } } return res diff --git a/datastructure/tree/bstree_test.go b/datastructure/tree/bstree_test.go index 07da79e..8120f00 100644 --- a/datastructure/tree/bstree_test.go +++ b/datastructure/tree/bstree_test.go @@ -133,3 +133,25 @@ 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.InsertNode(5) + superTree.InsertNode(2) + superTree.InsertNode(4) + + superTree.Print() + + subTree1 := NewBSTree(3, &intComparator{}) + subTree1.InsertNode(5) + subTree1.InsertNode(2) + subTree1.InsertNode(4) + + assert.Equal(true, superTree) + subTree1.Print() + +} \ No newline at end of file diff --git a/datastructure/tree/tree_internal.go b/datastructure/tree/tree_internal.go index 78802a7..c87d2ab 100644 --- a/datastructure/tree/tree_internal.go +++ b/datastructure/tree/tree_internal.go @@ -216,7 +216,7 @@ func calculateDepth[T any](node *datastructure.TreeNode[T], depth int) int { return max(calculateDepth(node.Left, depth+1), calculateDepth(node.Right, depth+1)) } -func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) bool { +func isSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) bool { if subTreeRoot == nil { return true } @@ -227,7 +227,7 @@ func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], co return false } - return hasSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && hasSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator) + return isSubTree(superTreeRoot.Left, subTreeRoot.Left, comparator) && hasSubTree(superTreeRoot.Right, subTreeRoot.Right, comparator) } func max(a, b int) int {