開発環境
- OS X Mavericks - Apple (OS)
- Dart Editor (開発環境)
- Dartium | Dart/ Structured web apps (ブラウザ, Dart VM 用 (Chromium with the Dart VM))
- Safari (ブラウザ, JavaScript 用)
- Dart (プログラミング言語)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅢ部(高度なプログラミング概念)の17章(高度なポインタ)、17.7(ツリー)、17.8(ツリーの出力)、17.9(プログラムの残りの部分)、設問 17-2をDartで解いてみる。
その他参考書籍
- What is Dart? [Kindle版] (O'Reilly Media) Kathy Walrath Seth Ladd (著) このブログでの感想
設問 17-2
コード
sample.dart
import 'dart:html';
void main(){
InputElement run = querySelector('#run_dart');
InputElement clear = querySelector('#clear');
Element pre = querySelector('#pre0');
for(var word in words){
t.enter(word);
}
run.onClick.listen((MouseEvent event){
pre.text = window.navigator.userAgent + '\n';
pre.text += '$t';
});
clear.onClick.listen((MouseEvent event) => pre.text = '');
}
Tree t = new Tree();
List<String> words = ['able', 'baker', 'cook', 'delta', 'easy'];
class Node{
String word;
Node left;
Node right;
Node(String word){
this.word = word;
this.left = null;
this.right = null;
}
}
class Tree{
Node top;
Tree(){
this.top = null;
}
void enter(String word){
this.top = _enter(this.top, word);
}
Node _enter(Node node, String word){
if(node == null){
node = new Node(word);
} else if(node.word.compareTo(word) < 0){
node.right = _enter(node.right, word);
} else if(node.word.compareTo(word) > 0){
node.left = _enter(node.left, word);
}
return node;
}
String toString(){
return _printTree(top, 0);
}
String _printTree(Node node, int n){
if(node == null){
return '';
}
return _printTree(node.left, n + 1) +
new List.filled(n, ' ').join() + node.word + '\n' +
_printTree(node.right, n + 1);
}
}
0 コメント:
コメントを投稿