|
| 1 | +<link rel="import" href="polymer.html"> |
| 2 | +<link rel="import" href="binary_tree.html"> |
| 3 | + |
| 4 | +<dom-module id="root-tree"> |
| 5 | + <template> |
| 6 | + <div> |
| 7 | + <button on-click="create" id="createDom">createDom</button> |
| 8 | + <button on-click="destroy" id="destroyDom">destroyDom</button> |
| 9 | + </div> |
| 10 | + <div> |
| 11 | + <binary-tree data="[[data]]"></binary-tree> |
| 12 | + </div> |
| 13 | + </template> |
| 14 | +</dom-module> |
| 15 | +<script type="application/javascript"> |
| 16 | + (function() { |
| 17 | + var count = 0; |
| 18 | + var depthInput = document.querySelector('input[name=depth]'); |
| 19 | + var match = /depth=(\w+)/.exec(decodeURIComponent(location.search)); |
| 20 | + if (match) { |
| 21 | + depthInput.value = match[1]; |
| 22 | + } |
| 23 | + var maxDepth = depthInput.valueAsNumber; |
| 24 | + Polymer({ |
| 25 | + is: 'root-tree', |
| 26 | + properties: { |
| 27 | + data: Object |
| 28 | + }, |
| 29 | + create: function() { |
| 30 | + var values = count++ % 2 == 0 ? |
| 31 | + ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*'] : |
| 32 | + ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', '-']; |
| 33 | + |
| 34 | + this.data = this.buildTree(maxDepth, values, 0); |
| 35 | + }, |
| 36 | + destroy: function() { |
| 37 | + this.data = null; |
| 38 | + }, |
| 39 | + buildTree: function(maxDepth, values, curDepth) { |
| 40 | + if (maxDepth == curDepth) return {value: '', left: null, right: null}; |
| 41 | + return { |
| 42 | + value: values[curDepth], |
| 43 | + left: this.buildTree(maxDepth, values, curDepth+1), |
| 44 | + right: this.buildTree(maxDepth, values, curDepth+1) |
| 45 | + }; |
| 46 | + } |
| 47 | + }); |
| 48 | + })(); |
| 49 | +</script> |
0 commit comments