開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Safari、Firefox + Firebug (Webプラウザ、プラグイン)
- JavaScript (プログラミング言語)
- jQuery (JavaScript Library)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅢ部(高度なプログラミング概念)の17章(高度なポインタ)、17.7(ツリー)、17.8(ツリーの出力)、17.9(プログラムの残りの部分)、設問 17-2をJavaScriptで解いてみる。
その他参考書籍
設問 17-2.
コード(BBEdit)
var Node = function (word) { var word = word, left = null, right = null; this.getWord = function () { return word; }, this.setWord = function (word) { word = word; }, this.getLeft = function () { return left; }, this.setLeft = function (node) { left = node; }, this.getRight = function () { return right; }, this.setRight = function (node) { right = node; }; }, Tree = function () { var root = null, _enter = function (node, word) { if(node === null){ node = new Node(word); } else if(node.getWord() > word){ node.setLeft(_enter(node.getLeft(), word)); } else if(node.getWord() < word){ node.setRight(_enter(node.getRight(), word)); } return node; }, printTree = function (node, n) { if(node === null){ return ''; } return printTree(node.getLeft(), n + 1) + new Array(n + 1).join(' ') + node.getWord() + '\n' + printTree(node.getRight(), n + 1); }; this.s = printTree(root, 0); this.enter = function (word) { root = _enter(root, word); }, this.toString = function () { return printTree(root, 0); }; }, t = new Tree(), words = ['able', 'baker', 'cook', 'delta', 'easy'], i, max; for(i = 0, max = words.length; i < max; i += 1){ t.enter(words[i]); } $('#pre0').text(t.toString());
0 コメント:
コメントを投稿