mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-10 02:50:59 +00:00
Add dart chapter_computational_complexity (#363)
* add dart chapter_array_and_linkedlist * update my_list.dart * update chapter_array_and_linkedlist * Update my_list.dart * Update array.dart * Update file name * Add chapter_computational_complexity * Add chapter_computational_complexity * add space_complexity class and format code * remove class --------- Co-authored-by: huangjianqing <huangjianqing@52tt.com> Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
/**
|
||||
* File: PrintUtil
|
||||
* Created Time: 2023-01-23
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
import 'ListNode.dart';
|
||||
|
||||
class PrintUtil {
|
||||
|
||||
void printLinkedList(ListNode? head) {
|
||||
List<String> list = [];
|
||||
|
||||
while (head != null) {
|
||||
list.add('${head.val}');
|
||||
head = head.next;
|
||||
}
|
||||
|
||||
print(list.join(' -> '));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* File: PrintUtil
|
||||
* Created Time: 2023-01-23
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
import 'dart:io';
|
||||
|
||||
import 'list_node.dart';
|
||||
import 'tree_node.dart';
|
||||
|
||||
class Trunk {
|
||||
Trunk? prev;
|
||||
String str;
|
||||
|
||||
Trunk(this.prev, this.str);
|
||||
}
|
||||
|
||||
void printLinkedList(ListNode? head) {
|
||||
List<String> list = [];
|
||||
|
||||
while (head != null) {
|
||||
list.add('${head.val}');
|
||||
head = head.next;
|
||||
}
|
||||
|
||||
print(list.join(' -> '));
|
||||
}
|
||||
/*
|
||||
* Print a binary tree
|
||||
* @param root
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
|
||||
void printTree(TreeNode? root, [Trunk? prev = null, bool isLeft = false]) {
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
String prev_str = ' ';
|
||||
Trunk trunk = Trunk(prev, prev_str);
|
||||
|
||||
printTree(root.right, trunk, true);
|
||||
|
||||
if (prev == null) {
|
||||
trunk.str = '---';
|
||||
} else if (isLeft) {
|
||||
trunk.str = '/---';
|
||||
prev_str = ' |';
|
||||
} else {
|
||||
trunk.str = '\\---';
|
||||
prev.str = prev_str;
|
||||
}
|
||||
showTrunks(trunk);
|
||||
print(' ${root.val}');
|
||||
|
||||
if (prev != null) {
|
||||
prev.str = prev_str;
|
||||
}
|
||||
trunk.str = ' |';
|
||||
|
||||
printTree(root.left, trunk, false);
|
||||
}
|
||||
|
||||
void showTrunks(Trunk? p) {
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p.prev);
|
||||
stdout.write(p.str);
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* File: tree_node.dart
|
||||
* Created Time: 2023-2-12
|
||||
* Author: Jefferson (JeffersonHuang77@gmail.com)
|
||||
*/
|
||||
|
||||
import 'dart:collection';
|
||||
|
||||
class TreeNode {
|
||||
late int val; // 结点值
|
||||
late int height; // 结点高度
|
||||
late TreeNode? left; // 左子结点引用
|
||||
late TreeNode? right; // 右子结点引用
|
||||
|
||||
TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a binary tree given an array
|
||||
* @param list
|
||||
* @return
|
||||
*/
|
||||
TreeNode? listToTree(List<int> list) {
|
||||
int size = list.length;
|
||||
if (size == 0) return null;
|
||||
|
||||
TreeNode root = TreeNode(list[0]);
|
||||
Queue<TreeNode?> queue = Queue();
|
||||
queue.add(root);
|
||||
int i = 0;
|
||||
while (!queue.isEmpty) {
|
||||
TreeNode? node = queue.first;
|
||||
queue.removeFirst();
|
||||
if (++i >= size) break;
|
||||
node?.left = TreeNode(list[i]);
|
||||
queue.add(node?.left);
|
||||
if (++i >= size) break;
|
||||
node?.left = TreeNode(list[i]);
|
||||
queue.add(node?.right);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a binary tree to a list
|
||||
* @param root
|
||||
* @return
|
||||
*/
|
||||
List<int?> treeToList(TreeNode? root) {
|
||||
List<int?> list = [];
|
||||
if (root == null) return list;
|
||||
Queue<TreeNode?> queue = Queue();
|
||||
queue.add(root);
|
||||
|
||||
while (!queue.isEmpty) {
|
||||
TreeNode? node = queue.first;
|
||||
queue.removeFirst();
|
||||
if (node != null) {
|
||||
list.add(node.val);
|
||||
queue.add(node.left);
|
||||
queue.add(node.right);
|
||||
} else {
|
||||
list.add(null);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
Reference in New Issue
Block a user