feat: add dart code for chapter_tree (#448)

pull/449/head
liuyuxin 2 years ago committed by GitHub
parent 33cc384958
commit 42c89be785
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,226 @@
/**
* File: avl_tree.dart
* Created Time: 2023-04-04
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import 'dart:math';
import '../utils/print_util.dart';
import '../utils/tree_node.dart';
class AVLTree {
TreeNode? root;
/* 构造方法 */
AVLTree() {
root = null;
}
/* 获取结点高度 */
int height(TreeNode? node) {
return node == null ? -1 : node.height;
}
/* 更新结点高度 */
void updateHeight(TreeNode? node) {
// + 1
node!.height = max(height(node.left), height(node.right)) + 1;
}
/* 获取平衡因子 */
int balanceFactor(TreeNode? node) {
// 0
if (node == null) return 0;
// = -
return height(node.left) - height(node.right);
}
/* 右旋操作 */
TreeNode? rightRotate(TreeNode? node) {
TreeNode? child = node!.left;
TreeNode? grandChild = child!.right;
// child node
child.right = node;
node.left = grandChild;
//
updateHeight(node);
updateHeight(child);
//
return child;
}
/* 左旋操作 */
TreeNode? leftRotate(TreeNode? node) {
TreeNode? child = node!.right;
TreeNode? grandChild = child!.left;
// child node
child.left = node;
node.right = grandChild;
//
updateHeight(node);
updateHeight(child);
//
return child;
}
/* 执行旋转操作,使该子树重新恢复平衡 */
TreeNode? rotate(TreeNode? node) {
// node
int factor = balanceFactor(node);
//
if (factor > 1) {
if (balanceFactor(node!.left) >= 0) {
//
return rightRotate(node);
} else {
//
node.left = leftRotate(node.left);
return rightRotate(node);
}
}
//
if (factor < -1) {
if (balanceFactor(node!.right) <= 0) {
//
return leftRotate(node);
} else {
//
node.right = rightRotate(node.right);
return leftRotate(node);
}
}
//
return node;
}
/* 插入结点 */
TreeNode? insert(int val) {
root = insertHelper(root, val);
return root;
}
/* 递归插入结点(辅助方法) */
TreeNode? insertHelper(TreeNode? node, int val) {
if (node == null) return TreeNode(val);
/* 1. 查找插入位置,并插入结点 */
if (val < node.val)
node.left = insertHelper(node.left, val);
else if (val > node.val)
node.right = insertHelper(node.right, val);
else
return node; //
updateHeight(node); //
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
//
return node;
}
/* 删除结点 */
TreeNode? remove(int val) {
root = removeHelper(root, val);
return root;
}
/* 递归删除结点(辅助方法) */
TreeNode? removeHelper(TreeNode? node, int val) {
if (node == null) return null;
/* 1. 查找结点,并删除之 */
if (val < node.val)
node.left = removeHelper(node.left, val);
else if (val > node.val)
node.right = removeHelper(node.right, val);
else {
if (node.left == null || node.right == null) {
TreeNode? child = node.left ?? node.right;
// = 0 node
if (child == null)
return null;
// = 1 node
else
node = child;
} else {
// = 2
TreeNode? temp = getInOrderNext(node.right);
node.right = removeHelper(node.right, temp!.val);
node.val = temp.val;
}
}
updateHeight(node); //
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
node = rotate(node);
//
return node;
}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
TreeNode? getInOrderNext(TreeNode? node) {
if (node == null) return node;
// 访
while (node!.left != null) {
node = node.left;
}
return node;
}
/* 查找结点 */
TreeNode? search(int val) {
TreeNode? cur = root;
//
while (cur != null) {
// cur
if (val < cur.val)
cur = cur.left;
// cur
else if (val > cur.val)
cur = cur.right;
//
else
break;
}
return cur;
}
}
void testInsert(AVLTree tree, int val) {
tree.insert(val);
print("\n插入结点 $valAVL 树为");
printTree(tree.root);
}
void testRemove(AVLTree tree, int val) {
tree.remove(val);
print("\n删除结点 $valAVL 树为");
printTree(tree.root);
}
/* Driver Code */
void main() {
/* 初始化空 AVL 树 */
AVLTree avlTree = AVLTree();
/* 插入结点 */
// AVL
testInsert(avlTree, 1);
testInsert(avlTree, 2);
testInsert(avlTree, 3);
testInsert(avlTree, 4);
testInsert(avlTree, 5);
testInsert(avlTree, 8);
testInsert(avlTree, 7);
testInsert(avlTree, 9);
testInsert(avlTree, 10);
testInsert(avlTree, 6);
/* 插入重复结点 */
testInsert(avlTree, 7);
/* 删除结点 */
// AVL
testRemove(avlTree, 8); // 0
testRemove(avlTree, 5); // 1
testRemove(avlTree, 4); // 2
/* 查询结点 */
TreeNode? node = avlTree.search(7);
print("\n查找到的结点对象为 $node,结点值 = ${node!.val}");
}

@ -0,0 +1,162 @@
/**
* File: binary_search_tree.dart
* Created Time: 2023-04-04
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import '../utils/print_util.dart';
import '../utils/tree_node.dart';
/* 二叉搜索树 */
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;
}
//
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;
}
/* 插入结点 */
TreeNode? insert(int num) {
//
if (root == null) return null;
TreeNode? cur = root;
TreeNode? pre = null;
//
while (cur != null) {
//
if (cur.val == num) return null;
pre = cur;
// cur
if (cur.val < num)
cur = cur.right;
// cur
else
cur = cur.left;
}
// val
TreeNode? node = TreeNode(num);
if (pre!.val < num)
pre.right = node;
else
pre.left = node;
return node;
}
/* 删除结点 */
TreeNode? remove(int num) {
//
if (root == null) return null;
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 null;
// = 0 or 1
if (cur.left == null || cur.right == null) {
// = 0 / 1 child = null /
TreeNode? child = cur.left ?? cur.right;
// cur
if (pre!.left == cur)
pre.left = child;
else
pre.right = child;
} else {
// = 2
// cur
TreeNode? nex = getInOrderNext(cur.right);
int tem = nex!.val;
// nex
remove(nex.val);
// nex cur
cur.val = tem;
}
return cur;
}
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
TreeNode? getInOrderNext(TreeNode? root) {
if (root == null) return null;
// 访
while (root!.left != null) {
root = root.left;
}
return root;
}
/* Driver Code */
void main() {
/* 初始化二叉搜索树 */
List<int> nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
binarySearchTree(nums);
print("\n初始化的二叉树为\n");
printTree(getRoot());
/* 查找结点 */
TreeNode? node = search(7);
print("\n查找到的结点对象为 $node,结点值 = ${node?.val}");
/* 插入结点 */
node = insert(16);
print("\n插入节点 16 后,二叉树为\n");
printTree(getRoot());
/* 删除结点 */
remove(1);
print("\n删除结点 1 后,二叉树为\n");
printTree(getRoot());
remove(2);
print("\n删除结点 2 后,二叉树为\n");
printTree(getRoot());
remove(4);
print("\n删除结点 4 后,二叉树为\n");
printTree(getRoot());
}

@ -0,0 +1,37 @@
/**
* File: binary_tree.dart
* Created Time: 2023-04-03
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import '../utils/print_util.dart';
import '../utils/tree_node.dart';
void main() {
/* 初始化二叉树 */
//
TreeNode n1 = TreeNode(1);
TreeNode n2 = TreeNode(2);
TreeNode n3 = TreeNode(3);
TreeNode n4 = TreeNode(4);
TreeNode n5 = TreeNode(5);
//
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
print("\n初始化二叉树\n");
printTree(n1);
/* 插入与删除结点 */
TreeNode p = TreeNode(0);
// n1 -> n2 p
n1.left = p;
p.left = n2;
print("\n插入结点 P 后\n");
printTree(n1);
// P
n1.left = n2;
print("\n删除结点 P 后\n");
printTree(n1);
}

@ -0,0 +1,38 @@
/**
* File: binary_tree_bfs.dart
* Created Time: 2023-04-03
* Author: liuyuxin (gvenusleo@gmai.com)
*/
import 'dart:collection';
import '../utils/print_util.dart';
import '../utils/tree_node.dart';
/* 层序遍历 */
List<int> levelOrder(TreeNode? root) {
//
Queue<TreeNode?> queue = Queue();
queue.add(root);
//
List<int> res = [];
while (queue.isNotEmpty) {
TreeNode? node = queue.removeFirst(); //
res.add(node!.val); //
if (node.left != null) queue.add(node.left); //
if (node.right != null) queue.add(node.right); //
}
return res;
}
/* Driver Code */
void main() {
/* 初始化二叉树 */
//
TreeNode? root = listToTree([1, 2, 3, 4, 5, 6, 7]);
print("\n初始化二叉树\n");
printTree(root);
//
List<int> res = levelOrder(root);
print("\n层序遍历的结点打印序列 = $res");
}

@ -0,0 +1,62 @@
/**
* File: binary_tree_dfs.dart
* Created Time: 2023-04-04
* Author: liuyuxin (gvenusleo@gmail.com)
*/
import '../utils/print_util.dart';
import '../utils/tree_node.dart';
//
List<int> list = [];
/* 前序遍历 */
void preOrder(TreeNode? node) {
if (node == null) return;
// 访 -> ->
list.add(node.val);
preOrder(node.left);
preOrder(node.right);
}
/* 中序遍历 */
void inOrder(TreeNode? node) {
if (node == null) return;
// 访 -> ->
inOrder(node.left);
list.add(node.val);
inOrder(node.right);
}
/* 后序遍历 */
void postOrder(TreeNode? node) {
if (node == null) return;
// 访 -> ->
postOrder(node.left);
postOrder(node.right);
list.add(node.val);
}
/* Driver Code */
void main() {
/* 初始化二叉树 */
//
TreeNode? root = listToTree([1, 2, 3, 4, 5, 6, 7]);
print("\n初始化二叉树\n");
printTree(root);
/* 前序遍历 */
list.clear();
preOrder(root);
print("\n前序遍历的结点打印序列 = $list");
/* 中序遍历 */
list.clear();
inOrder(root);
print("\n中序遍历的结点打印序列 = $list");
/* 后序遍历 */
list.clear();
postOrder(root);
print("\n后序遍历的结点打印序列 = $list");
}

@ -7,14 +7,12 @@
import 'dart:collection';
class TreeNode {
late int val; //
late int height; //
late TreeNode? left; //
late TreeNode? right; //
int val; //
int height; //
TreeNode? left; //
TreeNode? right; //
TreeNode(int x) {
val = x;
}
TreeNode(this.val, [this.height = 0, this.left, this.right]);
}
/**
@ -30,14 +28,13 @@ TreeNode? listToTree(List<int> list) {
Queue<TreeNode?> queue = Queue();
queue.add(root);
int i = 0;
while (!queue.isEmpty) {
TreeNode? node = queue.first;
queue.removeFirst();
while (queue.isNotEmpty) {
TreeNode? node = queue.removeFirst();
if (++i >= size) break;
node?.left = TreeNode(list[i]);
queue.add(node?.left);
if (++i >= size) break;
node?.left = TreeNode(list[i]);
node?.right = TreeNode(list[i]);
queue.add(node?.right);
}
return root;

Loading…
Cancel
Save