Fix pseudocode formatting.

This commit is contained in:
Oleksii Trekhleb
2018-08-14 15:46:58 +03:00
parent b6ac765c99
commit b0c9057cdb
3 changed files with 291 additions and 257 deletions

View File

@ -10,7 +10,8 @@ nodes first, before moving to the next level neighbors.
## Pseudocode
BFS(root)
```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
@ -28,11 +29,8 @@ nodes first, before moving to the next level neighbors.
root ← ø
end if
end while
end BFS
## Space and Time Complexity:
O(b<sup>d + 1</sup>)
end BFS
```
## References

View File

@ -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
to traverse the list to find the previous node, so that its link can be modified.
## Pseudocode
## Pseudocode for Basic Operations
### Insert
Add(value)
```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)
@ -32,13 +33,16 @@ 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
end Add
```
### Delete
Remove(head, value)
```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
@ -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
@ -69,10 +73,13 @@ to traverse the list to find the previous node, so that its link can be modified
return true
end if
return false
end Remove
end Remove
```
### Reverse Traversal
ReverseTraversal(tail)
```text
ReverseTraversal(tail)
Pre: tail is the node of the list to traverse
Post: the list has been traversed in reverse order
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
n ← n.previous
end while
end Reverse Traversal
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

View File

@ -27,21 +27,24 @@ The leaves are not drawn.
![Binary Search Tree](https://upload.wikimedia.org/wikipedia/commons/d/da/Binary_search_tree.svg)
## Pseudocode
## Pseudocode for Basic Operations
### Insertion
insert(value)
Pre: value has passed custome type checks for type T
```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
end insert
```
insertNode(current, value)
```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
@ -53,13 +56,17 @@ The leaves are not drawn.
else
if current.right = ø
current.right ← node(value)
else
InsertNode(current.right, value)
end if
end if
end insertNode
end insertNode
```
### 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
Post: value is either located or not
if root = ø
@ -72,11 +79,14 @@ The leaves are not drawn.
else
return contains(root.right, value)
end if
end contains
end contains
```
### Deletion
remove(value)
```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
@ -109,10 +119,13 @@ The leaves are not drawn.
end if
count ← count - 1
return true
end remove
end remove
```
### 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
root is the root node of the BST and is != ø
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)
end if
end if
end findParent
end findParent
```
### Find Node
findNode(root, value)
```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 ø
@ -153,10 +169,13 @@ The leaves are not drawn.
else
return findNode(root.right, value)
end if
end findNode
end findNode
```
### Find Minimum
findMin(root)
```text
findMin(root)
Pre: root is the root node of the BST
root = ø
Post: the smallest value in the BST is located
@ -164,11 +183,13 @@ The leaves are not drawn.
return root.value
end if
findMin(root.left)
end findMin
end findMin
```
### Find Maximum
### Find Maximim
findMax(root)
```text
findMax(root)
Pre: root is the root node of the BST
root = ø
Post: the largest value in the BST is located
@ -176,11 +197,15 @@ The leaves are not drawn.
return root.value
end if
findMax(root.right)
end findMax
end findMax
```
### Traversal
#### InOrder
inorder(root)
### 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 = ø
@ -188,10 +213,13 @@ The leaves are not drawn.
yield root.value
inorder(root.right)
end if
end inorder
end inorder
```
#### PreOrder
preorder(root)
#### 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 = ø
@ -199,9 +227,13 @@ The leaves are not drawn.
preorder(root.left)
preorder(root.right)
end if
end preorder
#### PostOrder
postorder(root)
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 = ø
@ -209,24 +241,21 @@ The leaves are not drawn.
postorder(root.right)
yield root.value
end if
end postorder
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)