mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Merge branches 'fourier' and 'master' of https://github.com/trekhleb/javascript-algorithms into fourier
This commit is contained in:
commit
7d6854489e
@ -8,6 +8,30 @@ nodes first, before moving to the next level neighbors.
|
||||
|
||||
![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/5/5d/Breadth-First-Search-Algorithm.gif)
|
||||
|
||||
## Pseudocode
|
||||
|
||||
```text
|
||||
BFS(root)
|
||||
Pre: root is the node of the BST
|
||||
Post: the nodes in the BST have been visited in breadth first order
|
||||
q ← queue
|
||||
while root = ø
|
||||
yield root.value
|
||||
if root.left = ø
|
||||
q.enqueue(root.left)
|
||||
end if
|
||||
if root.right = ø
|
||||
q.enqueue(root.right)
|
||||
end if
|
||||
if !q.isEmpty()
|
||||
root ← q.dequeue()
|
||||
else
|
||||
root ← ø
|
||||
end if
|
||||
end while
|
||||
end BFS
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Breadth-first_search)
|
||||
|
@ -19,6 +19,89 @@ potentially more efficient (for nodes other than first nodes) because there
|
||||
is no need to keep track of the previous node during traversal or no need
|
||||
to traverse the list to find the previous node, so that its link can be modified.
|
||||
|
||||
## Pseudocode for Basic Operations
|
||||
|
||||
### Insert
|
||||
|
||||
```text
|
||||
Add(value)
|
||||
Pre: value is the value to add to the list
|
||||
Post: value has been placed at the tail of the list
|
||||
n ← node(value)
|
||||
if head = ø
|
||||
head ← n
|
||||
tail ← n
|
||||
else
|
||||
n.previous ← tail
|
||||
tail.next ← n
|
||||
tail ← n
|
||||
end if
|
||||
end Add
|
||||
```
|
||||
|
||||
### Delete
|
||||
|
||||
```text
|
||||
Remove(head, value)
|
||||
Pre: head is the head node in the list
|
||||
value is the value to remove from the list
|
||||
Post: value is removed from the list, true; otherwise false
|
||||
if head = ø
|
||||
return false
|
||||
end if
|
||||
if value = head.value
|
||||
if head = tail
|
||||
head ← ø
|
||||
tail ← ø
|
||||
else
|
||||
head ← head.Next
|
||||
head.previous ← ø
|
||||
end if
|
||||
return true
|
||||
end if
|
||||
n ← head.next
|
||||
while n = ø and value = n.value
|
||||
n ← n.next
|
||||
end while
|
||||
if n = tail
|
||||
tail ← tail.previous
|
||||
tail.next ← ø
|
||||
return true
|
||||
else if n = ø
|
||||
n.previous.next ← n.next
|
||||
n.next.previous ← n.previous
|
||||
return true
|
||||
end if
|
||||
return false
|
||||
end Remove
|
||||
```
|
||||
|
||||
### Reverse Traversal
|
||||
|
||||
```text
|
||||
ReverseTraversal(tail)
|
||||
Pre: tail is the node of the list to traverse
|
||||
Post: the list has been traversed in reverse order
|
||||
n ← tail
|
||||
while n = ø
|
||||
yield n.value
|
||||
n ← n.previous
|
||||
end while
|
||||
end Reverse Traversal
|
||||
```
|
||||
|
||||
## Complexities
|
||||
|
||||
## Time Complexity
|
||||
|
||||
| Access | Search | Insertion | Deletion |
|
||||
| :-------: | :-------: | :-------: | :-------: |
|
||||
| O(n) | O(n) | O(1) | O(1) |
|
||||
|
||||
### Space Complexity
|
||||
|
||||
O(n)
|
||||
|
||||
## References
|
||||
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Doubly_linked_list)
|
||||
|
@ -61,7 +61,8 @@ end Contains
|
||||
```text
|
||||
Remove(head, value)
|
||||
Pre: head is the head node in the list
|
||||
Post: value is the value to remove from the list, true, otherwise false
|
||||
value is the value to remove from the list
|
||||
Post: value is removed from the list, true, otherwise false
|
||||
if head = ø
|
||||
return false
|
||||
end if
|
||||
|
@ -27,6 +27,235 @@ The leaves are not drawn.
|
||||
|
||||
![Binary Search Tree](https://upload.wikimedia.org/wikipedia/commons/d/da/Binary_search_tree.svg)
|
||||
|
||||
## Pseudocode for Basic Operations
|
||||
|
||||
### Insertion
|
||||
|
||||
```text
|
||||
insert(value)
|
||||
Pre: value has passed custom type checks for type T
|
||||
Post: value has been placed in the correct location in the tree
|
||||
if root = ø
|
||||
root ← node(value)
|
||||
else
|
||||
insertNode(root, value)
|
||||
end if
|
||||
end insert
|
||||
```
|
||||
|
||||
```text
|
||||
insertNode(current, value)
|
||||
Pre: current is the node to start from
|
||||
Post: value has been placed in the correct location in the tree
|
||||
if value < current.value
|
||||
if current.left = ø
|
||||
current.left ← node(value)
|
||||
else
|
||||
InsertNode(current.left, value)
|
||||
end if
|
||||
else
|
||||
if current.right = ø
|
||||
current.right ← node(value)
|
||||
else
|
||||
InsertNode(current.right, value)
|
||||
end if
|
||||
end if
|
||||
end insertNode
|
||||
```
|
||||
|
||||
### Searching
|
||||
|
||||
```text
|
||||
contains(root, value)
|
||||
Pre: root is the root node of the tree, value is what we would like to locate
|
||||
Post: value is either located or not
|
||||
if root = ø
|
||||
return false
|
||||
end if
|
||||
if root.value = value
|
||||
return true
|
||||
else if value < root.value
|
||||
return contains(root.left, value)
|
||||
else
|
||||
return contains(root.right, value)
|
||||
end if
|
||||
end contains
|
||||
```
|
||||
|
||||
|
||||
### Deletion
|
||||
|
||||
```text
|
||||
remove(value)
|
||||
Pre: value is the value of the node to remove, root is the node of the BST
|
||||
count is the number of items in the BST
|
||||
Post: node with value is removed if found in which case yields true, otherwise false
|
||||
nodeToRemove ← findNode(value)
|
||||
if nodeToRemove = ø
|
||||
return false
|
||||
end if
|
||||
parent ← findParent(value)
|
||||
if count = 1
|
||||
root ← ø
|
||||
else if nodeToRemove.left = ø and nodeToRemove.right = ø
|
||||
if nodeToRemove.value < parent.value
|
||||
parent.left ← nodeToRemove.right
|
||||
else
|
||||
parent.right ← nodeToRemove.right
|
||||
end if
|
||||
else if nodeToRemove.left = ø and nodeToRemove.right = ø
|
||||
if nodeToRemove.value < parent.value
|
||||
parent.left ← nodeToRemove.left
|
||||
else
|
||||
parent.right ← nodeToRemove.left
|
||||
end if
|
||||
else
|
||||
largestValue ← nodeToRemove.left
|
||||
while largestValue.right = ø
|
||||
largestValue ← largestValue.right
|
||||
end while
|
||||
findParent(largestValue.value).right ← ø
|
||||
nodeToRemove.value ← largestValue.value
|
||||
end if
|
||||
count ← count - 1
|
||||
return true
|
||||
end remove
|
||||
```
|
||||
|
||||
### Find Parent of Node
|
||||
|
||||
```text
|
||||
findParent(value, root)
|
||||
Pre: value is the value of the node we want to find the parent of
|
||||
root is the root node of the BST and is != ø
|
||||
Post: a reference to the prent node of value if found; otherwise ø
|
||||
if value = root.value
|
||||
return ø
|
||||
end if
|
||||
if value < root.value
|
||||
if root.left = ø
|
||||
return ø
|
||||
else if root.left.value = value
|
||||
return root
|
||||
else
|
||||
return findParent(value, root.left)
|
||||
end if
|
||||
else
|
||||
if root.right = ø
|
||||
return ø
|
||||
else if root.right.value = value
|
||||
return root
|
||||
else
|
||||
return findParent(value, root.right)
|
||||
end if
|
||||
end if
|
||||
end findParent
|
||||
```
|
||||
|
||||
### Find Node
|
||||
|
||||
```text
|
||||
findNode(root, value)
|
||||
Pre: value is the value of the node we want to find the parent of
|
||||
root is the root node of the BST
|
||||
Post: a reference to the node of value if found; otherwise ø
|
||||
if root = ø
|
||||
return ø
|
||||
end if
|
||||
if root.value = value
|
||||
return root
|
||||
else if value < root.value
|
||||
return findNode(root.left, value)
|
||||
else
|
||||
return findNode(root.right, value)
|
||||
end if
|
||||
end findNode
|
||||
```
|
||||
|
||||
### Find Minimum
|
||||
|
||||
```text
|
||||
findMin(root)
|
||||
Pre: root is the root node of the BST
|
||||
root = ø
|
||||
Post: the smallest value in the BST is located
|
||||
if root.left = ø
|
||||
return root.value
|
||||
end if
|
||||
findMin(root.left)
|
||||
end findMin
|
||||
```
|
||||
|
||||
### Find Maximum
|
||||
|
||||
```text
|
||||
findMax(root)
|
||||
Pre: root is the root node of the BST
|
||||
root = ø
|
||||
Post: the largest value in the BST is located
|
||||
if root.right = ø
|
||||
return root.value
|
||||
end if
|
||||
findMax(root.right)
|
||||
end findMax
|
||||
```
|
||||
|
||||
### Traversal
|
||||
|
||||
#### InOrder Traversal
|
||||
|
||||
```text
|
||||
inorder(root)
|
||||
Pre: root is the root node of the BST
|
||||
Post: the nodes in the BST have been visited in inorder
|
||||
if root = ø
|
||||
inorder(root.left)
|
||||
yield root.value
|
||||
inorder(root.right)
|
||||
end if
|
||||
end inorder
|
||||
```
|
||||
|
||||
#### PreOrder Traversal
|
||||
|
||||
```text
|
||||
preorder(root)
|
||||
Pre: root is the root node of the BST
|
||||
Post: the nodes in the BST have been visited in preorder
|
||||
if root = ø
|
||||
yield root.value
|
||||
preorder(root.left)
|
||||
preorder(root.right)
|
||||
end if
|
||||
end preorder
|
||||
```
|
||||
|
||||
#### PostOrder Traversal
|
||||
|
||||
```text
|
||||
postorder(root)
|
||||
Pre: root is the root node of the BST
|
||||
Post: the nodes in the BST have been visited in postorder
|
||||
if root = ø
|
||||
postorder(root.left)
|
||||
postorder(root.right)
|
||||
yield root.value
|
||||
end if
|
||||
end postorder
|
||||
```
|
||||
|
||||
## Complexities
|
||||
|
||||
### Time Complexity
|
||||
|
||||
| Access | Search | Insertion | Deletion |
|
||||
| :-------: | :-------: | :-------: | :-------: |
|
||||
| O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) |
|
||||
|
||||
### Space Complexity
|
||||
|
||||
O(n)
|
||||
|
||||
## References
|
||||
|
||||
- [Wikipedia](https://en.wikipedia.org/wiki/Binary_search_tree)
|
||||
|
Loading…
Reference in New Issue
Block a user