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,7 +10,8 @@ nodes first, before moving to the next level neighbors.
|
|||||||
|
|
||||||
## Pseudocode
|
## Pseudocode
|
||||||
|
|
||||||
BFS(root)
|
```text
|
||||||
|
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
|
||||||
q ← queue
|
q ← queue
|
||||||
@ -28,11 +29,8 @@ nodes first, before moving to the next level neighbors.
|
|||||||
root ← ø
|
root ← ø
|
||||||
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,11 +19,12 @@ 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
|
||||||
|
|
||||||
Add(value)
|
```text
|
||||||
|
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
|
||||||
n ← node(value)
|
n ← node(value)
|
||||||
@ -32,13 +33,16 @@ 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
|
||||||
Remove(head, value)
|
|
||||||
|
```text
|
||||||
|
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
|
||||||
Post: value is removed from the list, true; otherwise false
|
Post: value is removed from the list, true; otherwise false
|
||||||
@ -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
|
||||||
@ -69,10 +73,13 @@ to traverse the list to find the previous node, so that its link can be modified
|
|||||||
return true
|
return true
|
||||||
end if
|
end if
|
||||||
return false
|
return false
|
||||||
end Remove
|
end Remove
|
||||||
|
```
|
||||||
|
|
||||||
### Reverse Traversal
|
### Reverse Traversal
|
||||||
ReverseTraversal(tail)
|
|
||||||
|
```text
|
||||||
|
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
|
||||||
n ← tail
|
n ← tail
|
||||||
@ -80,16 +87,16 @@ to traverse the list to find the previous node, so that its link can be modified
|
|||||||
yield n.value
|
yield n.value
|
||||||
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,21 +27,24 @@ The leaves are not drawn.
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Pseudocode
|
## Pseudocode for Basic Operations
|
||||||
|
|
||||||
### Insertion
|
### Insertion
|
||||||
|
|
||||||
insert(value)
|
```text
|
||||||
Pre: value has passed custome type checks for type T
|
insert(value)
|
||||||
|
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)
|
||||||
else
|
else
|
||||||
insertNode(root, value)
|
insertNode(root, value)
|
||||||
end if
|
end if
|
||||||
end insert
|
end insert
|
||||||
|
```
|
||||||
|
|
||||||
insertNode(current, value)
|
```text
|
||||||
|
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
|
||||||
if value < current.value
|
if value < current.value
|
||||||
@ -53,13 +56,17 @@ 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
|
||||||
|
|
||||||
contains(root, value)
|
```text
|
||||||
|
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
|
||||||
if root = ø
|
if root = ø
|
||||||
@ -72,11 +79,14 @@ The leaves are not drawn.
|
|||||||
else
|
else
|
||||||
return contains(root.right, value)
|
return contains(root.right, value)
|
||||||
end if
|
end if
|
||||||
end contains
|
end contains
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Deletion
|
### Deletion
|
||||||
|
|
||||||
remove(value)
|
```text
|
||||||
|
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
|
||||||
Post: node with value is removed if found in which case yields true, otherwise false
|
Post: node with value is removed if found in which case yields true, otherwise false
|
||||||
@ -109,10 +119,13 @@ The leaves are not drawn.
|
|||||||
end if
|
end if
|
||||||
count ← count - 1
|
count ← count - 1
|
||||||
return true
|
return true
|
||||||
end remove
|
end remove
|
||||||
|
```
|
||||||
|
|
||||||
### Find Parent of Node
|
### Find Parent of Node
|
||||||
findParent(value, root)
|
|
||||||
|
```text
|
||||||
|
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 != ø
|
||||||
Post: a reference to the prent node of value if found; otherwise ø
|
Post: a reference to the prent node of value if found; otherwise ø
|
||||||
@ -136,10 +149,13 @@ The leaves are not drawn.
|
|||||||
return findParent(value, root.right)
|
return findParent(value, root.right)
|
||||||
end if
|
end if
|
||||||
end if
|
end if
|
||||||
end findParent
|
end findParent
|
||||||
|
```
|
||||||
|
|
||||||
### Find Node
|
### Find Node
|
||||||
findNode(root, value)
|
|
||||||
|
```text
|
||||||
|
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
|
||||||
Post: a reference to the node of value if found; otherwise ø
|
Post: a reference to the node of value if found; otherwise ø
|
||||||
@ -153,10 +169,13 @@ The leaves are not drawn.
|
|||||||
else
|
else
|
||||||
return findNode(root.right, value)
|
return findNode(root.right, value)
|
||||||
end if
|
end if
|
||||||
end findNode
|
end findNode
|
||||||
|
```
|
||||||
|
|
||||||
### Find Minimum
|
### Find Minimum
|
||||||
findMin(root)
|
|
||||||
|
```text
|
||||||
|
findMin(root)
|
||||||
Pre: root is the root node of the BST
|
Pre: root is the root node of the BST
|
||||||
root = ø
|
root = ø
|
||||||
Post: the smallest value in the BST is located
|
Post: the smallest value in the BST is located
|
||||||
@ -164,11 +183,13 @@ The leaves are not drawn.
|
|||||||
return root.value
|
return root.value
|
||||||
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 = ø
|
||||||
Post: the largest value in the BST is located
|
Post: the largest value in the BST is located
|
||||||
@ -176,11 +197,15 @@ The leaves are not drawn.
|
|||||||
return root.value
|
return root.value
|
||||||
end if
|
end if
|
||||||
findMax(root.right)
|
findMax(root.right)
|
||||||
end findMax
|
end findMax
|
||||||
|
```
|
||||||
|
|
||||||
### Traversal
|
### Traversal
|
||||||
#### InOrder
|
|
||||||
inorder(root)
|
#### InOrder Traversal
|
||||||
|
|
||||||
|
```text
|
||||||
|
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
|
||||||
if root = ø
|
if root = ø
|
||||||
@ -188,10 +213,13 @@ The leaves are not drawn.
|
|||||||
yield root.value
|
yield root.value
|
||||||
inorder(root.right)
|
inorder(root.right)
|
||||||
end if
|
end if
|
||||||
end inorder
|
end inorder
|
||||||
|
```
|
||||||
|
|
||||||
#### PreOrder
|
#### PreOrder Traversal
|
||||||
preorder(root)
|
|
||||||
|
```text
|
||||||
|
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
|
||||||
if root = ø
|
if root = ø
|
||||||
@ -199,9 +227,13 @@ The leaves are not drawn.
|
|||||||
preorder(root.left)
|
preorder(root.left)
|
||||||
preorder(root.right)
|
preorder(root.right)
|
||||||
end if
|
end if
|
||||||
end preorder
|
end preorder
|
||||||
#### PostOrder
|
```
|
||||||
postorder(root)
|
|
||||||
|
#### PostOrder Traversal
|
||||||
|
|
||||||
|
```text
|
||||||
|
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
|
||||||
if root = ø
|
if root = ø
|
||||||
@ -209,24 +241,21 @@ The leaves are not drawn.
|
|||||||
postorder(root.right)
|
postorder(root.right)
|
||||||
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