mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-09 12:22:36 +08:00
Fix pseudocode formatting.
This commit is contained in:
@ -10,6 +10,7 @@ nodes first, before moving to the next level neighbors.
|
|||||||
|
|
||||||
## Pseudocode
|
## Pseudocode
|
||||||
|
|
||||||
|
```text
|
||||||
BFS(root)
|
BFS(root)
|
||||||
Pre: root is the node of the BST
|
Pre: root is the node of the BST
|
||||||
Post: the nodes in the BST have been visited in breadth first order
|
Post: the nodes in the BST have been visited in breadth first order
|
||||||
@ -29,10 +30,7 @@ nodes first, before moving to the next level neighbors.
|
|||||||
end if
|
end if
|
||||||
end while
|
end while
|
||||||
end BFS
|
end BFS
|
||||||
|
```
|
||||||
## Space and Time Complexity:
|
|
||||||
|
|
||||||
O(b<sup>d + 1</sup>)
|
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
|
@ -19,10 +19,11 @@ 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
|
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.
|
to traverse the list to find the previous node, so that its link can be modified.
|
||||||
|
|
||||||
## Pseudocode
|
## Pseudocode for Basic Operations
|
||||||
|
|
||||||
### Insert
|
### Insert
|
||||||
|
|
||||||
|
```text
|
||||||
Add(value)
|
Add(value)
|
||||||
Pre: value is the value to add to the list
|
Pre: value is the value to add to the list
|
||||||
Post: value has been placed at the tail of the list
|
Post: value has been placed at the tail of the list
|
||||||
@ -32,12 +33,15 @@ to traverse the list to find the previous node, so that its link can be modified
|
|||||||
tail ← n
|
tail ← n
|
||||||
else
|
else
|
||||||
n.previous ← tail
|
n.previous ← tail
|
||||||
tail.next ← n1
|
tail.next ← n
|
||||||
tail ← n
|
tail ← n
|
||||||
end if
|
end if
|
||||||
end Add
|
end Add
|
||||||
|
```
|
||||||
|
|
||||||
### Delete
|
### Delete
|
||||||
|
|
||||||
|
```text
|
||||||
Remove(head, value)
|
Remove(head, value)
|
||||||
Pre: head is the head node in the list
|
Pre: head is the head node in the list
|
||||||
value is the value to remove from the list
|
value is the value to remove from the list
|
||||||
@ -51,7 +55,7 @@ to traverse the list to find the previous node, so that its link can be modified
|
|||||||
tail ← ø
|
tail ← ø
|
||||||
else
|
else
|
||||||
head ← head.Next
|
head ← head.Next
|
||||||
head.previous ← ∅
|
head.previous ← ø
|
||||||
end if
|
end if
|
||||||
return true
|
return true
|
||||||
end if
|
end if
|
||||||
@ -70,8 +74,11 @@ to traverse the list to find the previous node, so that its link can be modified
|
|||||||
end if
|
end if
|
||||||
return false
|
return false
|
||||||
end Remove
|
end Remove
|
||||||
|
```
|
||||||
|
|
||||||
### Reverse Traversal
|
### Reverse Traversal
|
||||||
|
|
||||||
|
```text
|
||||||
ReverseTraversal(tail)
|
ReverseTraversal(tail)
|
||||||
Pre: tail is the node of the list to traverse
|
Pre: tail is the node of the list to traverse
|
||||||
Post: the list has been traversed in reverse order
|
Post: the list has been traversed in reverse order
|
||||||
@ -81,15 +88,15 @@ to traverse the list to find the previous node, so that its link can be modified
|
|||||||
n ← n.previous
|
n ← n.previous
|
||||||
end while
|
end while
|
||||||
end Reverse Traversal
|
end Reverse Traversal
|
||||||
|
```
|
||||||
|
|
||||||
## Big O
|
## Complexities
|
||||||
|
|
||||||
## Time Complexity
|
## Time Complexity
|
||||||
|
|
||||||
Access: O(n)
|
| Access | Search | Insertion | Deletion |
|
||||||
Search: O(n)
|
| :-------: | :-------: | :-------: | :-------: |
|
||||||
Insert: O(1)
|
| O(n) | O(n) | O(1) | O(1) |
|
||||||
Delete: O(1)
|
|
||||||
|
|
||||||
### Space Complexity
|
### Space Complexity
|
||||||
|
|
||||||
|
@ -27,12 +27,13 @@ The leaves are not drawn.
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Pseudocode
|
## Pseudocode for Basic Operations
|
||||||
|
|
||||||
### Insertion
|
### Insertion
|
||||||
|
|
||||||
|
```text
|
||||||
insert(value)
|
insert(value)
|
||||||
Pre: value has passed custome type checks for type T
|
Pre: value has passed custom type checks for type T
|
||||||
Post: value has been placed in the correct location in the tree
|
Post: value has been placed in the correct location in the tree
|
||||||
if root = ø
|
if root = ø
|
||||||
root ← node(value)
|
root ← node(value)
|
||||||
@ -40,7 +41,9 @@ The leaves are not drawn.
|
|||||||
insertNode(root, value)
|
insertNode(root, value)
|
||||||
end if
|
end if
|
||||||
end insert
|
end insert
|
||||||
|
```
|
||||||
|
|
||||||
|
```text
|
||||||
insertNode(current, value)
|
insertNode(current, value)
|
||||||
Pre: current is the node to start from
|
Pre: current is the node to start from
|
||||||
Post: value has been placed in the correct location in the tree
|
Post: value has been placed in the correct location in the tree
|
||||||
@ -53,12 +56,16 @@ The leaves are not drawn.
|
|||||||
else
|
else
|
||||||
if current.right = ø
|
if current.right = ø
|
||||||
current.right ← node(value)
|
current.right ← node(value)
|
||||||
|
else
|
||||||
|
InsertNode(current.right, value)
|
||||||
end if
|
end if
|
||||||
end if
|
end if
|
||||||
end insertNode
|
end insertNode
|
||||||
|
```
|
||||||
|
|
||||||
### Searching
|
### Searching
|
||||||
|
|
||||||
|
```text
|
||||||
contains(root, value)
|
contains(root, value)
|
||||||
Pre: root is the root node of the tree, value is what we would like to locate
|
Pre: root is the root node of the tree, value is what we would like to locate
|
||||||
Post: value is either located or not
|
Post: value is either located or not
|
||||||
@ -73,9 +80,12 @@ The leaves are not drawn.
|
|||||||
return contains(root.right, value)
|
return contains(root.right, value)
|
||||||
end if
|
end if
|
||||||
end contains
|
end contains
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Deletion
|
### Deletion
|
||||||
|
|
||||||
|
```text
|
||||||
remove(value)
|
remove(value)
|
||||||
Pre: value is the value of the node to remove, root is the node of the BST
|
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
|
count is the number of items in the BST
|
||||||
@ -110,8 +120,11 @@ The leaves are not drawn.
|
|||||||
count ← count - 1
|
count ← count - 1
|
||||||
return true
|
return true
|
||||||
end remove
|
end remove
|
||||||
|
```
|
||||||
|
|
||||||
### Find Parent of Node
|
### Find Parent of Node
|
||||||
|
|
||||||
|
```text
|
||||||
findParent(value, root)
|
findParent(value, root)
|
||||||
Pre: value is the value of the node we want to find the parent of
|
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 != ø
|
root is the root node of the BST and is != ø
|
||||||
@ -137,8 +150,11 @@ The leaves are not drawn.
|
|||||||
end if
|
end if
|
||||||
end if
|
end if
|
||||||
end findParent
|
end findParent
|
||||||
|
```
|
||||||
|
|
||||||
### Find Node
|
### Find Node
|
||||||
|
|
||||||
|
```text
|
||||||
findNode(root, value)
|
findNode(root, value)
|
||||||
Pre: value is the value of the node we want to find the parent of
|
Pre: value is the value of the node we want to find the parent of
|
||||||
root is the root node of the BST
|
root is the root node of the BST
|
||||||
@ -154,8 +170,11 @@ The leaves are not drawn.
|
|||||||
return findNode(root.right, value)
|
return findNode(root.right, value)
|
||||||
end if
|
end if
|
||||||
end findNode
|
end findNode
|
||||||
|
```
|
||||||
|
|
||||||
### Find Minimum
|
### Find Minimum
|
||||||
|
|
||||||
|
```text
|
||||||
findMin(root)
|
findMin(root)
|
||||||
Pre: root is the root node of the BST
|
Pre: root is the root node of the BST
|
||||||
root = ø
|
root = ø
|
||||||
@ -165,9 +184,11 @@ The leaves are not drawn.
|
|||||||
end if
|
end if
|
||||||
findMin(root.left)
|
findMin(root.left)
|
||||||
end findMin
|
end findMin
|
||||||
|
```
|
||||||
|
|
||||||
|
### Find Maximum
|
||||||
|
|
||||||
### Find Maximim
|
```text
|
||||||
findMax(root)
|
findMax(root)
|
||||||
Pre: root is the root node of the BST
|
Pre: root is the root node of the BST
|
||||||
root = ø
|
root = ø
|
||||||
@ -177,9 +198,13 @@ The leaves are not drawn.
|
|||||||
end if
|
end if
|
||||||
findMax(root.right)
|
findMax(root.right)
|
||||||
end findMax
|
end findMax
|
||||||
|
```
|
||||||
|
|
||||||
### Traversal
|
### Traversal
|
||||||
#### InOrder
|
|
||||||
|
#### InOrder Traversal
|
||||||
|
|
||||||
|
```text
|
||||||
inorder(root)
|
inorder(root)
|
||||||
Pre: root is the root node of the BST
|
Pre: root is the root node of the BST
|
||||||
Post: the nodes in the BST have been visited in inorder
|
Post: the nodes in the BST have been visited in inorder
|
||||||
@ -189,8 +214,11 @@ The leaves are not drawn.
|
|||||||
inorder(root.right)
|
inorder(root.right)
|
||||||
end if
|
end if
|
||||||
end inorder
|
end inorder
|
||||||
|
```
|
||||||
|
|
||||||
#### PreOrder
|
#### PreOrder Traversal
|
||||||
|
|
||||||
|
```text
|
||||||
preorder(root)
|
preorder(root)
|
||||||
Pre: root is the root node of the BST
|
Pre: root is the root node of the BST
|
||||||
Post: the nodes in the BST have been visited in preorder
|
Post: the nodes in the BST have been visited in preorder
|
||||||
@ -200,7 +228,11 @@ The leaves are not drawn.
|
|||||||
preorder(root.right)
|
preorder(root.right)
|
||||||
end if
|
end if
|
||||||
end preorder
|
end preorder
|
||||||
#### PostOrder
|
```
|
||||||
|
|
||||||
|
#### PostOrder Traversal
|
||||||
|
|
||||||
|
```text
|
||||||
postorder(root)
|
postorder(root)
|
||||||
Pre: root is the root node of the BST
|
Pre: root is the root node of the BST
|
||||||
Post: the nodes in the BST have been visited in postorder
|
Post: the nodes in the BST have been visited in postorder
|
||||||
@ -210,23 +242,20 @@ The leaves are not drawn.
|
|||||||
yield root.value
|
yield root.value
|
||||||
end if
|
end if
|
||||||
end postorder
|
end postorder
|
||||||
|
```
|
||||||
|
|
||||||
|
## Complexities
|
||||||
## Big O
|
|
||||||
|
|
||||||
### Time Complexity
|
### Time Complexity
|
||||||
|
|
||||||
Access: O(log(n))
|
| Access | Search | Insertion | Deletion |
|
||||||
Search: O(log(n))
|
| :-------: | :-------: | :-------: | :-------: |
|
||||||
Insert: O(log(n))
|
| O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) |
|
||||||
Delete: O(log(n))
|
|
||||||
|
|
||||||
|
|
||||||
### Space Complexity
|
### Space Complexity
|
||||||
|
|
||||||
O(n)
|
O(n)
|
||||||
|
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
- [Wikipedia](https://en.wikipedia.org/wiki/Binary_search_tree)
|
- [Wikipedia](https://en.wikipedia.org/wiki/Binary_search_tree)
|
||||||
|
Reference in New Issue
Block a user