Skip to content

Commit b0c9057

Browse files
committedAug 14, 2018
Fix pseudocode formatting.
1 parent b6ac765 commit b0c9057

File tree

3 files changed

+290
-256
lines changed

3 files changed

+290
-256
lines changed
 

‎src/algorithms/tree/breadth-first-search/README.md

+21-23
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,27 @@ nodes first, before moving to the next level neighbors.
1010

1111
## Pseudocode
1212

13-
BFS(root)
14-
Pre: root is the node of the BST
15-
Post: the nodes in the BST have been visited in breadth first order
16-
q ← queue
17-
while root = ø
18-
yield root.value
19-
if root.left = ø
20-
q.enqueue(root.left)
21-
end if
22-
if root.right = ø
23-
q.enqueue(root.right)
24-
end if
25-
if !q.isEmpty()
26-
root ← q.dequeue()
27-
else
28-
root ← ø
29-
end if
30-
end while
31-
end BFS
32-
33-
## Space and Time Complexity:
34-
35-
O(b<sup>d + 1</sup>)
13+
```text
14+
BFS(root)
15+
Pre: root is the node of the BST
16+
Post: the nodes in the BST have been visited in breadth first order
17+
q ← queue
18+
while root = ø
19+
yield root.value
20+
if root.left = ø
21+
q.enqueue(root.left)
22+
end if
23+
if root.right = ø
24+
q.enqueue(root.right)
25+
end if
26+
if !q.isEmpty()
27+
root ← q.dequeue()
28+
else
29+
root ← ø
30+
end if
31+
end while
32+
end BFS
33+
```
3634

3735
## References
3836

‎src/data-structures/doubly-linked-list/README.md

+67-60
Original file line numberDiff line numberDiff line change
@@ -19,77 +19,84 @@ potentially more efficient (for nodes other than first nodes) because there
1919
is no need to keep track of the previous node during traversal or no need
2020
to traverse the list to find the previous node, so that its link can be modified.
2121

22-
## Pseudocode
22+
## Pseudocode for Basic Operations
2323

2424
### Insert
2525

26-
Add(value)
27-
Pre: value is the value to add to the list
28-
Post: value has been placed at the tail of the list
29-
n ← node(value)
30-
if head = ø
31-
head ← n
32-
tail ← n
33-
else
34-
n.previous ← tail
35-
tail.next ← n1
36-
tail ← n
37-
end if
38-
end Add
26+
```text
27+
Add(value)
28+
Pre: value is the value to add to the list
29+
Post: value has been placed at the tail of the list
30+
n ← node(value)
31+
if head = ø
32+
head ← n
33+
tail ← n
34+
else
35+
n.previous ← tail
36+
tail.next ← n
37+
tail ← n
38+
end if
39+
end Add
40+
```
3941

4042
### Delete
41-
Remove(head, value)
42-
Pre: head is the head node in the list
43-
value is the value to remove from the list
44-
Post: value is removed from the list, true; otherwise false
45-
if head = ø
46-
return false
47-
end if
48-
if value = head.value
49-
if head = tail
50-
head ← ø
51-
tail ← ø
52-
else
53-
head ← head.Next
54-
head.previous ← ∅
55-
end if
56-
return true
57-
end if
58-
n ← head.next
59-
while n = ø and value = n.value
60-
n ← n.next
61-
end while
62-
if n = tail
63-
tail ← tail.previous
64-
tail.next ← ø
65-
return true
66-
else if n = ø
67-
n.previous.next ← n.next
68-
n.next.previous ← n.previous
69-
return true
70-
end if
71-
return false
72-
end Remove
43+
44+
```text
45+
Remove(head, value)
46+
Pre: head is the head node in the list
47+
value is the value to remove from the list
48+
Post: value is removed from the list, true; otherwise false
49+
if head = ø
50+
return false
51+
end if
52+
if value = head.value
53+
if head = tail
54+
head ← ø
55+
tail ← ø
56+
else
57+
head ← head.Next
58+
head.previous ← ø
59+
end if
60+
return true
61+
end if
62+
n ← head.next
63+
while n = ø and value = n.value
64+
n ← n.next
65+
end while
66+
if n = tail
67+
tail ← tail.previous
68+
tail.next ← ø
69+
return true
70+
else if n = ø
71+
n.previous.next ← n.next
72+
n.next.previous ← n.previous
73+
return true
74+
end if
75+
return false
76+
end Remove
77+
```
7378

7479
### Reverse Traversal
75-
ReverseTraversal(tail)
76-
Pre: tail is the node of the list to traverse
77-
Post: the list has been traversed in reverse order
78-
n ← tail
79-
while n = ø
80-
yield n.value
81-
n ← n.previous
82-
end while
83-
end Reverse Traversal
80+
81+
```text
82+
ReverseTraversal(tail)
83+
Pre: tail is the node of the list to traverse
84+
Post: the list has been traversed in reverse order
85+
n ← tail
86+
while n = ø
87+
yield n.value
88+
n ← n.previous
89+
end while
90+
end Reverse Traversal
91+
```
8492

85-
## Big O
93+
## Complexities
8694

8795
## Time Complexity
8896

89-
Access: O(n)
90-
Search: O(n)
91-
Insert: O(1)
92-
Delete: O(1)
97+
| Access | Search | Insertion | Deletion |
98+
| :-------: | :-------: | :-------: | :-------: |
99+
| O(n) | O(n) | O(1) | O(1) |
93100

94101
### Space Complexity
95102

0 commit comments

Comments
 (0)
Please sign in to comment.