Skip to content

Commit 99bd54c

Browse files
committed
refactored from ES6 classes to factory function to Binary Tree Data Strucutre
1 parent 07a314b commit 99bd54c

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed

10-chapter-Binary-Tree/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(function (exports) {
2+
const {tree} = require('./tree.module')
3+
Object.assign(exports, {tree})
4+
}((typeof module.exports !== undefined) ? module.exports : window))

10-chapter-Binary-Tree/readme.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Binary Tree and Binary Search Tree
2+
3+
### Code Examples
4+
- [Binary Tree Structure](./tree.module.js)
5+
- [Import Module](./index.js)
6+
7+
### Description
8+
9+
`Binary trees` are chosen over other more primary data structures because you can search a binary tree very quickly (as opposed to a linked list, for example) and you can quickly insert and delete data from a binary tree (as opposed to an array).
10+
11+
common operations you can perform on graphs:
12+
13+
**Additions**
14+
- `insert`: insert a node to tree, if the node is lower than the root it place it to left otherwise to the right
15+
16+
**Removals**
17+
18+
- `remove`: removes a node from the tree
19+
20+
**Search**
21+
22+
- `find`: Search for a specific value
23+
- `maxValue`: Search for the minimum value
24+
- `minValue`: Search for the maximum value
25+
26+
**Traversals**
27+
28+
- `inOrder`: In this traversal method, the left subtree is visited first, then the root and later the right sub-tree
29+
- `preOrder`: In this traversal method, the root node is visited first, then the left subtree and finally the right subtree.
30+
- `postOrder`: In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node.
31+
32+
![](https://blog.penjee.com/wp-content/uploads/2015/11/binary-search-tree-sorted-array-animation.gif)
33+
34+
35+
**Display**
36+
37+
- `show`: displays the node data
38+
39+
40+
### Example Use Cases:
41+
42+
- Binary Search Tree - Used in many search applications where data is constantly entering/leaving, such as the map and set objects in many languages' libraries.
43+
- Binary Space Partition - Used in almost every 3D video game to determine what objects need to be rendered.
44+
- Binary Trees - Used in almost every high-bandwidth router for storing router-tables.
45+
- Hash Trees - used in p2p programs and specialized image-signatures in which a hash needs to be verified, but the whole file is not available.
46+
- Heaps - Used in implementing efficient priority-queues, which in turn are used for scheduling processes in many operating systems, Quality-of-Service in routers, and A* (path-finding algorithm used in AI applications, including robotics and video games). Also used in heap-sort.

10-chapter-Binary-Tree/test.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const {tree} = require('./index')
2+
3+
const nums = tree()
4+
nums.insert(23)
5+
nums.insert(45)
6+
nums.insert(16)
7+
nums.insert(37)
8+
nums.insert(3)
9+
nums.insert(99)
10+
nums.insert(22)
11+
12+
console.log('Inorder traversal: ')
13+
nums.inOrder(nums.getRoot())
14+
console.log('\nPreorder traversal: ')
15+
nums.preOrder(nums.getRoot())
16+
console.log('\nPostorder traversal: ')
17+
nums.postOrder(nums.getRoot())
18+
console.log(`Find value 45: `)
19+
console.log(nums.find(45))
20+
21+
test('Binary tree', assert => {
22+
assert.equal(nums.totalNodes(), 7, `Total of nodes is ${nums.totalNodes()}`)
23+
assert.equal(nums.totalEdges(nums.getRoot()), 4, `Total of edges is ${nums.totalEdges(nums.getRoot())}`)
24+
assert.equal(nums.maxValue(), 99, `Max value of the tree is ${nums.maxValue()}`)
25+
assert.equal(nums.minValue(), 3, `Min value of the tree is ${nums.minValue()}`)
26+
})

10-chapter-Binary-Tree/tree.module.js

+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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

Comments
 (0)