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,29 +10,27 @@ nodes first, before moving to the next level neighbors.
## Pseudocode ## Pseudocode
BFS(root) ```text
Pre: root is the node of the BST BFS(root)
Post: the nodes in the BST have been visited in breadth first order Pre: root is the node of the BST
q ← queue Post: the nodes in the BST have been visited in breadth first order
while root = ø q ← queue
yield root.value while root = ø
if root.left = ø yield root.value
q.enqueue(root.left) if root.left = ø
end if q.enqueue(root.left)
if root.right = ø end if
q.enqueue(root.right) if root.right = ø
end if q.enqueue(root.right)
if !q.isEmpty() end if
root ← q.dequeue() if !q.isEmpty()
else root ← q.dequeue()
root ← ø else
end if root ← ø
end while end if
end BFS end while
end BFS
## Space and Time Complexity: ```
O(b<sup>d + 1</sup>)
## References ## References

View File

@ -19,77 +19,84 @@ 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
Pre: value is the value to add to the list Add(value)
Post: value has been placed at the tail of the list Pre: value is the value to add to the list
n ← node(value) Post: value has been placed at the tail of the list
if head = ø n ← node(value)
head ← n if head = ø
tail ← n head ← n
else tail ← n
n.previous ← tail else
tail.next ← n1 n.previous ← tail
tail ← n tail.next ← n
end if tail ← n
end Add end if
end Add
```
### Delete ### Delete
Remove(head, value)
Pre: head is the head node in the list ```text
value is the value to remove from the list Remove(head, value)
Post: value is removed from the list, true; otherwise false Pre: head is the head node in the list
if head = ø value is the value to remove from the list
return false Post: value is removed from the list, true; otherwise false
end if if head = ø
if value = head.value return false
if head = tail end if
head ← ø if value = head.value
tail ← ø if head = tail
else head ← ø
head ← head.Next tail ← ø
head.previous ← ∅ else
end if head ← head.Next
return true head.previous ← ø
end if end if
n ← head.next return true
while n = ø and value = n.value end if
n ← n.next n ← head.next
end while while n = ø and value = n.value
if n = tail n ← n.next
tail ← tail.previous end while
tail.next ← ø if n = tail
return true tail ← tail.previous
else if n = ø tail.next ← ø
n.previous.next ← n.next return true
n.next.previous ← n.previous else if n = ø
return true n.previous.next ← n.next
end if n.next.previous ← n.previous
return false return true
end Remove end if
return false
end Remove
```
### Reverse Traversal ### Reverse Traversal
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
## Big O ```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 ## 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

View File

@ -27,206 +27,235 @@ The leaves are not drawn.
![Binary Search Tree](https://upload.wikimedia.org/wikipedia/commons/d/da/Binary_search_tree.svg) ![Binary Search Tree](https://upload.wikimedia.org/wikipedia/commons/d/da/Binary_search_tree.svg)
## Pseudocode ## Pseudocode for Basic Operations
### Insertion ### Insertion
insert(value) ```text
Pre: value has passed custome type checks for type T insert(value)
Post: value has been placed in the correct location in the tree Pre: value has passed custom type checks for type T
if root = ø Post: value has been placed in the correct location in the tree
root ← node(value) if root = ø
else root ← node(value)
insertNode(root, value) else
end if insertNode(root, value)
end insert end if
end insert
```
insertNode(current, value) ```text
Pre: current is the node to start from insertNode(current, value)
Post: value has been placed in the correct location in the tree Pre: current is the node to start from
if value < current.value Post: value has been placed in the correct location in the tree
if current.left = ø if value < current.value
current.left node(value) if current.left = ø
else current.left ← node(value)
InsertNode(current.left, value) else
end if InsertNode(current.left, value)
else end if
if current.right = ø else
current.right node(value) if current.right = ø
end if current.right ← node(value)
end if else
end insertNode InsertNode(current.right, value)
end if
end if
end insertNode
```
### Searching ### Searching
contains(root, value) ```text
Pre: root is the root node of the tree, value is what we would like to locate contains(root, value)
Post: value is either located or not Pre: root is the root node of the tree, value is what we would like to locate
if root = ø Post: value is either located or not
return false if root = ø
end if return false
if root.value = value end if
return true if root.value = value
else if value < root.value return true
return contains(root.left, value) else if value < root.value
else return contains(root.left, value)
return contains(root.right, value) else
end if return contains(root.right, value)
end contains end if
end contains
```
### Deletion ### Deletion
remove(value) ```text
Pre: value is the value of the node to remove, root is the node of the BST remove(value)
count is the number of items in the BST Pre: value is the value of the node to remove, root is the node of the BST
Post: node with value is removed if found in which case yields true, otherwise false count is the number of items in the BST
nodeToRemove findNode(value) Post: node with value is removed if found in which case yields true, otherwise false
if nodeToRemove = ø nodeToRemove ← findNode(value)
return false if nodeToRemove = ø
end if return false
parent findParent(value) end if
if count = 1 parent ← findParent(value)
root ø if count = 1
else if nodeToRemove.left = ø and nodeToRemove.right = ø root ← ø
if nodeToRemove.value < parent.value else if nodeToRemove.left = ø and nodeToRemove.right = ø
parent.left nodeToRemove.right if nodeToRemove.value < parent.value
else parent.left ← nodeToRemove.right
parent.right nodeToRemove.right else
end if parent.right ← nodeToRemove.right
else if nodeToRemove.left = ø and nodeToRemove.right = ø end if
if nodeToRemove.value < parent.value else if nodeToRemove.left = ø and nodeToRemove.right = ø
parent.left nodeToRemove.left if nodeToRemove.value < parent.value
else parent.left ← nodeToRemove.left
parent.right nodeToRemove.left else
end if parent.right ← nodeToRemove.left
else end if
largestValue nodeToRemove.left else
while largestValue.right = ø largestValue ← nodeToRemove.left
largestValue largestValue.right while largestValue.right = ø
end while largestValue ← largestValue.right
findParent(largestValue.value).right ø end while
nodeToRemove.value largestValue.value findParent(largestValue.value).right ← ø
end if nodeToRemove.value ← largestValue.value
count count - 1 end if
return true count ← count - 1
end remove return true
end remove
```
### Find Parent of Node ### Find Parent of Node
findParent(value, root)
Pre: value is the value of the node we want to find the parent of ```text
root is the root node of the BST and is != ø findParent(value, root)
Post: a reference to the prent node of value if found; otherwise ø Pre: value is the value of the node we want to find the parent of
if value = root.value root is the root node of the BST and is != ø
return ø Post: a reference to the prent node of value if found; otherwise ø
end if if value = root.value
if value < root.value return ø
if root.left = ø end if
return ø if value < root.value
else if root.left.value = value if root.left = ø
return root return ø
else else if root.left.value = value
return findParent(value, root.left) return root
end if else
else return findParent(value, root.left)
if root.right = ø end if
return ø else
else if root.right.value = value if root.right = ø
return root return ø
else else if root.right.value = value
return findParent(value, root.right) return root
end if else
end if return findParent(value, root.right)
end findParent end if
end if
end findParent
```
### Find Node ### Find Node
findNode(root, value)
Pre: value is the value of the node we want to find the parent of ```text
root is the root node of the BST findNode(root, value)
Post: a reference to the node of value if found; otherwise ø Pre: value is the value of the node we want to find the parent of
if root = ø root is the root node of the BST
return ø Post: a reference to the node of value if found; otherwise ø
end if if root = ø
if root.value = value return ø
return root end if
else if value < root.value if root.value = value
return findNode(root.left, value) return root
else else if value < root.value
return findNode(root.right, value) return findNode(root.left, value)
end if else
end findNode return findNode(root.right, value)
end if
end findNode
```
### Find Minimum ### Find Minimum
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
```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 Maximim ### Find Maximum
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 ```text
#### InOrder findMax(root)
inorder(root) Pre: root is the root node of the BST
Pre: root is the root node of the BST root = ø
Post: the nodes in the BST have been visited in inorder Post: the largest value in the BST is located
if root = ø if root.right = ø
inorder(root.left) return root.value
yield root.value end if
inorder(root.right) findMax(root.right)
end if end findMax
end inorder ```
#### PreOrder ### Traversal
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
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
#### InOrder Traversal
## Big O ```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 ### 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)