Skip to content

Commit f8b68a3

Browse files
committed
chore(readme): add readme
1 parent 2ede60d commit f8b68a3

13 files changed

+238
-51
lines changed

README.md

+18-10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ These are the most famous data structures used in lot of applications to help so
1515

1616
- [**Linked List:**](./data-structures/lists/LinkedList.js) A linked list is a recursive data structure that is either empty or a reference to a node having a generic item and a reference to a linked list (a node is any kind of data that refers another node recursively).
1717

18+
- [**Binary Heap:**](./data-structures/binary-tree/BinaryHeap.js) A binary heap is a heap constructed with help of a binary tree structure, which can maintain all the values sorted during its insert and remove actions.
19+
1820
## Sorting
1921

2022
Below you can see some of the famous methods of sorting values.
@@ -53,24 +55,30 @@ With help of binary tree heap data structure used to build the heap priority que
5355

5456
- [**Heapsort:**](./algorithms/sorting/heap/HeapSort.js) The heapsort consists into two actions: first we reorganize the initial array into a heap and after we sortdown, that is when we pull the items out of the heap in decreasing order to build the sorted result.
5557

56-
## TODO
58+
## Searching
5759

58-
### 1. Finishing the following topics
60+
Below you can see some of the structures focused on searching. Usually these structures have the pattern or associating a value with a key. That key is used later to get the item you have inserted (what means that we must make sure that the key we create is unique).
5961

60-
Data Structures -> Binary Tree
62+
- [**Sequential Search:**](./algorithms/searching/symbol-table/SequentialSearch.js) It is a kind of Symbol Table, which we use a linked list structure to keep all items ordered and so to sequentially search them.
6163

62-
Analysis of Algorithms
64+
- [**Binary Search:**](./algorithms/searching/symbol-table/BinarySearch.js) It is a kind of Symbol Table, that during searches, we divide the keys in two parts and so, we determine if we will search on the right part or the left one, instead of searching sequentially all the array keys.
65+
66+
- [**Binary Search Tree:**](./algorithms/searching/symbol-table/BinarySearchTree.js) It is a symbol table that uses the binary tree data structure to handle all searching process. So, each node in the tree has a key that is larger than the keys in all node's left subtree and smaller than the keys in all nodes in that node's right subtree.
67+
68+
- [**Red Black Binary Search Tree:**](./algorithms/searching/balanced-search-tree/RedBlackBinarySearchTree.js) It is a binary search tree (actually the one with 2-3 nodes) with an optimization that helps keeping the path to a value almost the same for all entries. So, we can call it to be a balanced search tree method, since it is able to search values with same distance between the root of the tree to any value you want.
6369

64-
Sorting -> Heapsort -> Sortdown
70+
- [**Separate Chaining Hash:**](./algorithms/searching/hash-table/SeparateChainingHash.js) It is a hash table that converts the key into indexes to store the data inside an array of sequential searches.
6571

66-
Sorting -> Applications -> Pointer sorting
72+
- [**Linear Probing Hash:**](./algorithms/searching/hash-table/LinearProbingHash.js) It is a hash table which we store keys and values in separated arrays in order to get an easy search match later.
6773

68-
Sorting -> Applications -> Alternate orderings
74+
## TODO
75+
76+
### 1. Finishing the following topics
6977

70-
Sorting -> Priority Queues -> Heap Priority Queue
78+
Data Structures -> Binary Tree
79+
80+
Analysis of Algorithms
7181

7282
### 2. Adding the following information
7383

7484
Adding complexity order for all algorithms
75-
76-
Finishing Heapsort algorithm implementation

algorithms/searching/Search.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Search {
2+
compareValues(firstKey, secondKey) {
3+
if (!firstKey || !secondKey) {
4+
return null
5+
}
6+
7+
const comparison = firstKey.localeCompare(secondKey)
8+
9+
return comparison
10+
}
11+
12+
generateHash(key) {
13+
let hash = 0
14+
15+
for (let i = 0; i > key.length; i++) {
16+
const charCode = this.charCodeAt(i)
17+
hash = ((hash << 5) - hash) + charCode
18+
hash |= 0
19+
}
20+
21+
return hash
22+
}
23+
}
24+
25+
module.exports = Search
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
const Search = require("../Search")
2+
3+
class Node {
4+
#key
5+
#value
6+
#left
7+
#right
8+
#nodesCount
9+
10+
constructor(key, value, nodesCount) {
11+
this.#key = key
12+
this.#value = value
13+
this.#nodesCount = nodesCount
14+
}
15+
16+
get right() {
17+
return this.#right
18+
}
19+
20+
get left() {
21+
return this.#left
22+
}
23+
24+
get value() {
25+
return this.#value
26+
}
27+
28+
get nodesCount() {
29+
return this.#nodesCount
30+
}
31+
32+
get key() {
33+
return this.#key
34+
}
35+
36+
set value(value) {
37+
this.#value = value
38+
}
39+
40+
set right(right) {
41+
this.#right = right
42+
}
43+
44+
set left(left) {
45+
this.#left = left
46+
}
47+
48+
set nodesCount(nodesCount) {
49+
this.#nodesCount = nodesCount
50+
}
51+
}
52+
53+
class BinarySearchTree extends Search {
54+
#root
55+
56+
constructor() {
57+
super()
58+
59+
this.#root = null
60+
}
61+
62+
size() {
63+
return this.nodeSize(this.#root)
64+
}
65+
66+
nodeSize(node) {
67+
if (!node) {
68+
return 0
69+
} else {
70+
return node.nodesCount
71+
}
72+
}
73+
74+
get (key) {
75+
return this.getNodeValue(this.#root, key)
76+
}
77+
78+
getNodeValue (node, key) {
79+
if (!node) {
80+
return null
81+
}
82+
83+
const comparison = this.compareValues(key, node.key)
84+
85+
if (comparison < 0) {
86+
return this.getNodeValue(node.left, key)
87+
} else if (comparison > 0) {
88+
return this.getNodeValue(node.right, key)
89+
} else {
90+
return node.value
91+
}
92+
}
93+
94+
put (key, value) {
95+
this.#root = this.putNodeValue(this.#root, key, value)
96+
}
97+
98+
putNodeValue (node, key, value) {
99+
if (!node) {
100+
return new Node(key, value, 1)
101+
}
102+
103+
const comparison = this.compareValues(key, node.key)
104+
105+
if (comparison < 0) {
106+
node.left = this.putNodeValue(node.left, key, value)
107+
} else if (comparison > 0) {
108+
node.right = this.putNodeValue(node.right, key, value)
109+
} else {
110+
node.value = value
111+
}
112+
113+
node.nodesCount = this.nodeSize(node.left) + this.nodeSize(node.right) + 1
114+
115+
return node
116+
}
117+
}
118+
119+
module.exports = BinarySearchTree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const assert = require("assert")
2+
const BinarySearchTree = require("./BinarySearchTree.js")
3+
4+
const binarySearchTree = new BinarySearchTree()
5+
6+
binarySearchTree.put("Guilherme", 123)
7+
binarySearchTree.put("Mota", 431)
8+
binarySearchTree.put("teste", 0)
9+
10+
assert.deepEqual(binarySearchTree.get("Guilherme"), 123)
11+
assert.deepEqual(binarySearchTree.get("NotExistentKey"), null)
12+

algorithms/searching/symbol-table/SequentialSearch.js

+33-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
1-
const { Node } = require("../../../data-structures/DataStructures")
21
const Search = require("../Search")
32

3+
class Node {
4+
#key
5+
#value
6+
#next
7+
8+
constructor(key, value, next) {
9+
this.#key = key
10+
this.#value = value
11+
this.#next = next
12+
}
13+
14+
get next() {
15+
return this.#next
16+
}
17+
18+
get value() {
19+
return this.#value
20+
}
21+
22+
get key() {
23+
return this.#key
24+
}
25+
26+
set value(value) {
27+
this.#value = value
28+
}
29+
}
30+
431
class SequentialSearch extends Search {
532
#first
633

@@ -11,21 +38,21 @@ class SequentialSearch extends Search {
1138
}
1239

1340
get (key) {
14-
let item = null
41+
let value = null
1542

1643
for (let node = this.#first; node != null; node = node.next) {
1744
const nodeKey = node.key
1845

1946
const areKeysEqual = this.compareValues(key, nodeKey) === 0
2047

2148
if (areKeysEqual) {
22-
item = node.item
49+
value = node.value
2350

2451
break
2552
}
2653
}
2754

28-
return item
55+
return value
2956
}
3057

3158
put (key, value) {
@@ -35,18 +62,15 @@ class SequentialSearch extends Search {
3562
const areKeysEqual = this.compareValues(key, nodeKey) === 0
3663

3764
if (areKeysEqual) {
38-
node.item = value
65+
node.value = value
3966

4067
return
4168
}
4269
}
4370

4471
const next = this.#first
4572

46-
this.#first = new Node()
47-
this.#first.item = value
48-
this.#first.key = key
49-
this.#first.next = next
73+
this.#first = new Node(key, value, next)
5074
}
5175
}
5276

algorithms/sorting/Sort.js

+22-11
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,33 @@ class Sort {
8585
}
8686

8787
sinkBinaryTreeNodes(array = [], nodePosition, nodesCount) {
88-
let currentScanIndex = nodePosition
88+
let largestIndex = nodePosition
89+
let leftIndex = nodePosition * 2 + 1
90+
let rightIndex = leftIndex + 1
91+
92+
if (leftIndex < nodesCount && this.isFirstValueLowerThanSecondValue(array[largestIndex], array[leftIndex])) {
93+
largestIndex = leftIndex
94+
}
8995

90-
while (2 * currentScanIndex <= nodesCount) {
91-
let topScanIndex = 2 * currentScanIndex
96+
if (rightIndex < nodesCount && this.isFirstValueLowerThanSecondValue(array[largestIndex], array[rightIndex])) {
97+
largestIndex = rightIndex
98+
}
9299

93-
if (topScanIndex < nodesCount && this.isFirstValueLowerThanSecondValue(topScanIndex, topScanIndex + 1)) {
94-
topScanIndex++
95-
}
100+
if (largestIndex !== nodePosition) {
101+
this.exchangeValues(array, nodePosition, largestIndex)
96102

97-
if (!this.isFirstValueLowerThanSecondValue(currentScanIndex, topScanIndex)) {
98-
break
99-
}
103+
this.sinkBinaryTreeNodes(array, nodesCount, largestIndex)
104+
}
105+
}
106+
107+
swimBinaryTreeNodes(array = [], nodesCount) {
108+
let currentIndex = nodesCount
100109

101-
this.exchangeValues(array, currentScanIndex, topScanIndex)
110+
const middleIndex = Math.floor(currentIndex / 2)
102111

103-
currentScanIndex = topScanIndex
112+
while (currentIndex > 1 && this.isFirstValueLowerThanSecondValue(middleIndex, currentIndex)){
113+
this.exchangeValues(array, middleIndex, currentIndex)
114+
currentIndex = middleIndex
104115
}
105116
}
106117
}

algorithms/sorting/heap/HeapSort.test.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const heapSort = new HeapSort()
66
const unorderedValues = [1, 4, 3, 2]
77

88
heapSort.sort(unorderedValues)
9-
console.log(unorderedValues)
109

1110
assert.deepEqual(unorderedValues[0], 1)
1211
assert.deepEqual(unorderedValues[1], 2)

data-structures/bags/Bag.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const assert = require("../../sorting/elementary-sorts/node_modules/assert")
1+
const assert = require("assert")
22
const Bag = require("./Bag.js")
33

44
const bag = new Bag()

data-structures/lists/LinkedList.js

+4-15
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ class Node {
22
#item
33
#next
44

5-
constructor(item) {
6-
this.#item = null
7-
this.#next = null
5+
constructor(item, next) {
6+
this.#item = item
7+
this.#next = next
88
}
99

1010
get next() {
@@ -14,14 +14,6 @@ class Node {
1414
get item() {
1515
return this.#item
1616
}
17-
18-
set next(node) {
19-
this.#next = node
20-
}
21-
22-
set item(item) {
23-
this.#item = item
24-
}
2517
}
2618

2719
class LinkedList {
@@ -34,10 +26,7 @@ class LinkedList {
3426
}
3527

3628
push(item) {
37-
const node = new Node()
38-
39-
node.item = item
40-
node.next = this.#head
29+
const node = new Node(item, this.#head)
4130

4231
this.#head = node
4332

data-structures/lists/LinkedList.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const assert = require("../../sorting/elementary-sorts/node_modules/assert")
1+
const assert = require("assert")
22
const LinkedList = require("./LinkedList.js")
33

44
const linkedList = new LinkedList()

0 commit comments

Comments
 (0)