mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-13 23:42:23 +08:00
Fix pseudocode formatting.
This commit is contained in:
@ -10,6 +10,7 @@ nodes first, before moving to the next level neighbors.
|
||||
|
||||
## 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
|
||||
@ -29,10 +30,7 @@ nodes first, before moving to the next level neighbors.
|
||||
end if
|
||||
end while
|
||||
end BFS
|
||||
|
||||
## Space and Time Complexity:
|
||||
|
||||
O(b<sup>d + 1</sup>)
|
||||
```
|
||||
|
||||
## 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
|
||||
to traverse the list to find the previous node, so that its link can be modified.
|
||||
|
||||
## Pseudocode
|
||||
## 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
|
||||
@ -32,12 +33,15 @@ to traverse the list to find the previous node, so that its link can be modified
|
||||
tail ← n
|
||||
else
|
||||
n.previous ← tail
|
||||
tail.next ← n1
|
||||
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
|
||||
@ -51,7 +55,7 @@ to traverse the list to find the previous node, so that its link can be modified
|
||||
tail ← ø
|
||||
else
|
||||
head ← head.Next
|
||||
head.previous ← ∅
|
||||
head.previous ← ø
|
||||
end if
|
||||
return true
|
||||
end if
|
||||
@ -70,8 +74,11 @@ to traverse the list to find the previous node, so that its link can be modified
|
||||
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
|
||||
@ -81,15 +88,15 @@ to traverse the list to find the previous node, so that its link can be modified
|
||||
n ← n.previous
|
||||
end while
|
||||
end Reverse Traversal
|
||||
```
|
||||
|
||||
## Big O
|
||||
## Complexities
|
||||
|
||||
## Time Complexity
|
||||
|
||||
Access: O(n)
|
||||
Search: O(n)
|
||||
Insert: O(1)
|
||||
Delete: O(1)
|
||||
| Access | Search | Insertion | Deletion |
|
||||
| :-------: | :-------: | :-------: | :-------: |
|
||||
| O(n) | O(n) | O(1) | O(1) |
|
||||
|
||||
### Space Complexity
|
||||
|
||||
|
@ -27,12 +27,13 @@ The leaves are not drawn.
|
||||
|
||||

|
||||
|
||||
## Pseudocode
|
||||
## Pseudocode for Basic Operations
|
||||
|
||||
### Insertion
|
||||
|
||||
```text
|
||||
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
|
||||
if root = ø
|
||||
root ← node(value)
|
||||
@ -40,7 +41,9 @@ The leaves are not drawn.
|
||||
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
|
||||
@ -53,12 +56,16 @@ The leaves are not drawn.
|
||||
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
|
||||
@ -73,9 +80,12 @@ The leaves are not drawn.
|
||||
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
|
||||
@ -110,8 +120,11 @@ The leaves are not drawn.
|
||||
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 != ø
|
||||
@ -137,8 +150,11 @@ The leaves are not drawn.
|
||||
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
|
||||
@ -154,8 +170,11 @@ The leaves are not drawn.
|
||||
return findNode(root.right, value)
|
||||
end if
|
||||
end findNode
|
||||
```
|
||||
|
||||
### Find Minimum
|
||||
|
||||
```text
|
||||
findMin(root)
|
||||
Pre: root is the root node of the BST
|
||||
root = ø
|
||||
@ -165,9 +184,11 @@ The leaves are not drawn.
|
||||
end if
|
||||
findMin(root.left)
|
||||
end findMin
|
||||
```
|
||||
|
||||
### Find Maximum
|
||||
|
||||
### Find Maximim
|
||||
```text
|
||||
findMax(root)
|
||||
Pre: root is the root node of the BST
|
||||
root = ø
|
||||
@ -177,9 +198,13 @@ The leaves are not drawn.
|
||||
end if
|
||||
findMax(root.right)
|
||||
end findMax
|
||||
```
|
||||
|
||||
### Traversal
|
||||
#### InOrder
|
||||
|
||||
#### 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
|
||||
@ -189,8 +214,11 @@ The leaves are not drawn.
|
||||
inorder(root.right)
|
||||
end if
|
||||
end inorder
|
||||
```
|
||||
|
||||
#### PreOrder
|
||||
#### 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
|
||||
@ -200,7 +228,11 @@ The leaves are not drawn.
|
||||
preorder(root.right)
|
||||
end if
|
||||
end preorder
|
||||
#### PostOrder
|
||||
```
|
||||
|
||||
#### 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
|
||||
@ -210,23 +242,20 @@ The leaves are not drawn.
|
||||
yield root.value
|
||||
end if
|
||||
end postorder
|
||||
```
|
||||
|
||||
|
||||
## Big O
|
||||
## Complexities
|
||||
|
||||
### Time Complexity
|
||||
|
||||
Access: O(log(n))
|
||||
Search: O(log(n))
|
||||
Insert: O(log(n))
|
||||
Delete: O(log(n))
|
||||
|
||||
| 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)
|
||||
|
Reference in New Issue
Block a user