mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-16 03:59:18 +08:00
1. Add build script for Java.
2. Add height limitation for code blocks in extra.css. 3. Fix "节点" to "结点".
This commit is contained in:
@@ -28,7 +28,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
```java title=""
|
||||
/* AVL 树结点类 */
|
||||
class TreeNode {
|
||||
public int val; // 结点值
|
||||
@@ -41,7 +41,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="avl_tree.cpp"
|
||||
```cpp title=""
|
||||
/* AVL 树结点类 */
|
||||
struct TreeNode {
|
||||
int val{}; // 结点值
|
||||
@@ -55,7 +55,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="avl_tree.py"
|
||||
```python title=""
|
||||
""" AVL 树结点类 """
|
||||
class TreeNode:
|
||||
def __init__(self, val=None, left=None, right=None):
|
||||
@@ -67,7 +67,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="avl_tree.go"
|
||||
```go title=""
|
||||
/* AVL 树结点类 */
|
||||
type TreeNode struct {
|
||||
Val int // 结点值
|
||||
@@ -79,7 +79,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
```js title="avl_tree.js"
|
||||
```js title=""
|
||||
class TreeNode {
|
||||
val; // 结点值
|
||||
height; //结点高度
|
||||
@@ -96,7 +96,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
|
||||
=== "TypeScript"
|
||||
|
||||
```typescript title="avl_tree.ts"
|
||||
```typescript title=""
|
||||
class TreeNode {
|
||||
val: number; // 结点值
|
||||
height: number; // 结点高度
|
||||
@@ -113,13 +113,13 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="avl_tree.c"
|
||||
```c title=""
|
||||
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="avl_tree.cs"
|
||||
```csharp title=""
|
||||
/* AVL 树结点类 */
|
||||
class TreeNode {
|
||||
public int val; // 结点值
|
||||
@@ -132,7 +132,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="avl_tree.swift"
|
||||
```swift title=""
|
||||
/* AVL 树结点类 */
|
||||
class TreeNode {
|
||||
var val: Int // 结点值
|
||||
@@ -149,7 +149,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
```zig title=""
|
||||
|
||||
```
|
||||
|
||||
@@ -158,17 +158,9 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "Java"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
/* 获取结点高度 */
|
||||
int height(TreeNode node) {
|
||||
// 空结点高度为 -1 ,叶结点高度为 0
|
||||
return node == null ? -1 : node.height;
|
||||
}
|
||||
[class]{AVLTree}-[func]{height}
|
||||
|
||||
/* 更新结点高度 */
|
||||
void updateHeight(TreeNode node) {
|
||||
// 结点高度等于最高子树高度 + 1
|
||||
node.height = Math.max(height(node.left), height(node.right)) + 1;
|
||||
}
|
||||
[class]{AVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -305,13 +297,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "Java"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
/* 获取结点平衡因子 */
|
||||
public int balanceFactor(TreeNode node) {
|
||||
// 空结点平衡因子为 0
|
||||
if (node == null) return 0;
|
||||
// 结点平衡因子 = 左子树高度 - 右子树高度
|
||||
return height(node.left) - height(node.right);
|
||||
}
|
||||
[class]{AVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -421,7 +407,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
|
||||
### Case 1 - 右旋
|
||||
|
||||
如下图所示(结点下方为「平衡因子」),从底至顶看,二叉树中首个失衡结点是 **结点 3**。我们聚焦在以该失衡结点为根结点的子树上,将该结点记为 `node` ,将其左子节点记为 `child` ,执行「右旋」操作。完成右旋后,该子树已经恢复平衡,并且仍然为二叉搜索树。
|
||||
如下图所示(结点下方为「平衡因子」),从底至顶看,二叉树中首个失衡结点是 **结点 3**。我们聚焦在以该失衡结点为根结点的子树上,将该结点记为 `node` ,将其左子结点记为 `child` ,执行「右旋」操作。完成右旋后,该子树已经恢复平衡,并且仍然为二叉搜索树。
|
||||
|
||||
=== "Step 1"
|
||||

|
||||
@@ -444,19 +430,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Java"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
/* 右旋操作 */
|
||||
TreeNode rightRotate(TreeNode node) {
|
||||
TreeNode child = node.left;
|
||||
TreeNode grandChild = child.right;
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child.right = node;
|
||||
node.left = grandChild;
|
||||
// 更新结点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
return child;
|
||||
}
|
||||
[class]{AVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -472,7 +446,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
```
|
||||
@@ -496,7 +470,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
updateHeight(node)
|
||||
updateHeight(child)
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child
|
||||
}
|
||||
```
|
||||
@@ -514,7 +488,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
this.updateHeight(node);
|
||||
this.updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
```
|
||||
@@ -532,7 +506,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
this.updateHeight(node);
|
||||
this.updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
```
|
||||
@@ -557,7 +531,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
```
|
||||
@@ -575,7 +549,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
updateHeight(node: node)
|
||||
updateHeight(node: child)
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child
|
||||
}
|
||||
```
|
||||
@@ -601,19 +575,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Java"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
/* 左旋操作 */
|
||||
private TreeNode leftRotate(TreeNode node) {
|
||||
TreeNode child = node.right;
|
||||
TreeNode grandChild = child.left;
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child.left = node;
|
||||
node.right = grandChild;
|
||||
// 更新结点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
return child;
|
||||
}
|
||||
[class]{AVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -629,7 +591,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
```
|
||||
@@ -653,7 +615,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
updateHeight(node)
|
||||
updateHeight(child)
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child
|
||||
}
|
||||
```
|
||||
@@ -671,7 +633,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
this.updateHeight(node);
|
||||
this.updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
```
|
||||
@@ -689,7 +651,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
this.updateHeight(node);
|
||||
this.updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
```
|
||||
@@ -716,7 +678,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
updateHeight(node);
|
||||
updateHeight(child);
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child;
|
||||
}
|
||||
```
|
||||
@@ -734,7 +696,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 更新结点高度
|
||||
updateHeight(node: node)
|
||||
updateHeight(node: child)
|
||||
// 返回旋转后子树的根节点
|
||||
// 返回旋转后子树的根结点
|
||||
return child
|
||||
}
|
||||
|
||||
@@ -782,35 +744,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Java"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
TreeNode rotate(TreeNode node) {
|
||||
// 获取结点 node 的平衡因子
|
||||
int balanceFactor = balanceFactor(node);
|
||||
// 左偏树
|
||||
if (balanceFactor > 1) {
|
||||
if (balanceFactor(node.left) >= 0) {
|
||||
// 右旋
|
||||
return rightRotate(node);
|
||||
} else {
|
||||
// 先左旋后右旋
|
||||
node.left = leftRotate(node.left);
|
||||
return rightRotate(node);
|
||||
}
|
||||
}
|
||||
// 右偏树
|
||||
if (balanceFactor < -1) {
|
||||
if (balanceFactor(node.right) <= 0) {
|
||||
// 左旋
|
||||
return leftRotate(node);
|
||||
} else {
|
||||
// 先右旋后左旋
|
||||
node.right = rightRotate(node.right);
|
||||
return leftRotate(node);
|
||||
}
|
||||
}
|
||||
// 平衡树,无需旋转,直接返回
|
||||
return node;
|
||||
}
|
||||
[class]{AVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -1054,28 +988,9 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Java"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
/* 插入结点 */
|
||||
TreeNode insert(int val) {
|
||||
root = insertHelper(root, val);
|
||||
return root;
|
||||
}
|
||||
[class]{AVLTree}-[func]{insert}
|
||||
|
||||
/* 递归插入结点(辅助函数) */
|
||||
TreeNode insertHelper(TreeNode node, int val) {
|
||||
if (node == null) return new TreeNode(val);
|
||||
/* 1. 查找插入位置,并插入结点 */
|
||||
if (val < node.val)
|
||||
node.left = insertHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = insertHelper(node.right, val);
|
||||
else
|
||||
return node; // 重复结点不插入,直接返回
|
||||
updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
return node;
|
||||
}
|
||||
[class]{AVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -1100,7 +1015,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
```
|
||||
@@ -1139,7 +1054,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
updateHeight(node)
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node)
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node
|
||||
}
|
||||
```
|
||||
@@ -1163,7 +1078,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
this.updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = this.rotate(node);
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
```
|
||||
@@ -1191,7 +1106,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
this.updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = this.rotate(node);
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
```
|
||||
@@ -1228,7 +1143,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
```
|
||||
@@ -1260,7 +1175,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
updateHeight(node: node) // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node: node)
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node
|
||||
}
|
||||
```
|
||||
@@ -1278,42 +1193,11 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Java"
|
||||
|
||||
```java title="avl_tree.java"
|
||||
/* 删除结点 */
|
||||
TreeNode remove(int val) {
|
||||
root = removeHelper(root, val);
|
||||
return root;
|
||||
}
|
||||
[class]{AVLTree}-[func]{remove}
|
||||
|
||||
/* 递归删除结点(辅助函数) */
|
||||
TreeNode removeHelper(TreeNode node, int val) {
|
||||
if (node == null) return null;
|
||||
/* 1. 查找结点,并删除之 */
|
||||
if (val < node.val)
|
||||
node.left = removeHelper(node.left, val);
|
||||
else if (val > node.val)
|
||||
node.right = removeHelper(node.right, val);
|
||||
else {
|
||||
if (node.left == null || node.right == null) {
|
||||
TreeNode child = node.left != null ? node.left : node.right;
|
||||
// 子结点数量 = 0 ,直接删除 node 并返回
|
||||
if (child == null)
|
||||
return null;
|
||||
// 子结点数量 = 1 ,直接删除 node
|
||||
else
|
||||
node = child;
|
||||
} else {
|
||||
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
||||
TreeNode temp = getInOrderNext(node.right);
|
||||
node.right = removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
return node;
|
||||
}
|
||||
[class]{AVLTree}-[func]{removeHelper}
|
||||
|
||||
[class]{AVLTree}-[func]{getInOrderNext}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -1356,7 +1240,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
```
|
||||
@@ -1412,7 +1296,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
updateHeight(node)
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node)
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node
|
||||
}
|
||||
```
|
||||
@@ -1449,7 +1333,17 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
this.updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = this.rotate(node);
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
getInOrderNext(node) {
|
||||
if (node === null) return node;
|
||||
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
while (node.left !== null) {
|
||||
node = node.left;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -1501,7 +1395,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
this.updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = this.rotate(node);
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -1564,7 +1458,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
updateHeight(node); // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node);
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node;
|
||||
}
|
||||
```
|
||||
@@ -1611,7 +1505,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
updateHeight(node: node) // 更新结点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node: node)
|
||||
// 返回子树的根节点
|
||||
// 返回子树的根结点
|
||||
return node
|
||||
}
|
||||
```
|
||||
|
||||
@@ -38,21 +38,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="binary_search_tree.java"
|
||||
/* 查找结点 */
|
||||
TreeNode search(int num) {
|
||||
TreeNode cur = root;
|
||||
// 循环查找,越过叶结点后跳出
|
||||
while (cur != null) {
|
||||
// 目标结点在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// 目标结点在 cur 的左子树中
|
||||
else if (cur.val > num) cur = cur.left;
|
||||
// 找到目标结点,跳出循环
|
||||
else break;
|
||||
}
|
||||
// 返回目标结点
|
||||
return cur;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -220,27 +206,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="binary_search_tree.java"
|
||||
/* 插入结点 */
|
||||
TreeNode insert(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == null) return null;
|
||||
TreeNode cur = root, pre = null;
|
||||
// 循环查找,越过叶结点后跳出
|
||||
while (cur != null) {
|
||||
// 找到重复结点,直接返回
|
||||
if (cur.val == num) return null;
|
||||
pre = cur;
|
||||
// 插入位置在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// 插入位置在 cur 的左子树中
|
||||
else cur = cur.left;
|
||||
}
|
||||
// 插入结点 val
|
||||
TreeNode node = new TreeNode(num);
|
||||
if (pre.val < num) pre.right = node;
|
||||
else pre.left = node;
|
||||
return node;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -491,55 +457,9 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="binary_search_tree.java"
|
||||
/* 删除结点 */
|
||||
TreeNode remove(int num) {
|
||||
// 若树为空,直接提前返回
|
||||
if (root == null) return null;
|
||||
TreeNode cur = root, pre = null;
|
||||
// 循环查找,越过叶结点后跳出
|
||||
while (cur != null) {
|
||||
// 找到待删除结点,跳出循环
|
||||
if (cur.val == num) break;
|
||||
pre = cur;
|
||||
// 待删除结点在 cur 的右子树中
|
||||
if (cur.val < num) cur = cur.right;
|
||||
// 待删除结点在 cur 的左子树中
|
||||
else cur = cur.left;
|
||||
}
|
||||
// 若无待删除结点,则直接返回
|
||||
if (cur == null) return null;
|
||||
// 子结点数量 = 0 or 1
|
||||
if (cur.left == null || cur.right == null) {
|
||||
// 当子结点数量 = 0 / 1 时, child = null / 该子结点
|
||||
TreeNode child = cur.left != null ? cur.left : cur.right;
|
||||
// 删除结点 cur
|
||||
if (pre.left == cur) pre.left = child;
|
||||
else pre.right = child;
|
||||
// 释放内存
|
||||
delete cur;
|
||||
}
|
||||
// 子结点数量 = 2
|
||||
else {
|
||||
// 获取中序遍历中 cur 的下一个结点
|
||||
TreeNode nex = getInOrderNext(cur.right);
|
||||
int tmp = nex.val;
|
||||
// 递归删除结点 nex
|
||||
remove(nex.val);
|
||||
// 将 nex 的值复制给 cur
|
||||
cur.val = tmp;
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{remove}
|
||||
|
||||
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
public TreeNode getInOrderNext(TreeNode root) {
|
||||
if (root == null) return root;
|
||||
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
while (root.left != null) {
|
||||
root = root.left;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
[class]{BinarySearchTree}-[func]{getInOrderNext}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
@@ -197,7 +197,7 @@ comments: true
|
||||
|
||||
```python title="binary_tree.py"
|
||||
""" 初始化二叉树 """
|
||||
# 初始化节点
|
||||
# 初始化结点
|
||||
n1 = TreeNode(val=1)
|
||||
n2 = TreeNode(val=2)
|
||||
n3 = TreeNode(val=3)
|
||||
@@ -343,7 +343,7 @@ comments: true
|
||||
# 在 n1 -> n2 中间插入结点 P
|
||||
n1.left = p
|
||||
p.left = n2
|
||||
# 删除节点 P
|
||||
# 删除结点 P
|
||||
n1.left = n2
|
||||
```
|
||||
|
||||
|
||||
@@ -21,22 +21,7 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="binary_tree_bfs.java"
|
||||
/* 层序遍历 */
|
||||
List<Integer> hierOrder(TreeNode root) {
|
||||
// 初始化队列,加入根结点
|
||||
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
List<Integer> list = new ArrayList<>();
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll(); // 队列出队
|
||||
list.add(node.val); // 保存结点值
|
||||
if (node.left != null)
|
||||
queue.offer(node.left); // 左子结点入队
|
||||
if (node.right != null)
|
||||
queue.offer(node.right); // 右子结点入队
|
||||
}
|
||||
return list;
|
||||
}
|
||||
[class]{binary_tree_bfs}-[func]{hierOrder}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
@@ -52,7 +37,7 @@ comments: true
|
||||
while (!queue.empty()) {
|
||||
TreeNode* node = queue.front();
|
||||
queue.pop(); // 队列出队
|
||||
vec.push_back(node->val); // 保存结点
|
||||
vec.push_back(node->val); // 保存结点值
|
||||
if (node->left != nullptr)
|
||||
queue.push(node->left); // 左子结点入队
|
||||
if (node->right != nullptr)
|
||||
@@ -81,7 +66,7 @@ comments: true
|
||||
for queue.Len() > 0 {
|
||||
// poll
|
||||
node := queue.Remove(queue.Front()).(*TreeNode)
|
||||
// 保存结点
|
||||
// 保存结点值
|
||||
nums = append(nums, node.Val)
|
||||
if node.Left != nil {
|
||||
// 左子结点入队
|
||||
@@ -107,7 +92,7 @@ comments: true
|
||||
let list = [];
|
||||
while (queue.length) {
|
||||
let node = queue.shift(); // 队列出队
|
||||
list.push(node.val); // 保存结点
|
||||
list.push(node.val); // 保存结点值
|
||||
if (node.left)
|
||||
queue.push(node.left); // 左子结点入队
|
||||
if (node.right)
|
||||
@@ -128,7 +113,7 @@ comments: true
|
||||
const list: number[] = [];
|
||||
while (queue.length) {
|
||||
let node = queue.shift() as TreeNode; // 队列出队
|
||||
list.push(node.val); // 保存结点
|
||||
list.push(node.val); // 保存结点值
|
||||
if (node.left) {
|
||||
queue.push(node.left); // 左子结点入队
|
||||
}
|
||||
@@ -182,7 +167,7 @@ comments: true
|
||||
var list: [Int] = []
|
||||
while !queue.isEmpty {
|
||||
let node = queue.removeFirst() // 队列出队
|
||||
list.append(node.val) // 保存结点
|
||||
list.append(node.val) // 保存结点值
|
||||
if let left = node.left {
|
||||
queue.append(left) // 左子结点入队
|
||||
}
|
||||
@@ -223,32 +208,11 @@ comments: true
|
||||
=== "Java"
|
||||
|
||||
```java title="binary_tree_dfs.java"
|
||||
/* 前序遍历 */
|
||||
void preOrder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
// 访问优先级:根结点 -> 左子树 -> 右子树
|
||||
list.add(root.val);
|
||||
preOrder(root.left);
|
||||
preOrder(root.right);
|
||||
}
|
||||
|
||||
/* 中序遍历 */
|
||||
void inOrder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
// 访问优先级:左子树 -> 根结点 -> 右子树
|
||||
inOrder(root.left);
|
||||
list.add(root.val);
|
||||
inOrder(root.right);
|
||||
}
|
||||
|
||||
/* 后序遍历 */
|
||||
void postOrder(TreeNode root) {
|
||||
if (root == null) return;
|
||||
// 访问优先级:左子树 -> 右子树 -> 根结点
|
||||
postOrder(root.left);
|
||||
postOrder(root.right);
|
||||
list.add(root.val);
|
||||
}
|
||||
[class]{binary_tree_dfs}-[func]{preOrder}
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{inOrder}
|
||||
|
||||
[class]{binary_tree_dfs}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
Reference in New Issue
Block a user