mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-15 18:22:27 +08:00
feat: add LevelOrderTraverse func
This commit is contained in:
@@ -63,6 +63,13 @@ func (t *BSTree[T]) InOrderTraverse() []T {
|
|||||||
return inOrderTraverse(t.root)
|
return inOrderTraverse(t.root)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LevelOrderTraverse traverse tree node in level order
|
||||||
|
func (t *BSTree[T]) LevelOrderTraverse() []T {
|
||||||
|
traversal := make([]T, 0)
|
||||||
|
levelOrderTraverse(t.root, &traversal)
|
||||||
|
return traversal
|
||||||
|
}
|
||||||
|
|
||||||
// Depth returns the calculated depth of a binary saerch tree
|
// Depth returns the calculated depth of a binary saerch tree
|
||||||
func (t *BSTree[T]) Depth() int {
|
func (t *BSTree[T]) Depth() int {
|
||||||
return calculateDepth(t.root, 0)
|
return calculateDepth(t.root, 0)
|
||||||
|
|||||||
@@ -80,6 +80,24 @@ func TestBSTree_InOrderTraverse(t *testing.T) {
|
|||||||
assert.Equal([]int{2, 4, 5, 6, 7}, acturl)
|
assert.Equal([]int{2, 4, 5, 6, 7}, acturl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBSTree_LevelOrderTraverse(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestBSTree_LevelOrderTraverse")
|
||||||
|
|
||||||
|
bstree := NewBSTree(6)
|
||||||
|
|
||||||
|
comparator := &intComparator{}
|
||||||
|
bstree.InsertNode(7, comparator)
|
||||||
|
bstree.InsertNode(5, comparator)
|
||||||
|
bstree.InsertNode(2, comparator)
|
||||||
|
bstree.InsertNode(4, comparator)
|
||||||
|
|
||||||
|
bstree.Print()
|
||||||
|
|
||||||
|
acturl := bstree.LevelOrderTraverse()
|
||||||
|
t.Log(acturl)
|
||||||
|
assert.Equal([]int{6, 5, 7, 2, 4}, acturl)
|
||||||
|
}
|
||||||
|
|
||||||
func TestBSTree_DeletetNode(t *testing.T) {
|
func TestBSTree_DeletetNode(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestBSTree_DeletetNode")
|
assert := internal.NewAssert(t, "TestBSTree_DeletetNode")
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,24 @@ func inOrderPrint[T any](node *datastructure.TreeNode[T]) {
|
|||||||
inOrderPrint(node.Right)
|
inOrderPrint(node.Right)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func levelOrderTraverse[T any](root *datastructure.TreeNode[T], traversal *[]T) {
|
||||||
|
var q []*datastructure.TreeNode[T] // queue
|
||||||
|
var n *datastructure.TreeNode[T] // temp node
|
||||||
|
|
||||||
|
q = append(q, root)
|
||||||
|
|
||||||
|
for len(q) != 0 {
|
||||||
|
n, q = q[0], q[1:]
|
||||||
|
*traversal = append(*traversal, n.Data)
|
||||||
|
if n.Left != nil {
|
||||||
|
q = append(q, n.Left)
|
||||||
|
}
|
||||||
|
if n.Right != nil {
|
||||||
|
q = append(q, n.Right)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func insertTreeNode[T any](rootNode, newNode *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) {
|
func insertTreeNode[T any](rootNode, newNode *datastructure.TreeNode[T], comparator lancetconstraints.Comparator) {
|
||||||
if comparator.Compare(newNode.Data, rootNode.Data) == -1 {
|
if comparator.Compare(newNode.Data, rootNode.Data) == -1 {
|
||||||
if rootNode.Left == nil {
|
if rootNode.Left == nil {
|
||||||
@@ -199,5 +217,8 @@ func calculateDepth[T any](node *datastructure.TreeNode[T], depth int) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func max(a, b int) int {
|
func max(a, b int) int {
|
||||||
return int(math.Max(float64(a), float64(b)))
|
if a > b {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user