Add missing Dart codes and fix some errors (#689)

* Add missing Dart codes and fix some errors

* Update array_binary_tree.dart

---------

Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
liuyuxin
2023-08-17 05:04:38 +08:00
committed by GitHub
parent 5d7e0a59b1
commit 0858ab91c0
14 changed files with 504 additions and 204 deletions
@@ -0,0 +1,152 @@
/**
* File: array_binary_tree.dart
* Created Time: 2023-08-15
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import '../utils/print_util.dart';
import '../utils/tree_node.dart';
/* 数组表示下的二叉树类 */
class ArrayBinaryTree {
late List<int?> _tree;
/* 构造方法 */
ArrayBinaryTree(this._tree);
/* 节点数量 */
int size() {
return _tree.length;
}
/* 获取索引为 i 节点的值 */
int? val(int i) {
// 若索引越界,则返回 null ,代表空位
if (i < 0 || i >= size()) {
return null;
}
return _tree[i];
}
/* 获取索引为 i 节点的左子节点的索引 */
int? left(int i) {
return 2 * i + 1;
}
/* 获取索引为 i 节点的右子节点的索引 */
int? right(int i) {
return 2 * i + 2;
}
/* 获取索引为 i 节点的父节点的索引 */
int? parent(int i) {
return (i - 1) ~/ 2;
}
/* 层序遍历 */
List<int> levelOrder() {
List<int> res = [];
for (int i = 0; i < size(); i++) {
if (val(i) != null) {
res.add(val(i)!);
}
}
return res;
}
/* 深度优先遍历 */
void dfs(int i, String order, List<int?> res) {
// 若为空位,则返回
if (val(i) == null) {
return;
}
// 前序遍历
if (order == 'pre') {
res.add(val(i));
}
dfs(left(i)!, order, res);
// 中序遍历
if (order == 'in') {
res.add(val(i));
}
dfs(right(i)!, order, res);
// 后序遍历
if (order == 'post') {
res.add(val(i));
}
}
/* 前序遍历 */
List<int?> preOrder() {
List<int?> res = [];
dfs(0, 'pre', res);
return res;
}
/* 中序遍历 */
List<int?> inOrder() {
List<int?> res = [];
dfs(0, 'in', res);
return res;
}
/* 后序遍历 */
List<int?> postOrder() {
List<int?> res = [];
dfs(0, 'post', res);
return res;
}
}
/* Driver Code */
void main() {
// 初始化二叉树
// 这里借助了一个从数组直接生成二叉树的函数
List<int?> arr = [
1,
2,
3,
4,
null,
6,
7,
8,
9,
null,
null,
12,
null,
null,
15
];
TreeNode? root = listToTree(arr);
print("\n初始化二叉树\n");
print("二叉树的数组表示:");
print(arr);
print("二叉树的链表表示:");
printTree(root);
// 数组表示下的二叉树类
ArrayBinaryTree abt = ArrayBinaryTree(arr);
// 访问节点
int i = 1;
int? l = abt.left(i);
int? r = abt.right(i);
int? p = abt.parent(i);
print("\n当前节点的索引为 $i ,值为 ${abt.val(i)}");
print("其左子节点的索引为 $l ,值为 ${(l == null ? "null" : abt.val(l))}");
print("其右子节点的索引为 $r ,值为 ${(r == null ? "null" : abt.val(r))}");
print("其父节点的索引为 $p ,值为 ${(p == null ? "null" : abt.val(p))}");
// 遍历树
List<int?> res = abt.levelOrder();
print("\n层序遍历为:$res");
res = abt.preOrder();
print("前序遍历为 $res");
res = abt.inOrder();
print("中序遍历为 $res");
res = abt.postOrder();
print("后序遍历为 $res");
}
+1
View File
@@ -18,6 +18,7 @@ class AVLTree {
/* 获取节点高度 */
int height(TreeNode? node) {
// 空节点高度为 -1 ,叶节点高度为 0
return node == null ? -1 : node.height;
}
+119 -116
View File
@@ -8,122 +8,125 @@ import '../utils/print_util.dart';
import '../utils/tree_node.dart';
/* 二叉搜索树 */
TreeNode? root;
class BinarySearchTree {
late TreeNode? _root;
void binarySearchTree(List<int> nums) {
nums.sort(); // 排序数组
root = buildTree(nums, 0, nums.length - 1); // 构建二叉搜索树
}
/* 获取二叉树的根节点 */
TreeNode? getRoot() {
return root;
}
/* 构建二叉上搜索树 */
TreeNode? buildTree(List<int> nums, int i, int j) {
if (i > j) {
return null;
/* 构造方法 */
BinarySearchTree(List<int> nums) {
nums.sort(); // 排序数组
_root = buildTree(nums, 0, nums.length - 1); // 构建二叉搜索树
}
// 将数组中间节点作为根节点
int mid = (i + j) ~/ 2;
TreeNode? root = TreeNode(nums[mid]);
root.left = buildTree(nums, i, mid - 1);
root.right = buildTree(nums, mid + 1, j);
return root;
}
/* 查找节点 */
TreeNode? search(int num) {
TreeNode? cur = root;
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 目标节点在 cur 的右子树中
if (cur.val < num)
cur = cur.right;
// 目标节点在 cur 的左子树中
else if (cur.val > num)
cur = cur.left;
// 找到目标节点,跳出循环
/* 获取二叉树的根节点 */
TreeNode? getRoot() {
return _root;
}
/* 构建二叉上搜索树 */
TreeNode? buildTree(List<int> nums, int i, int j) {
if (i > j) {
return null;
}
// 将数组中间节点作为根节点
int mid = (i + j) ~/ 2;
TreeNode? root = TreeNode(nums[mid]);
root.left = buildTree(nums, i, mid - 1);
root.right = buildTree(nums, mid + 1, j);
return root;
}
/* 查找节点 */
TreeNode? search(int num) {
TreeNode? cur = _root;
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 目标节点在 cur 的右子树中
if (cur.val < num)
cur = cur.right;
// 目标节点在 cur 的左子树中
else if (cur.val > num)
cur = cur.left;
// 找到目标节点,跳出循环
else
break;
}
// 返回目标节点
return cur;
}
/* 插入节点 */
void insert(int num) {
// 若树为空,直接提前返回
if (_root == null) return;
TreeNode? cur = _root;
TreeNode? pre = null;
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 找到重复节点,直接返回
if (cur.val == num) return;
pre = cur;
// 插入位置在 cur 的右子树中
if (cur.val < num)
cur = cur.right;
// 插入位置在 cur 的左子树中
else
cur = cur.left;
}
// 插入节点
TreeNode? node = TreeNode(num);
if (pre!.val < num)
pre.right = node;
else
break;
pre.left = node;
}
// 返回目标节点
return cur;
}
/* 插入节点 */
void insert(int num) {
// 若树为空,直接提前返回
if (root == null) return;
TreeNode? cur = root;
TreeNode? pre = null;
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 找到重复节点,直接返回
if (cur.val == num) return;
pre = cur;
// 插入位置在 cur 的右子树中
if (cur.val < num)
cur = cur.right;
// 插入位置在 cur 的左子树中
else
cur = cur.left;
}
// 插入节点
TreeNode? node = TreeNode(num);
if (pre!.val < num)
pre.right = node;
else
pre.left = node;
}
/* 删除节点 */
void remove(int num) {
// 若树为空,直接提前返回
if (root == null) return;
void remove(int num) {
// 若树为空,直接提前返回
if (_root == null) return;
TreeNode? cur = root;
TreeNode? pre = null;
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 找到待删除节点,跳出循环
if (cur.val == num) break;
pre = cur;
// 待删除节点在 cur 的右子树中
if (cur.val < num)
cur = cur.right;
// 待删除节点在 cur 的左子树中
else
cur = cur.left;
}
// 若无待删除节点,直接返回
if (cur == null) return;
// 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) {
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
TreeNode? child = cur.left ?? cur.right;
// 删除节点 cur
if (cur != root) {
if (pre!.left == cur)
pre.left = child;
TreeNode? cur = _root;
TreeNode? pre = null;
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 找到待删除节点,跳出循环
if (cur.val == num) break;
pre = cur;
// 待删除节点在 cur 的右子树中
if (cur.val < num)
cur = cur.right;
// 待删除节点在 cur 的左子树中
else
pre.right = child;
cur = cur.left;
}
// 若无待删除节点,直接返回
if (cur == null) return;
// 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) {
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
TreeNode? child = cur.left ?? cur.right;
// 删除节点 cur
if (cur != _root) {
if (pre!.left == cur)
pre.left = child;
else
pre.right = child;
} else {
// 若删除节点为根节点,则重新指定根节点
_root = child;
}
} else {
// 若删除节点为根节点,则重新指定根节点
root = child;
// 子节点数量 = 2
// 获取中序遍历中 cur 的下一个节点
TreeNode? tmp = cur.right;
while (tmp!.left != null) {
tmp = tmp.left;
}
// 递归删除节点 tmp
remove(tmp.val);
// 用 tmp 覆盖 cur
cur.val = tmp.val;
}
} else {
// 子节点数量 = 2
// 获取中序遍历中 cur 的下一个节点
TreeNode? tmp = cur.right;
while (tmp!.left != null) {
tmp = tmp.left;
}
// 递归删除节点 tmp
remove(tmp.val);
// 用 tmp 覆盖 cur
cur.val = tmp.val;
}
}
@@ -131,27 +134,27 @@ void remove(int num) {
void main() {
/* 初始化二叉搜索树 */
List<int> nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
binarySearchTree(nums);
BinarySearchTree bst = BinarySearchTree(nums);
print("\n初始化的二叉树为\n");
printTree(getRoot());
printTree(bst.getRoot());
/* 查找节点 */
TreeNode? node = search(7);
TreeNode? node = bst.search(7);
print("\n查找到的节点对象为 $node,节点值 = ${node?.val}");
/* 插入节点 */
insert(16);
bst.insert(16);
print("\n插入节点 16 后,二叉树为\n");
printTree(getRoot());
printTree(bst.getRoot());
/* 删除节点 */
remove(1);
bst.remove(1);
print("\n删除节点 1 后,二叉树为\n");
printTree(getRoot());
remove(2);
printTree(bst.getRoot());
bst.remove(2);
print("\n删除节点 2 后,二叉树为\n");
printTree(getRoot());
remove(4);
printTree(bst.getRoot());
bst.remove(4);
print("\n删除节点 4 后,二叉树为\n");
printTree(getRoot());
printTree(bst.getRoot());
}