|
| 1 | +(function (exports) { |
| 2 | + const tree = (r = null, n = 0) => { |
| 3 | + let nodes = n |
| 4 | + let root = r |
| 5 | + |
| 6 | + const node = (data, left = null, right = null) => { |
| 7 | + const o = { |
| 8 | + left: null, |
| 9 | + right: null, |
| 10 | + show () { |
| 11 | + console.log(this.data) |
| 12 | + } |
| 13 | + } |
| 14 | + return Object.assign(Object.create(o), {data, left, right}) |
| 15 | + } |
| 16 | + |
| 17 | + const traversals = { |
| 18 | + find (data) { |
| 19 | + let current = root |
| 20 | + while ((current.data !== data)) { |
| 21 | + if (data < current.data) { |
| 22 | + current = current.left |
| 23 | + } else { |
| 24 | + current = current.right |
| 25 | + } |
| 26 | + if (current === null) { |
| 27 | + return undefined |
| 28 | + } |
| 29 | + } |
| 30 | + return current |
| 31 | + }, |
| 32 | + inOrder: function inOrder (node) { |
| 33 | + if (!(node === null)) { |
| 34 | + inOrder(node.left) |
| 35 | + node.show() |
| 36 | + inOrder(node.right) |
| 37 | + } |
| 38 | + }, |
| 39 | + preOrder: function preOrder (node) { |
| 40 | + if (!(node === null)) { |
| 41 | + node.show() |
| 42 | + preOrder(node.left) |
| 43 | + preOrder(node.right) |
| 44 | + } |
| 45 | + }, |
| 46 | + postOrder: function postOrder (node) { |
| 47 | + if (!(node === null)) { |
| 48 | + postOrder(node.left) |
| 49 | + postOrder(node.right) |
| 50 | + node.show() |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + const getValues = { |
| 56 | + minValue () { |
| 57 | + let current = root |
| 58 | + while (!(current.left === null)) { |
| 59 | + current = current.left |
| 60 | + } |
| 61 | + return current.data |
| 62 | + }, |
| 63 | + maxValue () { |
| 64 | + let current = root |
| 65 | + while (!(current.right === null)) { |
| 66 | + current = current.right |
| 67 | + } |
| 68 | + return current.data |
| 69 | + }, |
| 70 | + totalNodes: () => nodes, |
| 71 | + totalEdges: function totalEdges (node) { |
| 72 | + if (node === null) { |
| 73 | + return 0 |
| 74 | + } |
| 75 | + if (node.left === null && node.right === null) { |
| 76 | + return 1 |
| 77 | + } else { |
| 78 | + return totalEdges(node.left) + totalEdges(node.right) |
| 79 | + } |
| 80 | + }, |
| 81 | + getRoot: () => root |
| 82 | + } |
| 83 | + |
| 84 | + const insert = (data) => { |
| 85 | + const n = node(data) |
| 86 | + if (root == null) { |
| 87 | + root = n |
| 88 | + } else { |
| 89 | + let current = root |
| 90 | + while (!(current === null)) { |
| 91 | + let parent = current |
| 92 | + if (data < current.data) { |
| 93 | + current = current.left |
| 94 | + if (current === null) { |
| 95 | + parent.left = n |
| 96 | + } |
| 97 | + } else { |
| 98 | + current = current.right |
| 99 | + if (current === null) { |
| 100 | + parent.right = n |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + nodes += 1 |
| 106 | + } |
| 107 | + |
| 108 | + const remove = (data) => { |
| 109 | + root = removeNode(getValues.getRoot(), data) |
| 110 | + } |
| 111 | + |
| 112 | + const removeNode = (node, data) => { |
| 113 | + if (node === null) { |
| 114 | + return null |
| 115 | + } |
| 116 | + if (data === node.data) { |
| 117 | + if (node.left === null && node.right === null) { |
| 118 | + return null |
| 119 | + } |
| 120 | + if (node.left === null) { |
| 121 | + return node.right |
| 122 | + } |
| 123 | + if (node.right === null) { |
| 124 | + return node.left |
| 125 | + } |
| 126 | + |
| 127 | + let tempNode = getValues.minValue(node.right) |
| 128 | + node.data = tempNode.data |
| 129 | + node.right = removeNode(node.right, data) |
| 130 | + return node |
| 131 | + } else if (data < node.data) { |
| 132 | + node.left = removeNode(node.left, data) |
| 133 | + } else { |
| 134 | + node.right = removeNode(node.right, data) |
| 135 | + return node |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return Object.assign({}, {insert}, {remove}, traversals, getValues) |
| 140 | + } |
| 141 | + |
| 142 | + Object.assign(exports, {tree}) |
| 143 | +}((typeof exports !== undefined) ? exports : window)) |
0 commit comments