Skip to content

Commit 21d32f8

Browse files
committed
adds tree lesson
1 parent a420e7f commit 21d32f8

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

trees/tree.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
function createNode(key) {
2+
const children = []
3+
4+
return {
5+
key,
6+
children,
7+
addChild(childKey) {
8+
const childNode = createNode(childKey)
9+
children.push(childNode)
10+
return childNode
11+
}
12+
}
13+
}
14+
15+
function createBinaryNode(key) {
16+
return {
17+
key,
18+
left: null,
19+
right: null,
20+
addLeft(leftKey) {
21+
const newLeft = createBinaryNode(leftKey)
22+
this.left = newLeft
23+
return newLeft
24+
},
25+
addRight(rightKey) {
26+
const newRight = createBinaryNode(rightKey)
27+
this.right = newRight
28+
return newRight
29+
}
30+
}
31+
}
32+
33+
function createTree(rootKey) {
34+
const root = createNode(rootKey)
35+
36+
return {
37+
root,
38+
print() {
39+
let result = root.key
40+
41+
function traverse(node, visitFn) {
42+
if (node.children.length) {
43+
node.children.forEach(visitFn)
44+
node.children.forEach(n => traverse(n, visitFn))
45+
}
46+
}
47+
48+
function addKeyToResult(node) {
49+
result += ` => ${node.key}`
50+
}
51+
52+
traverse(root, addKeyToResult)
53+
54+
return result
55+
}
56+
}
57+
}
58+
59+
function createBinaryTree(rootKey) {
60+
const root = createBinaryNode(rootKey)
61+
62+
const TRAVERSALS = {
63+
IN_ORDER: (node, visitFn) => {
64+
if (node !== null) {
65+
TRAVERSALS.IN_ORDER(node.left, visitFn)
66+
visitFn(node)
67+
TRAVERSALS.IN_ORDER(node.right, visitFn)
68+
}
69+
},
70+
71+
PRE_ORDER: (node, visitFn) => {
72+
if (node !== null) {
73+
visitFn(node)
74+
TRAVERSALS.PRE_ORDER(node.left, visitFn)
75+
TRAVERSALS.PRE_ORDER(node.right, visitFn)
76+
}
77+
},
78+
79+
POST_ORDER: (node, visitFn) => {
80+
if (node !== null) {
81+
TRAVERSALS.POST_ORDER(node.left, visitFn)
82+
TRAVERSALS.POST_ORDER(node.right, visitFn)
83+
visitFn(node)
84+
}
85+
}
86+
}
87+
88+
return {
89+
root,
90+
print(orderType = 'IN_ORDER') {
91+
let result = ''
92+
93+
const visitFn = node => {
94+
result += result.length === 0 ? node.key : ` => ${node.key}`
95+
}
96+
97+
TRAVERSALS[orderType](this.root, visitFn)
98+
99+
return result
100+
}
101+
}
102+
}
103+
104+
const dom = createTree('html')
105+
const head = dom.root.addChild('head')
106+
const body = dom.root.addChild('body')
107+
const title = head.addChild(
108+
'title - egghead Tree Lesson'
109+
)
110+
const head = dom.body.addChild('header')
111+
const main = dom.body.addChild('main')
112+
const footer = dom.body.addChild('footer')
113+
const h1 = dom.header.addChild('h1 - Tree Lesson')
114+
const p = dom.main.addChild('p - Learn about trees!')
115+
const copyright = footer.addChild(`Copyright ${new Date().getFullYear()}`)
116+
117+
console.log(dom.print())
118+
119+
exports.createBinaryNode = createBinaryNode
120+
exports.createBinaryTree = createBinaryTree
121+
exports.createNode = createNode
122+
exports.createTree = createTree

0 commit comments

Comments
 (0)