mirror of
https://github.com/duke-git/lancet.git
synced 2026-03-01 00:35:28 +08:00
feat: add HasSubTree func for BSTree
This commit is contained in:
@@ -78,25 +78,25 @@ func (t *BSTree[T]) Depth() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsSubTree checks if the tree `t`` has `subTree` or not
|
// IsSubTree checks if the tree `t`` has `subTree` or not
|
||||||
func (t *BSTree[T]) IsSubTree(subTree *BSTree[T]) bool {
|
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool {
|
||||||
return isSubTree(t.root, subTree.root, t.comparator)
|
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 {
|
comparator lancetconstraints.Comparator) bool {
|
||||||
res := false
|
res := false
|
||||||
|
|
||||||
if superTreeRoot != nil && subTreeRoot != nil {
|
if superTreeRoot != nil && subTreeRoot != nil {
|
||||||
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 {
|
if comparator.Compare(superTreeRoot.Value, subTreeRoot.Value) == 0 {
|
||||||
res = hasSubTree(superTreeRoot, subTreeRoot, comparator)
|
res = isSubTree(superTreeRoot, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !res {
|
if !res {
|
||||||
res = isSubTree(superTreeRoot.Left, subTreeRoot, comparator)
|
res = hasSubTree(superTreeRoot.Left, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !res {
|
if !res {
|
||||||
res = isSubTree(superTreeRoot.Right, subTreeRoot, comparator)
|
res = hasSubTree(superTreeRoot.Right, subTreeRoot, comparator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
|
|||||||
@@ -133,3 +133,25 @@ func TestBSTree_Depth(t *testing.T) {
|
|||||||
|
|
||||||
assert.Equal(bstree.Depth(), 4)
|
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()
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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))
|
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 {
|
if subTreeRoot == nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -227,7 +227,7 @@ func hasSubTree[T any](superTreeRoot, subTreeRoot *datastructure.TreeNode[T], co
|
|||||||
return false
|
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 {
|
func max(a, b int) int {
|
||||||
|
|||||||
Reference in New Issue
Block a user