mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-26 05:33:40 +08:00
Cite the shared TreeNode for the AVL node and the C binary tree node (#2037)
Batch 3b of the runnable-snippets plan. avl_tree_node now cites each language's shared TreeNode in all 13 languages, and the C tree_node_class shares the same region. Rust gets an import region; Java, C#, Swift and Kotlin skip their serialization helpers with an adjacent region. The shared nodes gain reviewed field comments in C++ and Rust, aligned comments in Java, Swift and Kotlin, and a spacing fix in JavaScript. The AVL section adds one sentence saying the shared node class already has height. Four ru Rust files whose multi-line statements had collapsed into literal \n now compile again. en, ja, ko and zh-hant are regenerated; ru is synced by hand. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,7 @@ extern "C" {
|
||||
|
||||
#define MAX_NODE_SIZE 5000
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二叉树节点结构体 */
|
||||
typedef struct TreeNode {
|
||||
int val; // 节点值
|
||||
@@ -34,6 +35,7 @@ TreeNode *newTreeNode(int val) {
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化编码规则请参考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -11,17 +11,19 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二叉树节点结构体 */
|
||||
struct TreeNode {
|
||||
int val{};
|
||||
int height = 0;
|
||||
int val{}; // 节点值
|
||||
int height = 0; // 节点高度
|
||||
TreeNode *parent{};
|
||||
TreeNode *left{};
|
||||
TreeNode *right{};
|
||||
TreeNode *left{}; // 左子节点指针
|
||||
TreeNode *right{}; // 右子节点指针
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {
|
||||
}
|
||||
};
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化编码规则请参考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
|
||||
namespace hello_algo.utils;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二叉树节点类 */
|
||||
public class TreeNode(int? x) {
|
||||
public int? val = x; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode? left; // 左子节点引用
|
||||
public TreeNode? right; // 右子节点引用
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化编码规则请参考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -64,4 +66,6 @@ public class TreeNode(int? x) {
|
||||
TreeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
@@ -14,6 +15,7 @@ class TreeNode {
|
||||
/* 构造方法 */
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* 将列表反序列化为二叉树:递归 */
|
||||
TreeNode? listToTreeDFS(List<int?> arr, int i) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package pkg
|
||||
|
||||
// [region]{tree_node}
|
||||
// TreeNode 二叉树节点
|
||||
type TreeNode struct {
|
||||
Val any // 节点值
|
||||
@@ -22,6 +23,8 @@ func NewTreeNode(v any) *TreeNode {
|
||||
}
|
||||
}
|
||||
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化编码规则请参考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
// 二叉树的数组表示:
|
||||
|
||||
@@ -8,17 +8,19 @@ package utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二叉树节点类 */
|
||||
public class TreeNode {
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode left; // 左子节点引用
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode left; // 左子节点引用
|
||||
public TreeNode right; // 右子节点引用
|
||||
|
||||
/* 构造方法 */
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化编码规则请参考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -70,4 +72,6 @@ public class TreeNode {
|
||||
treeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二叉树节点 */
|
||||
class TreeNode {
|
||||
val; // 节点值
|
||||
left; // 左子节点指针
|
||||
right; // 右子节点指针
|
||||
height; //节点高度
|
||||
height; // 节点高度
|
||||
constructor(val, left, right, height) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.left = left === undefined ? null : left;
|
||||
@@ -17,6 +18,7 @@ class TreeNode {
|
||||
this.height = height === undefined ? 0 : height;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* 将数组反序列化为二叉树 */
|
||||
function arrToTree(arr, i = 0) {
|
||||
|
||||
@@ -6,14 +6,16 @@
|
||||
|
||||
package utils
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二叉树节点类 */
|
||||
/* 构造方法 */
|
||||
class TreeNode(
|
||||
var _val: Int // 节点值
|
||||
) {
|
||||
var height: Int = 0 // 节点高度
|
||||
var left: TreeNode? = null // 左子节点引用
|
||||
var height: Int = 0 // 节点高度
|
||||
var left: TreeNode? = null // 左子节点引用
|
||||
var right: TreeNode? = null // 右子节点引用
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化编码规则请参考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -66,4 +68,6 @@ class TreeNode(
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -7,6 +7,7 @@ Author: krahets (krahets@163.com)
|
||||
from collections import deque
|
||||
|
||||
|
||||
# [region]{tree_node}
|
||||
class TreeNode:
|
||||
"""二叉树节点类"""
|
||||
|
||||
@@ -15,6 +16,7 @@ class TreeNode:
|
||||
self.height: int = 0 # 节点高度
|
||||
self.left: TreeNode | None = None # 左子节点引用
|
||||
self.right: TreeNode | None = None # 右子节点引用
|
||||
# [endregion]{tree_node}
|
||||
|
||||
# 序列化编码规则请参考:
|
||||
# https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -4,6 +4,7 @@ Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# [region]{tree_node}
|
||||
### 二叉树节点类 ###
|
||||
class TreeNode
|
||||
attr_accessor :val # 节点值
|
||||
@@ -16,6 +17,7 @@ class TreeNode
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
# [endregion]{tree_node}
|
||||
|
||||
### 将列表反序列化为二叉数树:递归 ###
|
||||
def arr_to_tree_dfs(arr, i)
|
||||
|
||||
@@ -4,17 +4,20 @@
|
||||
* Author: xBLACKICEx (xBLACKICE@outlook.com), night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{import}
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
// [endregion]{import}
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二叉树节点类型 */
|
||||
#[derive(Debug)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub height: i32,
|
||||
pub val: i32, // 节点值
|
||||
pub height: i32, // 节点高度
|
||||
pub parent: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>, // 左子节点指针
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>, // 右子节点指针
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
@@ -29,6 +32,7 @@ impl TreeNode {
|
||||
}))
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! op_vec {
|
||||
|
||||
@@ -1,182 +1,58 @@
|
||||
<!-- [snippet]{avl_tree_node} -->
|
||||
```python title=""
|
||||
class TreeNode:
|
||||
"""AVL 树节点类"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 节点值
|
||||
self.height: int = 0 # 节点高度
|
||||
self.left: TreeNode | None = None # 左子节点引用
|
||||
self.right: TreeNode | None = None # 右子节点引用
|
||||
[file]{modules/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```cpp title=""
|
||||
/* AVL 树节点类 */
|
||||
struct TreeNode {
|
||||
int val{}; // 节点值
|
||||
int height = 0; // 节点高度
|
||||
TreeNode *left{}; // 左子节点
|
||||
TreeNode *right{}; // 右子节点
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x) : val(x){}
|
||||
};
|
||||
[file]{utils/tree_node.hpp}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```java title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
public int val; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode left; // 左子节点
|
||||
public TreeNode right; // 右子节点
|
||||
public TreeNode(int x) { val = x; }
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```csharp title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode(int? x) {
|
||||
public int? val = x; // 节点值
|
||||
public int height; // 节点高度
|
||||
public TreeNode? left; // 左子节点引用
|
||||
public TreeNode? right; // 右子节点引用
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```go title=""
|
||||
/* AVL 树节点结构体 */
|
||||
type TreeNode struct {
|
||||
Val int // 节点值
|
||||
Height int // 节点高度
|
||||
Left *TreeNode // 左子节点引用
|
||||
Right *TreeNode // 右子节点引用
|
||||
}
|
||||
[file]{pkg/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```swift title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
var val: Int // 节点值
|
||||
var height: Int // 节点高度
|
||||
var left: TreeNode? // 左子节点
|
||||
var right: TreeNode? // 右子节点
|
||||
|
||||
init(x: Int) {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```javascript title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
val; // 节点值
|
||||
height; //节点高度
|
||||
left; // 左子节点指针
|
||||
right; // 右子节点指针
|
||||
constructor(val, left, right, height) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```typescript title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
val: number; // 节点值
|
||||
height: number; // 节点高度
|
||||
left: TreeNode | null; // 左子节点指针
|
||||
right: TreeNode | null; // 右子节点指针
|
||||
constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```dart title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode {
|
||||
int val; // 节点值
|
||||
int height; // 节点高度
|
||||
TreeNode? left; // 左子节点
|
||||
TreeNode? right; // 右子节点
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```rust title=""
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
[file]{src/include/tree_node}-[region]{import}
|
||||
|
||||
/* AVL 树节点结构体 */
|
||||
struct TreeNode {
|
||||
val: i32, // 节点值
|
||||
height: i32, // 节点高度
|
||||
left: Option<Rc<RefCell<TreeNode>>>, // 左子节点
|
||||
right: Option<Rc<RefCell<TreeNode>>>, // 右子节点
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
/* 构造方法 */
|
||||
fn new(val: i32) -> Rc<RefCell<Self>> {
|
||||
Rc::new(RefCell::new(Self {
|
||||
val,
|
||||
height: 0,
|
||||
left: None,
|
||||
right: None
|
||||
}))
|
||||
}
|
||||
}
|
||||
[file]{src/include/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* AVL 树节点结构体 */
|
||||
typedef struct TreeNode {
|
||||
int val;
|
||||
int height;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
} TreeNode;
|
||||
|
||||
/* 构造函数 */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
/* AVL 树节点类 */
|
||||
class TreeNode(val _val: Int) { // 节点值
|
||||
var height: Int = 0 // 节点高度
|
||||
var left: TreeNode? = null // 左子节点
|
||||
var right: TreeNode? = null // 右子节点
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```ruby title=""
|
||||
### AVL 树节点类 ###
|
||||
class TreeNode
|
||||
attr_accessor :val # 节点值
|
||||
attr_accessor :height # 节点高度
|
||||
attr_accessor :left # 左子节点引用
|
||||
attr_accessor :right # 右子节点引用
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
@@ -130,25 +130,7 @@ impl TreeNode {
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* 二叉树节点结构体 */
|
||||
typedef struct TreeNode {
|
||||
int val; // 节点值
|
||||
int height; // 节点高度
|
||||
struct TreeNode *left; // 左子节点指针
|
||||
struct TreeNode *right; // 右子节点指针
|
||||
} TreeNode;
|
||||
|
||||
/* 构造函数 */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二叉树节点类 */
|
||||
public class TreeNode {
|
||||
public var val: Int // 节点值
|
||||
public var height: Int // 节点高度
|
||||
public var left: TreeNode? // 左子节点引用
|
||||
public var val: Int // 节点值
|
||||
public var height: Int // 节点高度
|
||||
public var left: TreeNode? // 左子节点引用
|
||||
public var right: TreeNode? // 右子节点引用
|
||||
|
||||
/* 构造方法 */
|
||||
@@ -16,6 +17,7 @@ public class TreeNode {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化编码规则请参考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -68,4 +70,6 @@ public class TreeNode {
|
||||
treeToListDFS(root: root, i: 0, res: &res)
|
||||
return res
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二叉树节点 */
|
||||
class TreeNode {
|
||||
val: number; // 节点值
|
||||
@@ -22,6 +23,7 @@ class TreeNode {
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* 将数组反序列化为二叉树 */
|
||||
function arrToTree(arr: (number | null)[], i: number = 0): TreeNode | null {
|
||||
|
||||
@@ -18,7 +18,7 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
|
||||
|
||||
### 节点高度
|
||||
|
||||
由于 AVL 树的相关操作需要获取节点高度,因此我们需要为节点类添加 `height` 变量:
|
||||
由于 AVL 树的相关操作需要获取节点高度,因此我们需要为节点类添加 `height` 变量。本书各章的源码共用同一个二叉树节点类,它已经包含该变量:
|
||||
|
||||
```snippet
|
||||
avl_tree_node
|
||||
|
||||
@@ -15,6 +15,7 @@ extern "C" {
|
||||
|
||||
#define MAX_NODE_SIZE 5000
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Binary tree node structure */
|
||||
typedef struct TreeNode {
|
||||
int val; // Node value
|
||||
@@ -34,6 +35,7 @@ TreeNode *newTreeNode(int val) {
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// For the serialization rules, see:
|
||||
// https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -11,17 +11,19 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Binary tree node structure */
|
||||
struct TreeNode {
|
||||
int val{};
|
||||
int height = 0;
|
||||
int val{}; // Node value
|
||||
int height = 0; // Node height
|
||||
TreeNode *parent{};
|
||||
TreeNode *left{};
|
||||
TreeNode *right{};
|
||||
TreeNode *left{}; // Pointer to the left child
|
||||
TreeNode *right{}; // Pointer to the right child
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {
|
||||
}
|
||||
};
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// For the serialization rules, see:
|
||||
// https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
|
||||
namespace hello_algo.utils;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Binary tree node class */
|
||||
public class TreeNode(int? x) {
|
||||
public int? val = x; // Node value
|
||||
public int height; // Node height
|
||||
public TreeNode? left; // Reference to the left child
|
||||
public TreeNode? right; // Reference to the right child
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// For the serialization rules, see:
|
||||
// https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/
|
||||
@@ -64,4 +66,6 @@ public class TreeNode(int? x) {
|
||||
TreeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Binary tree node class */
|
||||
class TreeNode {
|
||||
int val; // Node value
|
||||
@@ -14,6 +15,7 @@ class TreeNode {
|
||||
/* Constructor */
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* Deserialize a list into a binary tree: recursion */
|
||||
TreeNode? listToTreeDFS(List<int?> arr, int i) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package pkg
|
||||
|
||||
// [region]{tree_node}
|
||||
// TreeNode represents a binary tree node.
|
||||
type TreeNode struct {
|
||||
Val any // Node value
|
||||
@@ -22,6 +23,8 @@ func NewTreeNode(v any) *TreeNode {
|
||||
}
|
||||
}
|
||||
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// For the serialization rules, see:
|
||||
// https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/
|
||||
// Array representation of the binary tree:
|
||||
|
||||
@@ -8,17 +8,19 @@ package utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Binary tree node class */
|
||||
public class TreeNode {
|
||||
public int val; // Node value
|
||||
public int height; // Node height
|
||||
public TreeNode left; // Reference to the left child
|
||||
public int val; // Node value
|
||||
public int height; // Node height
|
||||
public TreeNode left; // Reference to the left child
|
||||
public TreeNode right; // Reference to the right child
|
||||
|
||||
/* Constructor */
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// For the serialization rules, see:
|
||||
// https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/
|
||||
@@ -70,4 +72,6 @@ public class TreeNode {
|
||||
treeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Binary tree node */
|
||||
class TreeNode {
|
||||
val; // Node value
|
||||
left; // Pointer to the left child
|
||||
right; // Pointer to the right child
|
||||
height; //Node height
|
||||
height; // Node height
|
||||
constructor(val, left, right, height) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.left = left === undefined ? null : left;
|
||||
@@ -17,6 +18,7 @@ class TreeNode {
|
||||
this.height = height === undefined ? 0 : height;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* Deserialize an array into a binary tree */
|
||||
function arrToTree(arr, i = 0) {
|
||||
|
||||
@@ -6,14 +6,16 @@
|
||||
|
||||
package utils
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Binary tree node class */
|
||||
/* Constructor */
|
||||
class TreeNode(
|
||||
var _val: Int // Node value
|
||||
) {
|
||||
var height: Int = 0 // Node height
|
||||
var left: TreeNode? = null // Reference to the left child
|
||||
var height: Int = 0 // Node height
|
||||
var left: TreeNode? = null // Reference to the left child
|
||||
var right: TreeNode? = null // Reference to the right child
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// For the serialization rules, see:
|
||||
// https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/
|
||||
@@ -66,4 +68,6 @@ class TreeNode(
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -7,6 +7,7 @@ Author: krahets (krahets@163.com)
|
||||
from collections import deque
|
||||
|
||||
|
||||
# [region]{tree_node}
|
||||
class TreeNode:
|
||||
"""Binary tree node class"""
|
||||
|
||||
@@ -15,6 +16,7 @@ class TreeNode:
|
||||
self.height: int = 0 # Node height
|
||||
self.left: TreeNode | None = None # Reference to the left child
|
||||
self.right: TreeNode | None = None # Reference to the right child
|
||||
# [endregion]{tree_node}
|
||||
|
||||
# For the serialization rules, see:
|
||||
# https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -4,6 +4,7 @@ Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# [region]{tree_node}
|
||||
### Binary tree node class ###
|
||||
class TreeNode
|
||||
attr_accessor :val # Node value
|
||||
@@ -16,6 +17,7 @@ class TreeNode
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
# [endregion]{tree_node}
|
||||
|
||||
### Deserialize a list into a binary tree: recursion ###
|
||||
def arr_to_tree_dfs(arr, i)
|
||||
|
||||
@@ -4,17 +4,20 @@
|
||||
* Author: xBLACKICEx (xBLACKICE@outlook.com), night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{import}
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
// [endregion]{import}
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Binary tree node type */
|
||||
#[derive(Debug)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub height: i32,
|
||||
pub val: i32, // Node value
|
||||
pub height: i32, // Node height
|
||||
pub parent: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>, // Pointer to the left child
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>, // Pointer to the right child
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
@@ -29,6 +32,7 @@ impl TreeNode {
|
||||
}))
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! op_vec {
|
||||
|
||||
@@ -1,182 +1,58 @@
|
||||
<!-- [snippet]{avl_tree_node} -->
|
||||
```python title=""
|
||||
class TreeNode:
|
||||
"""AVL tree node class"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # Node value
|
||||
self.height: int = 0 # Node height
|
||||
self.left: TreeNode | None = None # Left child reference
|
||||
self.right: TreeNode | None = None # Right child reference
|
||||
[file]{modules/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```cpp title=""
|
||||
/* AVL tree node class */
|
||||
struct TreeNode {
|
||||
int val{}; // Node value
|
||||
int height = 0; // Node height
|
||||
TreeNode *left{}; // Left child
|
||||
TreeNode *right{}; // Right child
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x) : val(x){}
|
||||
};
|
||||
[file]{utils/tree_node.hpp}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```java title=""
|
||||
/* AVL tree node class */
|
||||
class TreeNode {
|
||||
public int val; // Node value
|
||||
public int height; // Node height
|
||||
public TreeNode left; // Left child
|
||||
public TreeNode right; // Right child
|
||||
public TreeNode(int x) { val = x; }
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```csharp title=""
|
||||
/* AVL tree node class */
|
||||
class TreeNode(int? x) {
|
||||
public int? val = x; // Node value
|
||||
public int height; // Node height
|
||||
public TreeNode? left; // Left child reference
|
||||
public TreeNode? right; // Right child reference
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```go title=""
|
||||
/* AVL tree node struct */
|
||||
type TreeNode struct {
|
||||
Val int // Node value
|
||||
Height int // Node height
|
||||
Left *TreeNode // Left child reference
|
||||
Right *TreeNode // Right child reference
|
||||
}
|
||||
[file]{pkg/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```swift title=""
|
||||
/* AVL tree node class */
|
||||
class TreeNode {
|
||||
var val: Int // Node value
|
||||
var height: Int // Node height
|
||||
var left: TreeNode? // Left child
|
||||
var right: TreeNode? // Right child
|
||||
|
||||
init(x: Int) {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```javascript title=""
|
||||
/* AVL tree node class */
|
||||
class TreeNode {
|
||||
val; // Node value
|
||||
height; //Node height
|
||||
left; // Left child pointer
|
||||
right; // Right child pointer
|
||||
constructor(val, left, right, height) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```typescript title=""
|
||||
/* AVL tree node class */
|
||||
class TreeNode {
|
||||
val: number; // Node value
|
||||
height: number; // Node height
|
||||
left: TreeNode | null; // Left child pointer
|
||||
right: TreeNode | null; // Right child pointer
|
||||
constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```dart title=""
|
||||
/* AVL tree node class */
|
||||
class TreeNode {
|
||||
int val; // Node value
|
||||
int height; // Node height
|
||||
TreeNode? left; // Left child
|
||||
TreeNode? right; // Right child
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```rust title=""
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
[file]{src/include/tree_node}-[region]{import}
|
||||
|
||||
/* AVL tree node struct */
|
||||
struct TreeNode {
|
||||
val: i32, // Node value
|
||||
height: i32, // Node height
|
||||
left: Option<Rc<RefCell<TreeNode>>>, // Left child
|
||||
right: Option<Rc<RefCell<TreeNode>>>, // Right child
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
/* Constructor */
|
||||
fn new(val: i32) -> Rc<RefCell<Self>> {
|
||||
Rc::new(RefCell::new(Self {
|
||||
val,
|
||||
height: 0,
|
||||
left: None,
|
||||
right: None
|
||||
}))
|
||||
}
|
||||
}
|
||||
[file]{src/include/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* AVL tree node struct */
|
||||
typedef struct TreeNode {
|
||||
int val;
|
||||
int height;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
} TreeNode;
|
||||
|
||||
/* Constructor */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
/* AVL tree node class */
|
||||
class TreeNode(val _val: Int) { // Node value
|
||||
var height: Int = 0 // Node height
|
||||
var left: TreeNode? = null // Left child
|
||||
var right: TreeNode? = null // Right child
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```ruby title=""
|
||||
### AVL tree node class ###
|
||||
class TreeNode
|
||||
attr_accessor :val # Node value
|
||||
attr_accessor :height # Node height
|
||||
attr_accessor :left # Left child reference
|
||||
attr_accessor :right # Right child reference
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
@@ -130,25 +130,7 @@ impl TreeNode {
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* Binary tree node struct */
|
||||
typedef struct TreeNode {
|
||||
int val; // Node value
|
||||
int height; // Node height
|
||||
struct TreeNode *left; // Left child pointer
|
||||
struct TreeNode *right; // Right child pointer
|
||||
} TreeNode;
|
||||
|
||||
/* Constructor */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Binary tree node class */
|
||||
public class TreeNode {
|
||||
public var val: Int // Node value
|
||||
public var height: Int // Node height
|
||||
public var left: TreeNode? // Reference to the left child
|
||||
public var val: Int // Node value
|
||||
public var height: Int // Node height
|
||||
public var left: TreeNode? // Reference to the left child
|
||||
public var right: TreeNode? // Reference to the right child
|
||||
|
||||
/* Constructor */
|
||||
@@ -16,6 +17,7 @@ public class TreeNode {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// For the serialization rules, see:
|
||||
// https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/
|
||||
@@ -68,4 +70,6 @@ public class TreeNode {
|
||||
treeToListDFS(root: root, i: 0, res: &res)
|
||||
return res
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Binary tree node */
|
||||
class TreeNode {
|
||||
val: number; // Node value
|
||||
@@ -22,6 +23,7 @@ class TreeNode {
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* Deserialize an array into a binary tree */
|
||||
function arrToTree(arr: (number | null)[], i: number = 0): TreeNode | null {
|
||||
|
||||
@@ -18,7 +18,7 @@ An AVL tree is both a binary search tree and a balanced binary tree, satisfying
|
||||
|
||||
### Node height
|
||||
|
||||
AVL operations need node heights, so add a `height` variable to the node class:
|
||||
AVL operations need node heights, so add a `height` variable to the node class. All chapters of this book's source code share one binary tree node class, which already includes it:
|
||||
|
||||
```snippet
|
||||
avl_tree_node
|
||||
|
||||
@@ -15,6 +15,7 @@ extern "C" {
|
||||
|
||||
#define MAX_NODE_SIZE 5000
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二分木のノードの構造体 */
|
||||
typedef struct TreeNode {
|
||||
int val; // ノードの値
|
||||
@@ -34,6 +35,7 @@ TreeNode *newTreeNode(int val) {
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// シリアライズの符号化規則は次を参照:
|
||||
// https://www.hello-algo.com/ja/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -11,17 +11,19 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二分木のノードの構造体 */
|
||||
struct TreeNode {
|
||||
int val{};
|
||||
int height = 0;
|
||||
int val{}; // ノードの値
|
||||
int height = 0; // ノードの高さ
|
||||
TreeNode *parent{};
|
||||
TreeNode *left{};
|
||||
TreeNode *right{};
|
||||
TreeNode *left{}; // 左の子ノードへのポインタ
|
||||
TreeNode *right{}; // 右の子ノードへのポインタ
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {
|
||||
}
|
||||
};
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// シリアライズの符号化規則は次を参照:
|
||||
// https://www.hello-algo.com/ja/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
|
||||
namespace hello_algo.utils;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二分木のノードクラス */
|
||||
public class TreeNode(int? x) {
|
||||
public int? val = x; // ノードの値
|
||||
public int height; // ノードの高さ
|
||||
public TreeNode? left; // 左の子ノードへの参照
|
||||
public TreeNode? right; // 右の子ノードへの参照
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// シリアライズの符号化規則は次を参照:
|
||||
// https://www.hello-algo.com/ja/chapter_tree/array_representation_of_tree/
|
||||
@@ -64,4 +66,6 @@ public class TreeNode(int? x) {
|
||||
TreeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二分木のノードクラス */
|
||||
class TreeNode {
|
||||
int val; // ノードの値
|
||||
@@ -14,6 +15,7 @@ class TreeNode {
|
||||
/* コンストラクタ */
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* リストから二分木へデシリアライズ:再帰 */
|
||||
TreeNode? listToTreeDFS(List<int?> arr, int i) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package pkg
|
||||
|
||||
// [region]{tree_node}
|
||||
// TreeNode は二分木のノード
|
||||
type TreeNode struct {
|
||||
Val any // ノードの値
|
||||
@@ -22,6 +23,8 @@ func NewTreeNode(v any) *TreeNode {
|
||||
}
|
||||
}
|
||||
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// シリアライズの符号化規則は次を参照:
|
||||
// https://www.hello-algo.com/ja/chapter_tree/array_representation_of_tree/
|
||||
// 二分木の配列表現:
|
||||
|
||||
@@ -8,17 +8,19 @@ package utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二分木のノードクラス */
|
||||
public class TreeNode {
|
||||
public int val; // ノードの値
|
||||
public int height; // ノードの高さ
|
||||
public TreeNode left; // 左の子ノードへの参照
|
||||
public int val; // ノードの値
|
||||
public int height; // ノードの高さ
|
||||
public TreeNode left; // 左の子ノードへの参照
|
||||
public TreeNode right; // 右の子ノードへの参照
|
||||
|
||||
/* コンストラクタ */
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// シリアライズの符号化規則は次を参照:
|
||||
// https://www.hello-algo.com/ja/chapter_tree/array_representation_of_tree/
|
||||
@@ -70,4 +72,6 @@ public class TreeNode {
|
||||
treeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二分木のノード */
|
||||
class TreeNode {
|
||||
val; // ノードの値
|
||||
left; // 左の子ノードへのポインタ
|
||||
right; // 右の子ノードへのポインタ
|
||||
height; //ノードの高さ
|
||||
height; // ノードの高さ
|
||||
constructor(val, left, right, height) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.left = left === undefined ? null : left;
|
||||
@@ -17,6 +18,7 @@ class TreeNode {
|
||||
this.height = height === undefined ? 0 : height;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* 配列をデシリアライズして二分木にする */
|
||||
function arrToTree(arr, i = 0) {
|
||||
|
||||
@@ -6,14 +6,16 @@
|
||||
|
||||
package utils
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二分木のノードクラス */
|
||||
/* コンストラクタ */
|
||||
class TreeNode(
|
||||
var _val: Int // ノードの値
|
||||
) {
|
||||
var height: Int = 0 // ノードの高さ
|
||||
var left: TreeNode? = null // 左の子ノードへの参照
|
||||
var height: Int = 0 // ノードの高さ
|
||||
var left: TreeNode? = null // 左の子ノードへの参照
|
||||
var right: TreeNode? = null // 右の子ノードへの参照
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// シリアライズの符号化規則は次を参照:
|
||||
// https://www.hello-algo.com/ja/chapter_tree/array_representation_of_tree/
|
||||
@@ -66,4 +68,6 @@ class TreeNode(
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -7,6 +7,7 @@ Author: krahets (krahets@163.com)
|
||||
from collections import deque
|
||||
|
||||
|
||||
# [region]{tree_node}
|
||||
class TreeNode:
|
||||
"""二分木のノードクラス"""
|
||||
|
||||
@@ -15,6 +16,7 @@ class TreeNode:
|
||||
self.height: int = 0 # ノードの高さ
|
||||
self.left: TreeNode | None = None # 左の子ノードへの参照
|
||||
self.right: TreeNode | None = None # 右の子ノードへの参照
|
||||
# [endregion]{tree_node}
|
||||
|
||||
# シリアライズの符号化規則は次を参照:
|
||||
# https://www.hello-algo.com/ja/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -4,6 +4,7 @@ Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# [region]{tree_node}
|
||||
### 二分木のノードクラス ###
|
||||
class TreeNode
|
||||
attr_accessor :val # ノードの値
|
||||
@@ -16,6 +17,7 @@ class TreeNode
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
# [endregion]{tree_node}
|
||||
|
||||
### リストをデシリアライズして二分木にする:再帰 ###
|
||||
def arr_to_tree_dfs(arr, i)
|
||||
|
||||
@@ -4,17 +4,20 @@
|
||||
* Author: xBLACKICEx (xBLACKICE@outlook.com), night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{import}
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
// [endregion]{import}
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二分木のノードの型 */
|
||||
#[derive(Debug)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub height: i32,
|
||||
pub val: i32, // ノードの値
|
||||
pub height: i32, // ノードの高さ
|
||||
pub parent: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>, // 左の子ノードへのポインタ
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>, // 右の子ノードへのポインタ
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
@@ -29,6 +32,7 @@ impl TreeNode {
|
||||
}))
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! op_vec {
|
||||
|
||||
@@ -1,182 +1,58 @@
|
||||
<!-- [snippet]{avl_tree_node} -->
|
||||
```python title=""
|
||||
class TreeNode:
|
||||
"""AVL 木のノードクラス"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # ノードの値
|
||||
self.height: int = 0 # ノードの高さ
|
||||
self.left: TreeNode | None = None # 左の子への参照
|
||||
self.right: TreeNode | None = None # 右の子への参照
|
||||
[file]{modules/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```cpp title=""
|
||||
/* AVL 木のノードクラス */
|
||||
struct TreeNode {
|
||||
int val{}; // ノードの値
|
||||
int height = 0; // ノードの高さ
|
||||
TreeNode *left{}; // 左の子
|
||||
TreeNode *right{}; // 右の子
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x) : val(x){}
|
||||
};
|
||||
[file]{utils/tree_node.hpp}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```java title=""
|
||||
/* AVL 木のノードクラス */
|
||||
class TreeNode {
|
||||
public int val; // ノードの値
|
||||
public int height; // ノードの高さ
|
||||
public TreeNode left; // 左の子
|
||||
public TreeNode right; // 右の子
|
||||
public TreeNode(int x) { val = x; }
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```csharp title=""
|
||||
/* AVL 木のノードクラス */
|
||||
class TreeNode(int? x) {
|
||||
public int? val = x; // ノードの値
|
||||
public int height; // ノードの高さ
|
||||
public TreeNode? left; // 左の子への参照
|
||||
public TreeNode? right; // 右の子への参照
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```go title=""
|
||||
/* AVL 木のノード構造体 */
|
||||
type TreeNode struct {
|
||||
Val int // ノードの値
|
||||
Height int // ノードの高さ
|
||||
Left *TreeNode // 左の子への参照
|
||||
Right *TreeNode // 右の子への参照
|
||||
}
|
||||
[file]{pkg/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```swift title=""
|
||||
/* AVL 木のノードクラス */
|
||||
class TreeNode {
|
||||
var val: Int // ノードの値
|
||||
var height: Int // ノードの高さ
|
||||
var left: TreeNode? // 左の子
|
||||
var right: TreeNode? // 右の子
|
||||
|
||||
init(x: Int) {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```javascript title=""
|
||||
/* AVL 木のノードクラス */
|
||||
class TreeNode {
|
||||
val; // ノードの値
|
||||
height; //ノードの高さ
|
||||
left; // 左の子へのポインタ
|
||||
right; // 右の子へのポインタ
|
||||
constructor(val, left, right, height) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```typescript title=""
|
||||
/* AVL 木のノードクラス */
|
||||
class TreeNode {
|
||||
val: number; // ノードの値
|
||||
height: number; // ノードの高さ
|
||||
left: TreeNode | null; // 左の子へのポインタ
|
||||
right: TreeNode | null; // 右の子へのポインタ
|
||||
constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```dart title=""
|
||||
/* AVL 木のノードクラス */
|
||||
class TreeNode {
|
||||
int val; // ノードの値
|
||||
int height; // ノードの高さ
|
||||
TreeNode? left; // 左の子
|
||||
TreeNode? right; // 右の子
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```rust title=""
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
[file]{src/include/tree_node}-[region]{import}
|
||||
|
||||
/* AVL 木のノード構造体 */
|
||||
struct TreeNode {
|
||||
val: i32, // ノードの値
|
||||
height: i32, // ノードの高さ
|
||||
left: Option<Rc<RefCell<TreeNode>>>, // 左の子
|
||||
right: Option<Rc<RefCell<TreeNode>>>, // 右の子
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
/* コンストラクタ */
|
||||
fn new(val: i32) -> Rc<RefCell<Self>> {
|
||||
Rc::new(RefCell::new(Self {
|
||||
val,
|
||||
height: 0,
|
||||
left: None,
|
||||
right: None
|
||||
}))
|
||||
}
|
||||
}
|
||||
[file]{src/include/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* AVL 木のノード構造体 */
|
||||
typedef struct TreeNode {
|
||||
int val;
|
||||
int height;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
} TreeNode;
|
||||
|
||||
/* コンストラクタ */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
/* AVL 木のノードクラス */
|
||||
class TreeNode(val _val: Int) { // ノードの値
|
||||
var height: Int = 0 // ノードの高さ
|
||||
var left: TreeNode? = null // 左の子
|
||||
var right: TreeNode? = null // 右の子
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```ruby title=""
|
||||
### AVL 木のノードクラス ###
|
||||
class TreeNode
|
||||
attr_accessor :val # ノードの値
|
||||
attr_accessor :height # ノードの高さ
|
||||
attr_accessor :left # 左の子への参照
|
||||
attr_accessor :right # 右の子への参照
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
@@ -130,25 +130,7 @@ impl TreeNode {
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* 二分木のノード構造体 */
|
||||
typedef struct TreeNode {
|
||||
int val; // ノードの値
|
||||
int height; // ノードの高さ
|
||||
struct TreeNode *left; // 左の子へのポインタ
|
||||
struct TreeNode *right; // 右の子へのポインタ
|
||||
} TreeNode;
|
||||
|
||||
/* コンストラクタ */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二分木のノードクラス */
|
||||
public class TreeNode {
|
||||
public var val: Int // ノードの値
|
||||
public var height: Int // ノードの高さ
|
||||
public var left: TreeNode? // 左の子ノードへの参照
|
||||
public var val: Int // ノードの値
|
||||
public var height: Int // ノードの高さ
|
||||
public var left: TreeNode? // 左の子ノードへの参照
|
||||
public var right: TreeNode? // 右の子ノードへの参照
|
||||
|
||||
/* コンストラクタ */
|
||||
@@ -16,6 +17,7 @@ public class TreeNode {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// シリアライズの符号化規則は次を参照:
|
||||
// https://www.hello-algo.com/ja/chapter_tree/array_representation_of_tree/
|
||||
@@ -68,4 +70,6 @@ public class TreeNode {
|
||||
treeToListDFS(root: root, i: 0, res: &res)
|
||||
return res
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二分木のノード */
|
||||
class TreeNode {
|
||||
val: number; // ノードの値
|
||||
@@ -22,6 +23,7 @@ class TreeNode {
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* 配列をデシリアライズして二分木にする */
|
||||
function arrToTree(arr: (number | null)[], i: number = 0): TreeNode | null {
|
||||
|
||||
@@ -18,7 +18,7 @@ AVL 木は二分探索木であると同時に平衡二分木でもあり、両
|
||||
|
||||
### ノードの高さ
|
||||
|
||||
AVL 木の操作ではノードの高さが必要なので、ノードクラスに変数 `height` を追加します。
|
||||
AVL 木の操作ではノードの高さが必要なので、ノードクラスに変数 `height` を追加します。本書のソースコードでは全章が同じ二分木のノードクラスを共有しており、このクラスにはすでに `height` が含まれています。
|
||||
|
||||
```snippet
|
||||
avl_tree_node
|
||||
|
||||
@@ -15,6 +15,7 @@ extern "C" {
|
||||
|
||||
#define MAX_NODE_SIZE 5000
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 이진 트리 노드 구조체 */
|
||||
typedef struct TreeNode {
|
||||
int val; // 노드 값
|
||||
@@ -34,6 +35,7 @@ TreeNode *newTreeNode(int val) {
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 직렬화 인코딩 규칙은 다음을 참고한다:
|
||||
// https://www.hello-algo.com/ko/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -11,17 +11,19 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 이진 트리 노드 구조체 */
|
||||
struct TreeNode {
|
||||
int val{};
|
||||
int height = 0;
|
||||
int val{}; // 노드 값
|
||||
int height = 0; // 노드 높이
|
||||
TreeNode *parent{};
|
||||
TreeNode *left{};
|
||||
TreeNode *right{};
|
||||
TreeNode *left{}; // 왼쪽 자식 노드 포인터
|
||||
TreeNode *right{}; // 오른쪽 자식 노드 포인터
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {
|
||||
}
|
||||
};
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 직렬화 인코딩 규칙은 다음을 참고한다:
|
||||
// https://www.hello-algo.com/ko/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
|
||||
namespace hello_algo.utils;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 이진 트리 노드 클래스 */
|
||||
public class TreeNode(int? x) {
|
||||
public int? val = x; // 노드 값
|
||||
public int height; // 노드 높이
|
||||
public TreeNode? left; // 왼쪽 자식 노드 참조
|
||||
public TreeNode? right; // 오른쪽 자식 노드 참조
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 직렬화 인코딩 규칙은 다음을 참고한다:
|
||||
// https://www.hello-algo.com/ko/chapter_tree/array_representation_of_tree/
|
||||
@@ -64,4 +66,6 @@ public class TreeNode(int? x) {
|
||||
TreeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 이진 트리 노드 클래스 */
|
||||
class TreeNode {
|
||||
int val; // 노드 값
|
||||
@@ -14,6 +15,7 @@ class TreeNode {
|
||||
/* 생성자 */
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* 리스트를 이진 트리로 역직렬화: 재귀 */
|
||||
TreeNode? listToTreeDFS(List<int?> arr, int i) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package pkg
|
||||
|
||||
// [region]{tree_node}
|
||||
// TreeNode: 이진 트리 노드
|
||||
type TreeNode struct {
|
||||
Val any // 노드 값
|
||||
@@ -22,6 +23,8 @@ func NewTreeNode(v any) *TreeNode {
|
||||
}
|
||||
}
|
||||
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 직렬화 인코딩 규칙은 다음을 참고한다:
|
||||
// https://www.hello-algo.com/ko/chapter_tree/array_representation_of_tree/
|
||||
// 이진 트리의 배열 표현:
|
||||
|
||||
@@ -8,17 +8,19 @@ package utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 이진 트리 노드 클래스 */
|
||||
public class TreeNode {
|
||||
public int val; // 노드 값
|
||||
public int height; // 노드 높이
|
||||
public TreeNode left; // 왼쪽 자식 노드 참조
|
||||
public int val; // 노드 값
|
||||
public int height; // 노드 높이
|
||||
public TreeNode left; // 왼쪽 자식 노드 참조
|
||||
public TreeNode right; // 오른쪽 자식 노드 참조
|
||||
|
||||
/* 생성자 */
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 직렬화 인코딩 규칙은 다음을 참고한다:
|
||||
// https://www.hello-algo.com/ko/chapter_tree/array_representation_of_tree/
|
||||
@@ -70,4 +72,6 @@ public class TreeNode {
|
||||
treeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 이진 트리 노드 */
|
||||
class TreeNode {
|
||||
val; // 노드 값
|
||||
left; // 왼쪽 자식 노드 포인터
|
||||
right; // 오른쪽 자식 노드 포인터
|
||||
height; //노드 높이
|
||||
height; // 노드 높이
|
||||
constructor(val, left, right, height) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.left = left === undefined ? null : left;
|
||||
@@ -17,6 +18,7 @@ class TreeNode {
|
||||
this.height = height === undefined ? 0 : height;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* 배열을 역직렬화하여 이진 트리로 변환한다 */
|
||||
function arrToTree(arr, i = 0) {
|
||||
|
||||
@@ -6,14 +6,16 @@
|
||||
|
||||
package utils
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 이진 트리 노드 클래스 */
|
||||
/* 생성자 */
|
||||
class TreeNode(
|
||||
var _val: Int // 노드 값
|
||||
) {
|
||||
var height: Int = 0 // 노드 높이
|
||||
var left: TreeNode? = null // 왼쪽 자식 노드 참조
|
||||
var height: Int = 0 // 노드 높이
|
||||
var left: TreeNode? = null // 왼쪽 자식 노드 참조
|
||||
var right: TreeNode? = null // 오른쪽 자식 노드 참조
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 직렬화 인코딩 규칙은 다음을 참고한다:
|
||||
// https://www.hello-algo.com/ko/chapter_tree/array_representation_of_tree/
|
||||
@@ -66,4 +68,6 @@ class TreeNode(
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -7,6 +7,7 @@ Author: krahets (krahets@163.com)
|
||||
from collections import deque
|
||||
|
||||
|
||||
# [region]{tree_node}
|
||||
class TreeNode:
|
||||
"""이진 트리 노드 클래스"""
|
||||
|
||||
@@ -15,6 +16,7 @@ class TreeNode:
|
||||
self.height: int = 0 # 노드 높이
|
||||
self.left: TreeNode | None = None # 왼쪽 자식 노드 참조
|
||||
self.right: TreeNode | None = None # 오른쪽 자식 노드 참조
|
||||
# [endregion]{tree_node}
|
||||
|
||||
# 직렬화 인코딩 규칙은 다음을 참고한다:
|
||||
# https://www.hello-algo.com/ko/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -4,6 +4,7 @@ Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# [region]{tree_node}
|
||||
### 이진 트리 노드 클래스 ###
|
||||
class TreeNode
|
||||
attr_accessor :val # 노드 값
|
||||
@@ -16,6 +17,7 @@ class TreeNode
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
# [endregion]{tree_node}
|
||||
|
||||
### 리스트를 이진 트리로 역직렬화: 재귀 ###
|
||||
def arr_to_tree_dfs(arr, i)
|
||||
|
||||
@@ -4,17 +4,20 @@
|
||||
* Author: xBLACKICEx (xBLACKICE@outlook.com), night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{import}
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
// [endregion]{import}
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 이진 트리 노드 자료형 */
|
||||
#[derive(Debug)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub height: i32,
|
||||
pub val: i32, // 노드 값
|
||||
pub height: i32, // 노드 높이
|
||||
pub parent: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>, // 왼쪽 자식 노드 포인터
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>, // 오른쪽 자식 노드 포인터
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
@@ -29,6 +32,7 @@ impl TreeNode {
|
||||
}))
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! op_vec {
|
||||
|
||||
@@ -1,182 +1,58 @@
|
||||
<!-- [snippet]{avl_tree_node} -->
|
||||
```python title=""
|
||||
class TreeNode:
|
||||
"""AVL 트리 노드 클래스"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 노드 값
|
||||
self.height: int = 0 # 노드 높이
|
||||
self.left: TreeNode | None = None # 왼쪽 자식 노드 참조
|
||||
self.right: TreeNode | None = None # 오른쪽 자식 노드 참조
|
||||
[file]{modules/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```cpp title=""
|
||||
/* AVL 트리 노드 클래스 */
|
||||
struct TreeNode {
|
||||
int val{}; // 노드 값
|
||||
int height = 0; // 노드 높이
|
||||
TreeNode *left{}; // 왼쪽 자식 노드
|
||||
TreeNode *right{}; // 오른쪽 자식 노드
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x) : val(x){}
|
||||
};
|
||||
[file]{utils/tree_node.hpp}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```java title=""
|
||||
/* AVL 트리 노드 클래스 */
|
||||
class TreeNode {
|
||||
public int val; // 노드 값
|
||||
public int height; // 노드 높이
|
||||
public TreeNode left; // 왼쪽 자식 노드
|
||||
public TreeNode right; // 오른쪽 자식 노드
|
||||
public TreeNode(int x) { val = x; }
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```csharp title=""
|
||||
/* AVL 트리 노드 클래스 */
|
||||
class TreeNode(int? x) {
|
||||
public int? val = x; // 노드 값
|
||||
public int height; // 노드 높이
|
||||
public TreeNode? left; // 왼쪽 자식 노드 참조
|
||||
public TreeNode? right; // 오른쪽 자식 노드 참조
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```go title=""
|
||||
/* AVL 트리 노드 구조체 */
|
||||
type TreeNode struct {
|
||||
Val int // 노드 값
|
||||
Height int // 노드 높이
|
||||
Left *TreeNode // 왼쪽 자식 노드 참조
|
||||
Right *TreeNode // 오른쪽 자식 노드 참조
|
||||
}
|
||||
[file]{pkg/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```swift title=""
|
||||
/* AVL 트리 노드 클래스 */
|
||||
class TreeNode {
|
||||
var val: Int // 노드 값
|
||||
var height: Int // 노드 높이
|
||||
var left: TreeNode? // 왼쪽 자식 노드
|
||||
var right: TreeNode? // 오른쪽 자식 노드
|
||||
|
||||
init(x: Int) {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```javascript title=""
|
||||
/* AVL 트리 노드 클래스 */
|
||||
class TreeNode {
|
||||
val; // 노드 값
|
||||
height; //노드 높이
|
||||
left; // 왼쪽 자식 노드 포인터
|
||||
right; // 오른쪽 자식 노드 포인터
|
||||
constructor(val, left, right, height) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```typescript title=""
|
||||
/* AVL 트리 노드 클래스 */
|
||||
class TreeNode {
|
||||
val: number; // 노드 값
|
||||
height: number; // 노드 높이
|
||||
left: TreeNode | null; // 왼쪽 자식 노드 포인터
|
||||
right: TreeNode | null; // 오른쪽 자식 노드 포인터
|
||||
constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```dart title=""
|
||||
/* AVL 트리 노드 클래스 */
|
||||
class TreeNode {
|
||||
int val; // 노드 값
|
||||
int height; // 노드 높이
|
||||
TreeNode? left; // 왼쪽 자식 노드
|
||||
TreeNode? right; // 오른쪽 자식 노드
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```rust title=""
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
[file]{src/include/tree_node}-[region]{import}
|
||||
|
||||
/* AVL 트리 노드 구조체 */
|
||||
struct TreeNode {
|
||||
val: i32, // 노드 값
|
||||
height: i32, // 노드 높이
|
||||
left: Option<Rc<RefCell<TreeNode>>>, // 왼쪽 자식 노드
|
||||
right: Option<Rc<RefCell<TreeNode>>>, // 오른쪽 자식 노드
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
/* 생성자 */
|
||||
fn new(val: i32) -> Rc<RefCell<Self>> {
|
||||
Rc::new(RefCell::new(Self {
|
||||
val,
|
||||
height: 0,
|
||||
left: None,
|
||||
right: None
|
||||
}))
|
||||
}
|
||||
}
|
||||
[file]{src/include/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* AVL 트리 노드 구조체 */
|
||||
typedef struct TreeNode {
|
||||
int val;
|
||||
int height;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
} TreeNode;
|
||||
|
||||
/* 생성자 */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
/* AVL 트리 노드 클래스 */
|
||||
class TreeNode(val _val: Int) { // 노드 값
|
||||
var height: Int = 0 // 노드 높이
|
||||
var left: TreeNode? = null // 왼쪽 자식 노드
|
||||
var right: TreeNode? = null // 오른쪽 자식 노드
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```ruby title=""
|
||||
### AVL 트리 노드 클래스 ###
|
||||
class TreeNode
|
||||
attr_accessor :val # 노드 값
|
||||
attr_accessor :height # 노드 높이
|
||||
attr_accessor :left # 왼쪽 자식 노드 참조
|
||||
attr_accessor :right # 오른쪽 자식 노드 참조
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
@@ -130,25 +130,7 @@ impl TreeNode {
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* 이진 트리 노드 구조체 */
|
||||
typedef struct TreeNode {
|
||||
int val; // 노드 값
|
||||
int height; // 노드 높이
|
||||
struct TreeNode *left; // 왼쪽 자식 노드 포인터
|
||||
struct TreeNode *right; // 오른쪽 자식 노드 포인터
|
||||
} TreeNode;
|
||||
|
||||
/* 생성자 */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 이진 트리 노드 클래스 */
|
||||
public class TreeNode {
|
||||
public var val: Int // 노드 값
|
||||
public var height: Int // 노드 높이
|
||||
public var left: TreeNode? // 왼쪽 자식 노드 참조
|
||||
public var val: Int // 노드 값
|
||||
public var height: Int // 노드 높이
|
||||
public var left: TreeNode? // 왼쪽 자식 노드 참조
|
||||
public var right: TreeNode? // 오른쪽 자식 노드 참조
|
||||
|
||||
/* 생성자 */
|
||||
@@ -16,6 +17,7 @@ public class TreeNode {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 직렬화 인코딩 규칙은 다음을 참고한다:
|
||||
// https://www.hello-algo.com/ko/chapter_tree/array_representation_of_tree/
|
||||
@@ -68,4 +70,6 @@ public class TreeNode {
|
||||
treeToListDFS(root: root, i: 0, res: &res)
|
||||
return res
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 이진 트리 노드 */
|
||||
class TreeNode {
|
||||
val: number; // 노드 값
|
||||
@@ -22,6 +23,7 @@ class TreeNode {
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* 배열을 역직렬화하여 이진 트리로 변환한다 */
|
||||
function arrToTree(arr: (number | null)[], i: number = 0): TreeNode | null {
|
||||
|
||||
@@ -18,7 +18,7 @@ AVL 트리는 이진 탐색 트리이면서 균형 이진 트리다. 두 종류
|
||||
|
||||
### 노드 높이
|
||||
|
||||
AVL 트리 연산에는 노드 높이가 필요하므로 노드 클래스에 `height` 변수를 추가해야 한다.
|
||||
AVL 트리 연산에는 노드 높이가 필요하므로 노드 클래스에 `height` 변수를 추가해야 한다. 이 책의 소스 코드는 모든 장이 같은 이진 트리 노드 클래스를 공유하며, 이 클래스에는 이미 `height` 변수가 포함되어 있다.
|
||||
|
||||
```snippet
|
||||
avl_tree_node
|
||||
|
||||
@@ -15,6 +15,7 @@ extern "C" {
|
||||
|
||||
#define MAX_NODE_SIZE 5000
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Структура узла двоичного дерева */
|
||||
typedef struct TreeNode {
|
||||
int val; // Значение узла
|
||||
@@ -34,6 +35,7 @@ TreeNode *newTreeNode(int val) {
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// Правила кодирования сериализации см.:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -11,17 +11,19 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Структура узла двоичного дерева */
|
||||
struct TreeNode {
|
||||
int val{};
|
||||
int height = 0;
|
||||
int val{}; // Значение узла
|
||||
int height = 0; // Высота узла
|
||||
TreeNode *parent{};
|
||||
TreeNode *left{};
|
||||
TreeNode *right{};
|
||||
TreeNode *left{}; // Указатель на левый дочерний узел
|
||||
TreeNode *right{}; // Указатель на правый дочерний узел
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {
|
||||
}
|
||||
};
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// Правила кодирования сериализации см.:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
|
||||
namespace hello_algo.utils;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Класс узла двоичного дерева */
|
||||
public class TreeNode(int? x) {
|
||||
public int? val = x; // Значение узла
|
||||
public int height; // Высота узла
|
||||
public TreeNode? left; // Ссылка на левый дочерний узел
|
||||
public TreeNode? right; // Ссылка на правый дочерний узел
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// Правила кодирования сериализации см.:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -64,4 +66,6 @@ public class TreeNode(int? x) {
|
||||
TreeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Класс узла двоичного дерева */
|
||||
class TreeNode {
|
||||
int val; // Значение узла
|
||||
@@ -14,6 +15,7 @@ class TreeNode {
|
||||
/* Конструктор */
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* Десериализовать список в двоичное дерево: рекурсия */
|
||||
TreeNode? listToTreeDFS(List<int?> arr, int i) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package pkg
|
||||
|
||||
// [region]{tree_node}
|
||||
// TreeNode: узел двоичного дерева
|
||||
type TreeNode struct {
|
||||
Val any // Значение узла
|
||||
@@ -22,6 +23,8 @@ func NewTreeNode(v any) *TreeNode {
|
||||
}
|
||||
}
|
||||
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// Правила кодирования сериализации см.:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
// Представление двоичного дерева массивом:
|
||||
|
||||
@@ -8,17 +8,19 @@ package utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Класс узла двоичного дерева */
|
||||
public class TreeNode {
|
||||
public int val; // Значение узла
|
||||
public int height; // Высота узла
|
||||
public TreeNode left; // Ссылка на левый дочерний узел
|
||||
public int val; // Значение узла
|
||||
public int height; // Высота узла
|
||||
public TreeNode left; // Ссылка на левый дочерний узел
|
||||
public TreeNode right; // Ссылка на правый дочерний узел
|
||||
|
||||
/* Конструктор */
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// Правила кодирования сериализации см.:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -70,4 +72,6 @@ public class TreeNode {
|
||||
treeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Узел двоичного дерева */
|
||||
class TreeNode {
|
||||
val; // Значение узла
|
||||
@@ -17,6 +18,7 @@ class TreeNode {
|
||||
this.height = height === undefined ? 0 : height;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* Десериализовать массив в двоичное дерево */
|
||||
function arrToTree(arr, i = 0) {
|
||||
|
||||
@@ -6,14 +6,16 @@
|
||||
|
||||
package utils
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Класс узла двоичного дерева */
|
||||
/* Конструктор */
|
||||
class TreeNode(
|
||||
var _val: Int // Значение узла
|
||||
) {
|
||||
var height: Int = 0 // Высота узла
|
||||
var left: TreeNode? = null // Ссылка на левый дочерний узел
|
||||
var height: Int = 0 // Высота узла
|
||||
var left: TreeNode? = null // Ссылка на левый дочерний узел
|
||||
var right: TreeNode? = null // Ссылка на правый дочерний узел
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// Правила кодирования сериализации см.:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -66,4 +68,6 @@ class TreeNode(
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -7,6 +7,7 @@ Author: krahets (krahets@163.com)
|
||||
from collections import deque
|
||||
|
||||
|
||||
# [region]{tree_node}
|
||||
class TreeNode:
|
||||
"""Класс узла двоичного дерева"""
|
||||
|
||||
@@ -15,6 +16,7 @@ class TreeNode:
|
||||
self.height: int = 0 # Высота узла
|
||||
self.left: TreeNode | None = None # Ссылка на левый дочерний узел
|
||||
self.right: TreeNode | None = None # Ссылка на правый дочерний узел
|
||||
# [endregion]{tree_node}
|
||||
|
||||
# Правила кодирования сериализации см.:
|
||||
# https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -4,6 +4,7 @@ Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# [region]{tree_node}
|
||||
### Класс узла двоичного дерева ###
|
||||
class TreeNode
|
||||
attr_accessor :val # Значение узла
|
||||
@@ -16,6 +17,7 @@ class TreeNode
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
# [endregion]{tree_node}
|
||||
|
||||
### Десериализация списка в двоичное дерево: рекурсия ###
|
||||
def arr_to_tree_dfs(arr, i)
|
||||
|
||||
@@ -145,7 +145,11 @@ pub fn main() {
|
||||
/* Операция поиска */
|
||||
// Ввести в хеш-таблицу ключ key и получить значение value
|
||||
println!(
|
||||
"\nПо номеру 13276 найдено имя {}",\nmatch map.get(13276) {\n Some(value) => value,\n None => "Not a valid Key",\n}
|
||||
"\nПо номеру 13276 найдено имя {}",
|
||||
match map.get(13276) {
|
||||
Some(value) => value,
|
||||
None => "Not a valid Key",
|
||||
}
|
||||
);
|
||||
|
||||
/* Операция удаления */
|
||||
|
||||
@@ -144,7 +144,13 @@ fn main() {
|
||||
let r = abt.right(i);
|
||||
let p = abt.parent(i);
|
||||
println!(
|
||||
"\nТекущий индекс узла = {}, значение = {}",\ni,\nif let Some(val) = abt.val(i) {\n format!("{val}")\n} else {\n "null".to_string()\n}
|
||||
"\nТекущий индекс узла = {}, значение = {}",
|
||||
i,
|
||||
if let Some(val) = abt.val(i) {
|
||||
format!("{val}")
|
||||
} else {
|
||||
"null".to_string()
|
||||
}
|
||||
);
|
||||
println!(
|
||||
"Индекс левого дочернего узла = {}, значение = {}",
|
||||
|
||||
@@ -289,7 +289,9 @@ fn main() {
|
||||
let node = avl_tree.search(7);
|
||||
if let Some(node) = node {
|
||||
println!(
|
||||
"\nНайденный объект узла = {:?}, значение узла = {}",\n&*node.borrow(),\nnode.borrow().val
|
||||
"\nНайденный объект узла = {:?}, значение узла = {}",
|
||||
&*node.borrow(),
|
||||
node.borrow().val
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -172,7 +172,9 @@ fn main() {
|
||||
/* Найти узел */
|
||||
let node = bst.search(7);
|
||||
println!(
|
||||
"\nНайденный объект узла = {:?}, значение узла = {}",\nnode.clone().unwrap(),\nnode.clone().unwrap().borrow().val
|
||||
"\nНайденный объект узла = {:?}, значение узла = {}",
|
||||
node.clone().unwrap(),
|
||||
node.clone().unwrap().borrow().val
|
||||
);
|
||||
|
||||
/* Вставка узла */
|
||||
|
||||
@@ -4,17 +4,20 @@
|
||||
* Author: xBLACKICEx (xBLACKICE@outlook.com), night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{import}
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
// [endregion]{import}
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Тип узла двоичного дерева */
|
||||
#[derive(Debug)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub height: i32,
|
||||
pub val: i32, // Значение узла
|
||||
pub height: i32, // Высота узла
|
||||
pub parent: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>, // Указатель на левый дочерний узел
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>, // Указатель на правый дочерний узел
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
@@ -29,6 +32,7 @@ impl TreeNode {
|
||||
}))
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! op_vec {
|
||||
|
||||
@@ -1,182 +1,58 @@
|
||||
<!-- [snippet]{avl_tree_node} -->
|
||||
```python title=""
|
||||
class TreeNode:
|
||||
"""Класс узла AVL-дерева"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # Значение узла
|
||||
self.height: int = 0 # Высота узла
|
||||
self.left: TreeNode | None = None # Ссылка на левого дочернего узла
|
||||
self.right: TreeNode | None = None # Ссылка на правого дочернего узла
|
||||
[file]{modules/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```cpp title=""
|
||||
/* Класс узла AVL-дерева */
|
||||
struct TreeNode {
|
||||
int val{}; // Значение узла
|
||||
int height = 0; // Высота узла
|
||||
TreeNode *left{}; // Левый дочерний узел
|
||||
TreeNode *right{}; // Правый дочерний узел
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x) : val(x){}
|
||||
};
|
||||
[file]{utils/tree_node.hpp}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```java title=""
|
||||
/* Класс узла AVL-дерева */
|
||||
class TreeNode {
|
||||
public int val; // Значение узла
|
||||
public int height; // Высота узла
|
||||
public TreeNode left; // Левый дочерний узел
|
||||
public TreeNode right; // Правый дочерний узел
|
||||
public TreeNode(int x) { val = x; }
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```csharp title=""
|
||||
/* Класс узла AVL-дерева */
|
||||
class TreeNode(int? x) {
|
||||
public int? val = x; // Значение узла
|
||||
public int height; // Высота узла
|
||||
public TreeNode? left; // Ссылка на левого дочернего узла
|
||||
public TreeNode? right; // Ссылка на правого дочернего узла
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```go title=""
|
||||
/* Структура узла AVL-дерева */
|
||||
type TreeNode struct {
|
||||
Val int // Значение узла
|
||||
Height int // Высота узла
|
||||
Left *TreeNode // Ссылка на левого дочернего узла
|
||||
Right *TreeNode // Ссылка на правого дочернего узла
|
||||
}
|
||||
[file]{pkg/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```swift title=""
|
||||
/* Класс узла AVL-дерева */
|
||||
class TreeNode {
|
||||
var val: Int // Значение узла
|
||||
var height: Int // Высота узла
|
||||
var left: TreeNode? // Левый дочерний узел
|
||||
var right: TreeNode? // Правый дочерний узел
|
||||
|
||||
init(x: Int) {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```javascript title=""
|
||||
/* Класс узла AVL-дерева */
|
||||
class TreeNode {
|
||||
val; // Значение узла
|
||||
height; // Высота узла
|
||||
left; // Указатель на левого дочернего узла
|
||||
right; // Указатель на правого дочернего узла
|
||||
constructor(val, left, right, height) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```typescript title=""
|
||||
/* Класс узла AVL-дерева */
|
||||
class TreeNode {
|
||||
val: number; // Значение узла
|
||||
height: number; // Высота узла
|
||||
left: TreeNode | null; // Указатель на левого дочернего узла
|
||||
right: TreeNode | null; // Указатель на правого дочернего узла
|
||||
constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```dart title=""
|
||||
/* Класс узла AVL-дерева */
|
||||
class TreeNode {
|
||||
int val; // Значение узла
|
||||
int height; // Высота узла
|
||||
TreeNode? left; // Левый дочерний узел
|
||||
TreeNode? right; // Правый дочерний узел
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```rust title=""
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
[file]{src/include/tree_node}-[region]{import}
|
||||
|
||||
/* Структура узла AVL-дерева */
|
||||
struct TreeNode {
|
||||
val: i32, // Значение узла
|
||||
height: i32, // Высота узла
|
||||
left: Option<Rc<RefCell<TreeNode>>>, // Левый дочерний узел
|
||||
right: Option<Rc<RefCell<TreeNode>>>, // Правый дочерний узел
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
/* Конструктор */
|
||||
fn new(val: i32) -> Rc<RefCell<Self>> {
|
||||
Rc::new(RefCell::new(Self {
|
||||
val,
|
||||
height: 0,
|
||||
left: None,
|
||||
right: None
|
||||
}))
|
||||
}
|
||||
}
|
||||
[file]{src/include/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* Структура узла AVL-дерева */
|
||||
typedef struct TreeNode {
|
||||
int val;
|
||||
int height;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
} TreeNode;
|
||||
|
||||
/* Конструктор */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
/* Класс узла AVL-дерева */
|
||||
class TreeNode(val _val: Int) { // Значение узла
|
||||
var height: Int = 0 // Высота узла
|
||||
var left: TreeNode? = null // Левый дочерний узел
|
||||
var right: TreeNode? = null // Правый дочерний узел
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```ruby title=""
|
||||
### Класс узла AVL-дерева ###
|
||||
class TreeNode
|
||||
attr_accessor :val # Значение узла
|
||||
attr_accessor :height # Высота узла
|
||||
attr_accessor :left # Ссылка на левого дочернего узла
|
||||
attr_accessor :right # Ссылка на правого дочернего узла
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
@@ -130,25 +130,7 @@ impl TreeNode {
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* Структура узла двоичного дерева */
|
||||
typedef struct TreeNode {
|
||||
int val; // Значение узла
|
||||
int height; // Высота узла
|
||||
struct TreeNode *left; // Указатель на левого дочернего узла
|
||||
struct TreeNode *right; // Указатель на правого дочернего узла
|
||||
} TreeNode;
|
||||
|
||||
/* Конструктор */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Класс узла двоичного дерева */
|
||||
public class TreeNode {
|
||||
public var val: Int // Значение узла
|
||||
public var height: Int // Высота узла
|
||||
public var left: TreeNode? // Ссылка на левый дочерний узел
|
||||
public var val: Int // Значение узла
|
||||
public var height: Int // Высота узла
|
||||
public var left: TreeNode? // Ссылка на левый дочерний узел
|
||||
public var right: TreeNode? // Ссылка на правый дочерний узел
|
||||
|
||||
/* Конструктор */
|
||||
@@ -16,6 +17,7 @@ public class TreeNode {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// Правила кодирования сериализации см.:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -68,4 +70,6 @@ public class TreeNode {
|
||||
treeToListDFS(root: root, i: 0, res: &res)
|
||||
return res
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* Узел двоичного дерева */
|
||||
class TreeNode {
|
||||
val: number; // Значение узла
|
||||
@@ -22,6 +23,7 @@ class TreeNode {
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* Десериализовать массив в двоичное дерево */
|
||||
function arrToTree(arr: (number | null)[], i: number = 0): TreeNode | null {
|
||||
|
||||
@@ -18,7 +18,7 @@ AVL-дерево одновременно является и двоичным
|
||||
|
||||
### Высота узла
|
||||
|
||||
Поскольку операции AVL-дерева требуют получать высоту узла, нам нужно добавить в класс узла переменную `height` :
|
||||
Поскольку операции AVL-дерева требуют получать высоту узла, нам нужно добавить в класс узла переменную `height`. Во всех главах исходного кода книги используется один и тот же класс узла двоичного дерева, и он уже содержит эту переменную:
|
||||
|
||||
```snippet
|
||||
avl_tree_node
|
||||
|
||||
@@ -15,6 +15,7 @@ extern "C" {
|
||||
|
||||
#define MAX_NODE_SIZE 5000
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二元樹節點結構體 */
|
||||
typedef struct TreeNode {
|
||||
int val; // 節點值
|
||||
@@ -34,6 +35,7 @@ TreeNode *newTreeNode(int val) {
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化編碼規則請參考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -11,17 +11,19 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二元樹節點結構體 */
|
||||
struct TreeNode {
|
||||
int val{};
|
||||
int height = 0;
|
||||
int val{}; // 節點值
|
||||
int height = 0; // 節點高度
|
||||
TreeNode *parent{};
|
||||
TreeNode *left{};
|
||||
TreeNode *right{};
|
||||
TreeNode *left{}; // 左子節點指標
|
||||
TreeNode *right{}; // 右子節點指標
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x, TreeNode *parent = nullptr) : val(x), parent(parent) {
|
||||
}
|
||||
};
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化編碼規則請參考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
|
||||
namespace hello_algo.utils;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二元樹節點類別 */
|
||||
public class TreeNode(int? x) {
|
||||
public int? val = x; // 節點值
|
||||
public int height; // 節點高度
|
||||
public TreeNode? left; // 左子節點引用
|
||||
public TreeNode? right; // 右子節點引用
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化編碼規則請參考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -64,4 +66,6 @@ public class TreeNode(int? x) {
|
||||
TreeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二元樹節點類別 */
|
||||
class TreeNode {
|
||||
int val; // 節點值
|
||||
@@ -14,6 +15,7 @@ class TreeNode {
|
||||
/* 建構子 */
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* 將串列反序列化為二元樹:遞迴 */
|
||||
TreeNode? listToTreeDFS(List<int?> arr, int i) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
package pkg
|
||||
|
||||
// [region]{tree_node}
|
||||
// TreeNode 二元樹節點
|
||||
type TreeNode struct {
|
||||
Val any // 節點值
|
||||
@@ -22,6 +23,8 @@ func NewTreeNode(v any) *TreeNode {
|
||||
}
|
||||
}
|
||||
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化編碼規則請參考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
// 二元樹的陣列表示:
|
||||
|
||||
@@ -8,17 +8,19 @@ package utils;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二元樹節點類別 */
|
||||
public class TreeNode {
|
||||
public int val; // 節點值
|
||||
public int height; // 節點高度
|
||||
public TreeNode left; // 左子節點引用
|
||||
public int val; // 節點值
|
||||
public int height; // 節點高度
|
||||
public TreeNode left; // 左子節點引用
|
||||
public TreeNode right; // 右子節點引用
|
||||
|
||||
/* 建構子 */
|
||||
public TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化編碼規則請參考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -70,4 +72,6 @@ public class TreeNode {
|
||||
treeToListDFS(root, 0, res);
|
||||
return res;
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
* Author: IsChristina (christinaxia77@foxmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二元樹節點 */
|
||||
class TreeNode {
|
||||
val; // 節點值
|
||||
left; // 左子節點指標
|
||||
right; // 右子節點指標
|
||||
height; //節點高度
|
||||
height; // 節點高度
|
||||
constructor(val, left, right, height) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.left = left === undefined ? null : left;
|
||||
@@ -17,6 +18,7 @@ class TreeNode {
|
||||
this.height = height === undefined ? 0 : height;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* 將陣列反序列化為二元樹 */
|
||||
function arrToTree(arr, i = 0) {
|
||||
|
||||
@@ -6,14 +6,16 @@
|
||||
|
||||
package utils
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二元樹節點類別 */
|
||||
/* 建構子 */
|
||||
class TreeNode(
|
||||
var _val: Int // 節點值
|
||||
) {
|
||||
var height: Int = 0 // 節點高度
|
||||
var left: TreeNode? = null // 左子節點引用
|
||||
var height: Int = 0 // 節點高度
|
||||
var left: TreeNode? = null // 左子節點引用
|
||||
var right: TreeNode? = null // 右子節點引用
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化編碼規則請參考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -66,4 +68,6 @@ class TreeNode(
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -7,6 +7,7 @@ Author: krahets (krahets@163.com)
|
||||
from collections import deque
|
||||
|
||||
|
||||
# [region]{tree_node}
|
||||
class TreeNode:
|
||||
"""二元樹節點類別"""
|
||||
|
||||
@@ -15,6 +16,7 @@ class TreeNode:
|
||||
self.height: int = 0 # 節點高度
|
||||
self.left: TreeNode | None = None # 左子節點引用
|
||||
self.right: TreeNode | None = None # 右子節點引用
|
||||
# [endregion]{tree_node}
|
||||
|
||||
# 序列化編碼規則請參考:
|
||||
# https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
|
||||
@@ -4,6 +4,7 @@ Created Time: 2024-03-30
|
||||
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
||||
=end
|
||||
|
||||
# [region]{tree_node}
|
||||
### 二元樹節點類別 ###
|
||||
class TreeNode
|
||||
attr_accessor :val # 節點值
|
||||
@@ -16,6 +17,7 @@ class TreeNode
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
# [endregion]{tree_node}
|
||||
|
||||
### 將串列反序列化為二元數樹:遞迴 ###
|
||||
def arr_to_tree_dfs(arr, i)
|
||||
|
||||
@@ -4,17 +4,20 @@
|
||||
* Author: xBLACKICEx (xBLACKICE@outlook.com), night-cruise (2586447362@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{import}
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
// [endregion]{import}
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二元樹節點型別 */
|
||||
#[derive(Debug)]
|
||||
pub struct TreeNode {
|
||||
pub val: i32,
|
||||
pub height: i32,
|
||||
pub val: i32, // 節點值
|
||||
pub height: i32, // 節點高度
|
||||
pub parent: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>,
|
||||
pub left: Option<Rc<RefCell<TreeNode>>>, // 左子節點指標
|
||||
pub right: Option<Rc<RefCell<TreeNode>>>, // 右子節點指標
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
@@ -29,6 +32,7 @@ impl TreeNode {
|
||||
}))
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! op_vec {
|
||||
|
||||
@@ -1,182 +1,58 @@
|
||||
<!-- [snippet]{avl_tree_node} -->
|
||||
```python title=""
|
||||
class TreeNode:
|
||||
"""AVL 樹節點類別"""
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 節點值
|
||||
self.height: int = 0 # 節點高度
|
||||
self.left: TreeNode | None = None # 左子節點引用
|
||||
self.right: TreeNode | None = None # 右子節點引用
|
||||
[file]{modules/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```cpp title=""
|
||||
/* AVL 樹節點類別 */
|
||||
struct TreeNode {
|
||||
int val{}; // 節點值
|
||||
int height = 0; // 節點高度
|
||||
TreeNode *left{}; // 左子節點
|
||||
TreeNode *right{}; // 右子節點
|
||||
TreeNode() = default;
|
||||
explicit TreeNode(int x) : val(x){}
|
||||
};
|
||||
[file]{utils/tree_node.hpp}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```java title=""
|
||||
/* AVL 樹節點類別 */
|
||||
class TreeNode {
|
||||
public int val; // 節點值
|
||||
public int height; // 節點高度
|
||||
public TreeNode left; // 左子節點
|
||||
public TreeNode right; // 右子節點
|
||||
public TreeNode(int x) { val = x; }
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```csharp title=""
|
||||
/* AVL 樹節點類別 */
|
||||
class TreeNode(int? x) {
|
||||
public int? val = x; // 節點值
|
||||
public int height; // 節點高度
|
||||
public TreeNode? left; // 左子節點引用
|
||||
public TreeNode? right; // 右子節點引用
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```go title=""
|
||||
/* AVL 樹節點結構體 */
|
||||
type TreeNode struct {
|
||||
Val int // 節點值
|
||||
Height int // 節點高度
|
||||
Left *TreeNode // 左子節點引用
|
||||
Right *TreeNode // 右子節點引用
|
||||
}
|
||||
[file]{pkg/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```swift title=""
|
||||
/* AVL 樹節點類別 */
|
||||
class TreeNode {
|
||||
var val: Int // 節點值
|
||||
var height: Int // 節點高度
|
||||
var left: TreeNode? // 左子節點
|
||||
var right: TreeNode? // 右子節點
|
||||
|
||||
init(x: Int) {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```javascript title=""
|
||||
/* AVL 樹節點類別 */
|
||||
class TreeNode {
|
||||
val; // 節點值
|
||||
height; //節點高度
|
||||
left; // 左子節點指標
|
||||
right; // 右子節點指標
|
||||
constructor(val, left, right, height) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```typescript title=""
|
||||
/* AVL 樹節點類別 */
|
||||
class TreeNode {
|
||||
val: number; // 節點值
|
||||
height: number; // 節點高度
|
||||
left: TreeNode | null; // 左子節點指標
|
||||
right: TreeNode | null; // 右子節點指標
|
||||
constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
[file]{modules/TreeNode}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```dart title=""
|
||||
/* AVL 樹節點類別 */
|
||||
class TreeNode {
|
||||
int val; // 節點值
|
||||
int height; // 節點高度
|
||||
TreeNode? left; // 左子節點
|
||||
TreeNode? right; // 右子節點
|
||||
TreeNode(this.val, [this.height = 0, this.left, this.right]);
|
||||
}
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```rust title=""
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
[file]{src/include/tree_node}-[region]{import}
|
||||
|
||||
/* AVL 樹節點結構體 */
|
||||
struct TreeNode {
|
||||
val: i32, // 節點值
|
||||
height: i32, // 節點高度
|
||||
left: Option<Rc<RefCell<TreeNode>>>, // 左子節點
|
||||
right: Option<Rc<RefCell<TreeNode>>>, // 右子節點
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
/* 建構子 */
|
||||
fn new(val: i32) -> Rc<RefCell<Self>> {
|
||||
Rc::new(RefCell::new(Self {
|
||||
val,
|
||||
height: 0,
|
||||
left: None,
|
||||
right: None
|
||||
}))
|
||||
}
|
||||
}
|
||||
[file]{src/include/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* AVL 樹節點結構體 */
|
||||
typedef struct TreeNode {
|
||||
int val;
|
||||
int height;
|
||||
struct TreeNode *left;
|
||||
struct TreeNode *right;
|
||||
} TreeNode;
|
||||
|
||||
/* 建構子 */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
/* AVL 樹節點類別 */
|
||||
class TreeNode(val _val: Int) { // 節點值
|
||||
var height: Int = 0 // 節點高度
|
||||
var left: TreeNode? = null // 左子節點
|
||||
var right: TreeNode? = null // 右子節點
|
||||
}
|
||||
[file]{utils/TreeNode}-[region]{tree_node}
|
||||
[file]{utils/TreeNode}-[region]{tree_node_end}
|
||||
```
|
||||
|
||||
```ruby title=""
|
||||
### AVL 樹節點類別 ###
|
||||
class TreeNode
|
||||
attr_accessor :val # 節點值
|
||||
attr_accessor :height # 節點高度
|
||||
attr_accessor :left # 左子節點引用
|
||||
attr_accessor :right # 右子節點引用
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
[file]{utils/tree_node}-[region]{tree_node}
|
||||
```
|
||||
|
||||
@@ -130,25 +130,7 @@ impl TreeNode {
|
||||
```
|
||||
|
||||
```c title=""
|
||||
/* 二元樹節點結構體 */
|
||||
typedef struct TreeNode {
|
||||
int val; // 節點值
|
||||
int height; // 節點高度
|
||||
struct TreeNode *left; // 左子節點指標
|
||||
struct TreeNode *right; // 右子節點指標
|
||||
} TreeNode;
|
||||
|
||||
/* 建構子 */
|
||||
TreeNode *newTreeNode(int val) {
|
||||
TreeNode *node;
|
||||
|
||||
node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||
node->val = val;
|
||||
node->height = 0;
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
return node;
|
||||
}
|
||||
[file]{utils/tree_node.h}-[region]{tree_node}
|
||||
```
|
||||
|
||||
```kotlin title=""
|
||||
|
||||
@@ -4,11 +4,12 @@
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二元樹節點類別 */
|
||||
public class TreeNode {
|
||||
public var val: Int // 節點值
|
||||
public var height: Int // 節點高度
|
||||
public var left: TreeNode? // 左子節點引用
|
||||
public var val: Int // 節點值
|
||||
public var height: Int // 節點高度
|
||||
public var left: TreeNode? // 左子節點引用
|
||||
public var right: TreeNode? // 右子節點引用
|
||||
|
||||
/* 建構子 */
|
||||
@@ -16,6 +17,7 @@ public class TreeNode {
|
||||
val = x
|
||||
height = 0
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
// 序列化編碼規則請參考:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
@@ -68,4 +70,6 @@ public class TreeNode {
|
||||
treeToListDFS(root: root, i: 0, res: &res)
|
||||
return res
|
||||
}
|
||||
// [region]{tree_node_end}
|
||||
}
|
||||
// [endregion]{tree_node_end}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
* Author: Justin (xiefahit@gmail.com)
|
||||
*/
|
||||
|
||||
// [region]{tree_node}
|
||||
/* 二元樹節點 */
|
||||
class TreeNode {
|
||||
val: number; // 節點值
|
||||
@@ -22,6 +23,7 @@ class TreeNode {
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
// [endregion]{tree_node}
|
||||
|
||||
/* 將陣列反序列化為二元樹 */
|
||||
function arrToTree(arr: (number | null)[], i: number = 0): TreeNode | null {
|
||||
|
||||
@@ -18,7 +18,7 @@ AVL 樹既是二元搜尋樹,也是平衡二元樹,同時滿足這兩類二
|
||||
|
||||
### 節點高度
|
||||
|
||||
由於 AVL 樹的相關操作需要獲取節點高度,因此我們需要為節點類別新增 `height` 變數:
|
||||
由於 AVL 樹的相關操作需要獲取節點高度,因此我們需要為節點類別新增 `height` 變數。本書各章的原始碼共用同一個二元樹節點類別,它已經包含該變數:
|
||||
|
||||
```snippet
|
||||
avl_tree_node
|
||||
|
||||
Reference in New Issue
Block a user