v7
This commit is contained in:
63
test/tree/main.dart
Normal file
63
test/tree/main.dart
Normal file
@@ -0,0 +1,63 @@
|
||||
import 'node.dart';
|
||||
|
||||
main(){
|
||||
/// 需求:在树中寻找满足需求的节点列表
|
||||
/// input: 2-3-1
|
||||
/// 输出节点 2, 2-3,2-3-1
|
||||
// findNodes(root, '2-2', 0,'');
|
||||
// List<Node> nodes = findNodes(root, '2-3-1', 0,'',[]);
|
||||
List<Node> nodes = find( '/2/3/1');
|
||||
print(nodes);
|
||||
}
|
||||
|
||||
List<Node> find(String input){
|
||||
String fixInput = input.substring(1);
|
||||
List<Node> nodes = findNodes(root2,fixInput,0,'/',[]);
|
||||
if(nodes.isNotEmpty&&nodes.last.value!=input){
|
||||
return [];
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
|
||||
List<Node> findNodes(Node node,String input,int deep,String prefix,List<Node> result){
|
||||
List<String> parts = input.split('/');
|
||||
if(deep>parts.length-1){
|
||||
return result;
|
||||
}
|
||||
String target = parts[deep];
|
||||
if(node.children.isNotEmpty){
|
||||
List<Node> nodes = node.children.where((e) => e.value==prefix+target).toList();
|
||||
bool match = nodes.isNotEmpty;
|
||||
if(match){
|
||||
Node matched = nodes.first;
|
||||
result.add(matched);
|
||||
String nextPrefix = '${matched.value}/';
|
||||
findNodes(matched, input, ++deep,nextPrefix,result);
|
||||
}
|
||||
}else{
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// List<Node> findNodes(Node node,String input,int deep,String prefix,List<Node> result){
|
||||
// List<String> parts = input.split('-');
|
||||
// if(deep>parts.length-1){
|
||||
// return result;
|
||||
// }
|
||||
// String target = parts[deep];
|
||||
// if(node.children.isNotEmpty){
|
||||
// List<Node> nodes = node.children.where((e) => e.value==prefix+target).toList();
|
||||
// bool match = nodes.isNotEmpty;
|
||||
// if(match){
|
||||
// Node matched = nodes.first;
|
||||
// result.add(matched);
|
||||
// String nextPrefix = '${matched.value}-';
|
||||
// findNodes(matched, input, ++deep,nextPrefix,result);
|
||||
// }
|
||||
// }else{
|
||||
// return result;
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
71
test/tree/node.dart
Normal file
71
test/tree/node.dart
Normal file
@@ -0,0 +1,71 @@
|
||||
class Node {
|
||||
final String value;
|
||||
final List<Node> children;
|
||||
|
||||
Node({required this.value, this.children = const []});
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Node{value: $value}';
|
||||
}
|
||||
}
|
||||
|
||||
Node root = Node(value: 'root', children: [
|
||||
Node(
|
||||
value: '1',
|
||||
children: [
|
||||
Node(value: '1-1'),
|
||||
Node(value: '1-2'),
|
||||
Node(value: '1-3'),
|
||||
],
|
||||
),
|
||||
Node(
|
||||
value: '2',
|
||||
children: [
|
||||
Node(value: '2-1'),
|
||||
Node(value: '2-2'),
|
||||
Node(value: '2-3',children: [
|
||||
Node(value: '2-3-1',),
|
||||
]),
|
||||
],
|
||||
),
|
||||
Node(
|
||||
value: '3',
|
||||
children: [
|
||||
Node(value: '3-1'),
|
||||
Node(value: '3-2', children: [
|
||||
Node(value: '3-2-1',),
|
||||
]),
|
||||
],
|
||||
),
|
||||
]);
|
||||
|
||||
Node root2 = Node(value: '/', children: [
|
||||
Node(
|
||||
value: '/1',
|
||||
children: [
|
||||
Node(value: '/1/1'),
|
||||
Node(value: '/1/2'),
|
||||
Node(value: '/1/3'),
|
||||
],
|
||||
),
|
||||
Node(
|
||||
value: '/2',
|
||||
children: [
|
||||
Node(value: '/2/1'),
|
||||
Node(value: '/2/2'),
|
||||
Node(value: '/2/3',children: [
|
||||
Node(value: '/2/3/1',),
|
||||
]),
|
||||
],
|
||||
),
|
||||
Node(
|
||||
value: '/3',
|
||||
children: [
|
||||
Node(value: '/3/1'),
|
||||
Node(value: '/3/2', children: [
|
||||
Node(value: '/3/2/1',),
|
||||
]),
|
||||
],
|
||||
),
|
||||
]);
|
||||
Reference in New Issue
Block a user