mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-04 06:07:20 +08:00 
			
		
		
		
	* 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>
		
			
				
	
	
		
			73 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			73 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
/**
 | 
						|
 * 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);
 | 
						|
}
 |