Merge branch 'krahets:master' into master
This commit is contained in:
@@ -88,7 +88,25 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_search_tree.go"
|
||||
|
||||
/* 查找结点 */
|
||||
func (bst *BinarySearchTree) Search(num int) *TreeNode {
|
||||
node := bst.root
|
||||
// 循环查找,越过叶结点后跳出
|
||||
for node != nil {
|
||||
if node.Val < num {
|
||||
// 目标结点在 root 的右子树中
|
||||
node = node.Right
|
||||
} else if node.Val > num {
|
||||
// 目标结点在 root 的左子树中
|
||||
node = node.Left
|
||||
} else {
|
||||
// 找到目标结点,跳出循环
|
||||
break
|
||||
}
|
||||
}
|
||||
// 返回目标结点
|
||||
return node
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -187,7 +205,36 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_search_tree.go"
|
||||
|
||||
/* 插入结点 */
|
||||
func (bst *BinarySearchTree) Insert(num int) *TreeNode {
|
||||
cur := bst.root
|
||||
// 若树为空,直接提前返回
|
||||
if cur == nil {
|
||||
return nil
|
||||
}
|
||||
// 待插入结点之前的结点位置
|
||||
var prev *TreeNode = nil
|
||||
// 循环查找,越过叶结点后跳出
|
||||
for cur != nil {
|
||||
if cur.Val == num {
|
||||
return nil
|
||||
}
|
||||
prev = cur
|
||||
if cur.Val < num {
|
||||
cur = cur.Right
|
||||
} else {
|
||||
cur = cur.Left
|
||||
}
|
||||
}
|
||||
// 插入结点
|
||||
node := NewTreeNode(num)
|
||||
if prev.Val < num {
|
||||
prev.Right = node
|
||||
} else {
|
||||
prev.Left = node
|
||||
}
|
||||
return cur
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -365,7 +412,60 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_search_tree.go"
|
||||
|
||||
/* 删除结点 */
|
||||
func (bst *BinarySearchTree) Remove(num int) *TreeNode {
|
||||
cur := bst.root
|
||||
// 若树为空,直接提前返回
|
||||
if cur == nil {
|
||||
return nil
|
||||
}
|
||||
// 待删除结点之前的结点位置
|
||||
var prev *TreeNode = nil
|
||||
// 循环查找,越过叶结点后跳出
|
||||
for cur != nil {
|
||||
if cur.Val == num {
|
||||
break
|
||||
}
|
||||
prev = cur
|
||||
if cur.Val < num {
|
||||
// 待删除结点在右子树中
|
||||
cur = cur.Right
|
||||
} else {
|
||||
// 待删除结点在左子树中
|
||||
cur = cur.Left
|
||||
}
|
||||
}
|
||||
// 若无待删除结点,则直接返回
|
||||
if cur == nil {
|
||||
return nil
|
||||
}
|
||||
// 子结点数为 0 或 1
|
||||
if cur.Left == nil || cur.Right == nil {
|
||||
var child *TreeNode = nil
|
||||
// 取出待删除结点的子结点
|
||||
if cur.Left != nil {
|
||||
child = cur.Left
|
||||
} else {
|
||||
child = cur.Right
|
||||
}
|
||||
// 将子结点替换为待删除结点
|
||||
if prev.Left == cur {
|
||||
prev.Left = child
|
||||
} else {
|
||||
prev.Right = child
|
||||
}
|
||||
// 子结点数为 2
|
||||
} else {
|
||||
// 获取中序遍历中待删除结点 cur 的下一个结点
|
||||
next := bst.GetInorderNext(cur)
|
||||
temp := next.Val
|
||||
// 递归删除结点 next
|
||||
bst.Remove(next.Val)
|
||||
// 将 next 的值复制给 cur
|
||||
cur.Val = temp
|
||||
}
|
||||
return cur
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
@@ -44,7 +44,20 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title=""
|
||||
|
||||
""" 链表结点类 """
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
""" 结点初始化方法 """
|
||||
func NewTreeNode(v int) *TreeNode {
|
||||
return &TreeNode{
|
||||
Left: nil,
|
||||
Right: nil,
|
||||
Val: v,
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -163,7 +176,18 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_tree.go"
|
||||
|
||||
/* 初始化二叉树 */
|
||||
// 初始化结点
|
||||
n1 := NewTreeNode(1)
|
||||
n2 := NewTreeNode(2)
|
||||
n3 := NewTreeNode(3)
|
||||
n4 := NewTreeNode(4)
|
||||
n5 := NewTreeNode(5)
|
||||
// 构建引用指向(即指针)
|
||||
n1.Left = n2
|
||||
n1.Right = n3
|
||||
n2.Left = n4
|
||||
n2.Right = n5
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -228,7 +252,14 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_tree.go"
|
||||
/* 插入与删除结点 */
|
||||
// 在 n1 -> n2 中间插入结点 P
|
||||
p := NewTreeNode(0)
|
||||
n1.Left = p
|
||||
p.Left = n2
|
||||
|
||||
// 删除结点 P
|
||||
n1.Left = n2
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -328,7 +359,29 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_tree_bfs.go"
|
||||
|
||||
/* 层序遍历 */
|
||||
func levelOrder(root *TreeNode) []int {
|
||||
// 初始化队列,加入根结点
|
||||
queue := list.New()
|
||||
queue.PushBack(root)
|
||||
// 初始化一个切片,用于保存遍历序列
|
||||
nums := make([]int, 0)
|
||||
for queue.Len() > 0 {
|
||||
// poll
|
||||
node := queue.Remove(queue.Front()).(*TreeNode)
|
||||
// 保存结点
|
||||
nums = append(nums, node.Val)
|
||||
if node.Left != nil {
|
||||
// 左子结点入队
|
||||
queue.PushBack(node.Left)
|
||||
}
|
||||
if node.Right != nil {
|
||||
// 右子结点入队
|
||||
queue.PushBack(node.Right)
|
||||
}
|
||||
}
|
||||
return nums
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -446,7 +499,38 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_tree_dfs.go"
|
||||
/* 前序遍历 */
|
||||
func preOrder(node *TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
// 访问优先级:根结点 -> 左子树 -> 右子树
|
||||
nums = append(nums, node.Val)
|
||||
preOrder(node.Left)
|
||||
preOrder(node.Right)
|
||||
}
|
||||
|
||||
/* 中序遍历 */
|
||||
func inOrder(node *TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
// 访问优先级:左子树 -> 根结点 -> 右子树
|
||||
inOrder(node.Left)
|
||||
nums = append(nums, node.Val)
|
||||
inOrder(node.Right)
|
||||
}
|
||||
|
||||
/* 后序遍历 */
|
||||
func postOrder(node *TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
// 访问优先级:左子树 -> 右子树 -> 根结点
|
||||
postOrder(node.Left)
|
||||
postOrder(node.Right)
|
||||
nums = append(nums, node.Val)
|
||||
}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
Reference in New Issue
Block a user