-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (113 loc) · 3.95 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const { User } = require('./1-constructor-functions.js')
const { LinkedList } = require('./2-linked-lists.js')
const { log, logAll, addAndLog, binarySearch } = require('./3-big-o-notation.js')
const { factorial } = require('./4-recursion.js')
const { BST } = require('./5-binary-search-tree.js')
const { HashTable } = require('./6-hash-table.js')
// >>> CONSTRUCTOR FUNCTIONS & "THIS" KEYWORD & PROTOTYPE
console.log("\n\n>>> CONSTRUCTOR FUNCTIONS & 'THIS' KEYWORD & PROTOTYPE\n")
let user200 = new User('user', '200', '300', 'binary')
console.log(user200);
console.log(user200.emailDomain)
console.log(user200.getEmailAddress())
// >>> LINKED LISTS
console.log("\n\n>>> LINKED LISTS\n")
let ll = new LinkedList();
ll.addToTail('one');
ll.addToTail(2000);
ll.addToTail('two');
ll.addToTail('three');
ll.addToTail(19);
ll.addToTail('three');
ll.addToTail(2);
console.log(ll)
// console.log(ll.removeTail(3000));
console.log(ll.indexOf('three'))
// >>> BIG O NOTATION
console.log("\n\n>>> BIG O NOTATION\n")
// Constant runtime - Big O Notation: "O (1)"
log([1, 2, 3, 4]);
log([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
// Linear runtime - Big O Notation: "O (n)"
logAll([1, 2, 3, 4, 5]);
logAll([1, 2, 3, 4, 5, 6]);
logAll([1, 2, 3, 4, 5, 6, 7]);
// Exponential runtime - Big O Notation: "O (n^2)"
addAndLog(['A', 'B', 'C']); // 9 pairs logged out
addAndLog(['A', 'B', 'C', 'D']); // 16 pairs logged out
addAndLog(['A', 'B', 'C', 'D', 'E']); // 25 pairs logged out
// Logarithmic runtime - Big O Notation: O (log n)
console.log(binarySearch(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'Ñ', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'], 'H'))
// >>> RECURSION
console.log("\n\n>>> RECURSION\n")
console.log(factorial(4))
// >>> BINARY SEARCH TREE
console.log("\n\n>>> BINARY SEARCH TREE\n")
// 50
// 30 70
// 20 45 60 100
// 10 35 59 85 105
let bst = new BST(50);
bst.insert(30)
bst.insert(70)
bst.insert(100)
bst.insert(60)
bst.insert(59)
bst.insert(20)
bst.insert(45)
bst.insert(35)
bst.insert(85)
bst.insert(105)
bst.insert(10)
console.log('\nShow Branch Value')
console.log(bst.right.left.left) // 59
console.log(bst.right.right) // 100
console.log(bst.left.right.left) // 35
console.log('\nContains Value')
console.log(bst.contains(50)); // true
console.log(bst.contains(15)); // false
console.log(bst.contains(100)); // true
// Depth-First Traversal
console.log('\nDepth-First Traversal')
function logNode(node) {
console.log(node);
}
console.log('\n bottom to top')
bst.depthFirstTraversal(logNode, 'in-order');
console.log('\n top to bottom')
bst.depthFirstTraversal(logNode, 'pre-order');
console.log('\n bottom to top branch by branch')
bst.depthFirstTraversal(logNode, 'post-order');
// Bread-First Traversal
console.log('\nBread-First Traversal')
function logChild(node) {
console.log(node.value);
}
bst.breadFirstTraversal(logChild)
console.log('\nMIN and MAX values (more on the left and more on the right)')
console.log('MIN:', bst.getMinVal() )
console.log('MAX:', bst.getMaxVal() )
// >>> HASH TABLE
console.log("\n\n>>> HASH TABLE\n")
let myHT = new HashTable(30)
console.log('\nprintout a hash table ')
console.log(myHT)
console.log('\nprintout a hash bucket index ')
console.log(myHT.hash('Franco'))
console.log('\ninsert new nodes into hash table')
myHT.insert('Dean', '[email protected]')
myHT.insert('Leo', '[email protected]')
myHT.insert('Dane', '[email protected]')
console.log('\nupdate new nodes into hash table')
myHT.insert('Dean', '[email protected]')
myHT.insert('Leo', '[email protected]')
myHT.insert('Dane', '[email protected]')
myHT.insert('Franco', '[email protected]')
myHT.insert('Meli', '[email protected]')
myHT.insert('Paco', '[email protected]')
console.log('\nget a node from hash table buckets')
console.log(myHT.get('Leo'))
console.log(myHT.get('Dane'))
console.log('\nretrieve all nodes in buckets')
console.log(myHT.retrieveAll())
console.log('\n', myHT.buckets);