import{_ as l,o as p,c as o,k as s,a,X as n}from"./chunks/framework.6e839c56.js";const _=JSON.parse('{"title":"Tree","description":"","frontmatter":{},"headers":[],"relativePath":"api/packages/datastructure/tree.md","filePath":"api/packages/datastructure/tree.md"}'),e={name:"api/packages/datastructure/tree.md"},t=s("h1",{id:"Tree",tabindex:"-1"},[a("Tree "),s("a",{class:"header-anchor",href:"#Tree","aria-label":'Permalink to "Tree"'},"​")],-1),r=s("p",null,"树是树节点的集合。 每个树节点都有一个值,一个指向左节点的左指针和一个指向右节点的右指针。",-1),c=s("div",{STYLE:"page-break-after: always;"},null,-1),y=s("h2",{id:"源码",tabindex:"-1"},[a("源码 "),s("a",{class:"header-anchor",href:"#源码","aria-label":'Permalink to "源码"'},"​")],-1),F=s("ul",null,[s("li",null,[s("a",{href:"https://github.com/duke-git/lancet/blob/main/datastructure/tree/bstree.go",target:"_blank",rel:"noreferrer"},"https://github.com/duke-git/lancet/blob/main/datastructure/tree/bstree.go")])],-1),i=s("div",{STYLE:"page-break-after: always;"},null,-1),A=n(`

用法

go
import (
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
import (
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)
`,2),E=s("div",{STYLE:"page-break-after: always;"},null,-1),u=n('

目录

1. BSTree

',3),d=s("div",{STYLE:"page-break-after: always;"},null,-1),v=n(`

文档

1. BSTree

BSTree是一种二叉搜索树数据结构,其中每个节点有两个孩子,分别称为左孩子和右孩子。 在 BSTree 中:leftNode < rootNode < rightNode。 T类型应该实现lancetconstraints.Comparator。

NewBSTree

返回BSTree指针实例

函数签名:

go
func NewBSTree[T any](rootData T, comparator lancetconstraints.Comparator) *BSTree[T]

type BSTree[T any] struct {
	root       *datastructure.TreeNode[T]
	comparator lancetconstraints.Comparator
}

type TreeNode[T any] struct {
	Value T
	Left  *TreeNode[T]
	Right *TreeNode[T]
}
func NewBSTree[T any](rootData T, comparator lancetconstraints.Comparator) *BSTree[T]

type BSTree[T any] struct {
	root       *datastructure.TreeNode[T]
	comparator lancetconstraints.Comparator
}

type TreeNode[T any] struct {
	Value T
	Left  *TreeNode[T]
	Right *TreeNode[T]
}

示例:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    fmt.Println(bstree) //
}
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    fmt.Println(bstree) //
}

Insert

将值插入二叉搜索树

函数签名:

go
func (t *BSTree[T]) Insert(data T)
func (t *BSTree[T]) Insert(data T)

示例:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.PreOrderTraverse()) //6, 5, 2, 4, 7
}
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.PreOrderTraverse()) //6, 5, 2, 4, 7
}

Delete

删除插入二叉搜索树中指定的值

函数签名:

go
func (t *BSTree[T]) Delete(data T)
func (t *BSTree[T]) Delete(data T)

示例:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    bstree.Delete(4)

    fmt.Println(bstree.PreOrderTraverse()) //2, 5, 6, 7
}
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    bstree.Delete(4)

    fmt.Println(bstree.PreOrderTraverse()) //2, 5, 6, 7
}

PreOrderTraverse

按前序遍历树节点

函数签名:

go
func (t *BSTree[T]) PreOrderTraverse() []T
func (t *BSTree[T]) PreOrderTraverse() []T

示例:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.PreOrderTraverse()) //6, 5, 2, 4, 7
}
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.PreOrderTraverse()) //6, 5, 2, 4, 7
}

InOrderTraverse

按中序遍历树节点

函数签名:

go
func (t *BSTree[T]) InOrderTraverse() []T
func (t *BSTree[T]) InOrderTraverse() []T

示例:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.InOrderTraverse()) //2, 4, 5, 6, 7
}
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.InOrderTraverse()) //2, 4, 5, 6, 7
}

PostOrderTraverse

按后序遍历树节点

函数签名:

go
func (t *BSTree[T]) PostOrderTraverse() []T
func (t *BSTree[T]) PostOrderTraverse() []T

示例:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.PostOrderTraverse()) //5, 2, 4, 7, 6
}
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.PostOrderTraverse()) //5, 2, 4, 7, 6
}

LevelOrderTraverse

按节点层次遍历树节点

函数签名:

go
func (t *BSTree[T]) LevelOrderTraverse() []T
func (t *BSTree[T]) LevelOrderTraverse() []T

示例:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.LevelOrderTraverse()) //6, 5, 7, 2, 4
}
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.LevelOrderTraverse()) //6, 5, 7, 2, 4
}

Depth

获取树的深度

函数签名:

go
func (t *BSTree[T]) Depth() int
func (t *BSTree[T]) Depth() int

示例:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.Depth()) //4
}
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.Depth()) //4
}

HasSubTree

判断给定树是否是子树

函数签名:

go
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool
func (t *BSTree[T]) HasSubTree(subTree *BSTree[T]) bool

示例:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    superTree := tree.NewBSTree(8, &intComparator{})
	superTree.Insert(4)
	superTree.Insert(5)
	superTree.Insert(6)
	superTree.Insert(9)
	superTree.Insert(4)

    subTree := tree.NewBSTree(5, &intComparator{})
	subTree.Insert(4)
	subTree.Insert(6)

    fmt.Println(superTree.HasSubTree(subTree)) //true
    fmt.Println(subTree.HasSubTree(superTree)) //false
}
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    superTree := tree.NewBSTree(8, &intComparator{})
	superTree.Insert(4)
	superTree.Insert(5)
	superTree.Insert(6)
	superTree.Insert(9)
	superTree.Insert(4)

    subTree := tree.NewBSTree(5, &intComparator{})
	subTree.Insert(4)
	subTree.Insert(6)

    fmt.Println(superTree.HasSubTree(subTree)) //true
    fmt.Println(subTree.HasSubTree(superTree)) //false
}

Print

打印树结构

函数签名:

go
func (t *BSTree[T]) Print()
func (t *BSTree[T]) Print()

示例:

go
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.Print())
//        6
//       / \\
//      /   \\
//     /     \\
//    /       \\
//    5       7
//   /
//  /
//  2
//   \\
//    4
}
package main

import (
    "fmt"
    tree "github.com/duke-git/lancet/v2/datastructure/tree"
)

type intComparator struct{}

func (c *intComparator) Compare(v1, v2 any) int {
	val1, _ := v1.(int)
	val2, _ := v2.(int)

	if val1 < val2 {
		return -1
	} else if val1 > val2 {
		return 1
	}
	return 0
}

func main() {
    bstree := tree.NewBSTree(6, &intComparator{})
    bstree.Insert(7)
	bstree.Insert(5)
	bstree.Insert(2)
	bstree.Insert(4)

    fmt.Println(bstree.Print())
//        6
//       / \\
//      /   \\
//     /     \\
//    /       \\
//    5       7
//   /
//  /
//  2
//   \\
//    4
}
`,63),B=[t,r,c,y,F,i,A,E,u,d,v];function C(m,b,T,g,h,f){return p(),o("div",null,B)}const k=l(e,[["render",C]]);export{_ as __pageData,k as default};