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

‎src/data-structures/tree/binary-search-tree/README.md

+202-173
Original file line numberDiff line numberDiff line change
@@ -27,206 +27,235 @@ The leaves are not drawn.
2727

2828
![Binary Search Tree](https://upload.wikimedia.org/wikipedia/commons/d/da/Binary_search_tree.svg)
2929

30-
## Pseudocode
30+
## Pseudocode for Basic Operations
3131

3232
### Insertion
3333

34-
insert(value)
35-
Pre: value has passed custome type checks for type T
36-
Post: value has been placed in the correct location in the tree
37-
if root = ø
38-
root ← node(value)
39-
else
40-
insertNode(root, value)
41-
end if
42-
end insert
34+
```text
35+
insert(value)
36+
Pre: value has passed custom type checks for type T
37+
Post: value has been placed in the correct location in the tree
38+
if root = ø
39+
root ← node(value)
40+
else
41+
insertNode(root, value)
42+
end if
43+
end insert
44+
```
4345

44-
insertNode(current, value)
45-
Pre: current is the node to start from
46-
Post: value has been placed in the correct location in the tree
47-
if value < current.value
48-
if current.left = ø
49-
current.left ← node(value)
50-
else
51-
InsertNode(current.left, value)
52-
end if
53-
else
54-
if current.right = ø
55-
current.right ← node(value)
56-
end if
57-
end if
58-
end insertNode
46+
```text
47+
insertNode(current, value)
48+
Pre: current is the node to start from
49+
Post: value has been placed in the correct location in the tree
50+
if value < current.value
51+
if current.left = ø
52+
current.left ← node(value)
53+
else
54+
InsertNode(current.left, value)
55+
end if
56+
else
57+
if current.right = ø
58+
current.right ← node(value)
59+
else
60+
InsertNode(current.right, value)
61+
end if
62+
end if
63+
end insertNode
64+
```
5965

6066
### Searching
6167

62-
contains(root, value)
63-
Pre: root is the root node of the tree, value is what we would like to locate
64-
Post: value is either located or not
65-
if root = ø
66-
return false
67-
end if
68-
if root.value = value
69-
return true
70-
else if value < root.value
71-
return contains(root.left, value)
72-
else
73-
return contains(root.right, value)
74-
end if
75-
end contains
68+
```text
69+
contains(root, value)
70+
Pre: root is the root node of the tree, value is what we would like to locate
71+
Post: value is either located or not
72+
if root = ø
73+
return false
74+
end if
75+
if root.value = value
76+
return true
77+
else if value < root.value
78+
return contains(root.left, value)
79+
else
80+
return contains(root.right, value)
81+
end if
82+
end contains
83+
```
84+
7685
7786
### Deletion
7887

79-
remove(value)
80-
Pre: value is the value of the node to remove, root is the node of the BST
81-
count is the number of items in the BST
82-
Post: node with value is removed if found in which case yields true, otherwise false
83-
nodeToRemove ← findNode(value)
84-
if nodeToRemove = ø
85-
return false
86-
end if
87-
parent ← findParent(value)
88-
if count = 1
89-
root ← ø
90-
else if nodeToRemove.left = ø and nodeToRemove.right = ø
91-
if nodeToRemove.value < parent.value
92-
parent.left ← nodeToRemove.right
93-
else
94-
parent.right ← nodeToRemove.right
95-
end if
96-
else if nodeToRemove.left = ø and nodeToRemove.right = ø
97-
if nodeToRemove.value < parent.value
98-
parent.left ← nodeToRemove.left
99-
else
100-
parent.right ← nodeToRemove.left
101-
end if
102-
else
103-
largestValue ← nodeToRemove.left
104-
while largestValue.right = ø
105-
largestValue ← largestValue.right
106-
end while
107-
findParent(largestValue.value).right ← ø
108-
nodeToRemove.value ← largestValue.value
109-
end if
110-
count ← count - 1
111-
return true
112-
end remove
88+
```text
89+
remove(value)
90+
Pre: value is the value of the node to remove, root is the node of the BST
91+
count is the number of items in the BST
92+
Post: node with value is removed if found in which case yields true, otherwise false
93+
nodeToRemove ← findNode(value)
94+
if nodeToRemove = ø
95+
return false
96+
end if
97+
parent ← findParent(value)
98+
if count = 1
99+
root ← ø
100+
else if nodeToRemove.left = ø and nodeToRemove.right = ø
101+
if nodeToRemove.value < parent.value
102+
parent.left ← nodeToRemove.right
103+
else
104+
parent.right ← nodeToRemove.right
105+
end if
106+
else if nodeToRemove.left = ø and nodeToRemove.right = ø
107+
if nodeToRemove.value < parent.value
108+
parent.left ← nodeToRemove.left
109+
else
110+
parent.right ← nodeToRemove.left
111+
end if
112+
else
113+
largestValue ← nodeToRemove.left
114+
while largestValue.right = ø
115+
largestValue ← largestValue.right
116+
end while
117+
findParent(largestValue.value).right ← ø
118+
nodeToRemove.value ← largestValue.value
119+
end if
120+
count ← count - 1
121+
return true
122+
end remove
123+
```
113124

114125
### Find Parent of Node
115-
findParent(value, root)
116-
Pre: value is the value of the node we want to find the parent of
117-
root is the root node of the BST and is != ø
118-
Post: a reference to the prent node of value if found; otherwise ø
119-
if value = root.value
120-
return ø
121-
end if
122-
if value < root.value
123-
if root.left = ø
124-
return ø
125-
else if root.left.value = value
126-
return root
127-
else
128-
return findParent(value, root.left)
129-
end if
130-
else
131-
if root.right = ø
132-
return ø
133-
else if root.right.value = value
134-
return root
135-
else
136-
return findParent(value, root.right)
137-
end if
138-
end if
139-
end findParent
126+
127+
```text
128+
findParent(value, root)
129+
Pre: value is the value of the node we want to find the parent of
130+
root is the root node of the BST and is != ø
131+
Post: a reference to the prent node of value if found; otherwise ø
132+
if value = root.value
133+
return ø
134+
end if
135+
if value < root.value
136+
if root.left = ø
137+
return ø
138+
else if root.left.value = value
139+
return root
140+
else
141+
return findParent(value, root.left)
142+
end if
143+
else
144+
if root.right = ø
145+
return ø
146+
else if root.right.value = value
147+
return root
148+
else
149+
return findParent(value, root.right)
150+
end if
151+
end if
152+
end findParent
153+
```
140154

141155
### Find Node
142-
findNode(root, value)
143-
Pre: value is the value of the node we want to find the parent of
144-
root is the root node of the BST
145-
Post: a reference to the node of value if found; otherwise ø
146-
if root = ø
147-
return ø
148-
end if
149-
if root.value = value
150-
return root
151-
else if value < root.value
152-
return findNode(root.left, value)
153-
else
154-
return findNode(root.right, value)
155-
end if
156-
end findNode
156+
157+
```text
158+
findNode(root, value)
159+
Pre: value is the value of the node we want to find the parent of
160+
root is the root node of the BST
161+
Post: a reference to the node of value if found; otherwise ø
162+
if root = ø
163+
return ø
164+
end if
165+
if root.value = value
166+
return root
167+
else if value < root.value
168+
return findNode(root.left, value)
169+
else
170+
return findNode(root.right, value)
171+
end if
172+
end findNode
173+
```
157174

158175
### Find Minimum
159-
findMin(root)
160-
Pre: root is the root node of the BST
161-
root = ø
162-
Post: the smallest value in the BST is located
163-
if root.left = ø
164-
return root.value
165-
end if
166-
findMin(root.left)
167-
end findMin
168-
176+
177+
```text
178+
findMin(root)
179+
Pre: root is the root node of the BST
180+
root = ø
181+
Post: the smallest value in the BST is located
182+
if root.left = ø
183+
return root.value
184+
end if
185+
findMin(root.left)
186+
end findMin
187+
```
169188

170-
### Find Maximim
171-
findMax(root)
172-
Pre: root is the root node of the BST
173-
root = ø
174-
Post: the largest value in the BST is located
175-
if root.right = ø
176-
return root.value
177-
end if
178-
findMax(root.right)
179-
end findMax
189+
### Find Maximum
190+
191+
```text
192+
findMax(root)
193+
Pre: root is the root node of the BST
194+
root = ø
195+
Post: the largest value in the BST is located
196+
if root.right = ø
197+
return root.value
198+
end if
199+
findMax(root.right)
200+
end findMax
201+
```
180202

181-
### Traversal
182-
#### InOrder
183-
inorder(root)
184-
Pre: root is the root node of the BST
185-
Post: the nodes in the BST have been visited in inorder
186-
if root = ø
187-
inorder(root.left)
188-
yield root.value
189-
inorder(root.right)
190-
end if
191-
end inorder
192-
193-
#### PreOrder
194-
preorder(root)
195-
Pre: root is the root node of the BST
196-
Post: the nodes in the BST have been visited in preorder
197-
if root = ø
198-
yield root.value
199-
preorder(root.left)
200-
preorder(root.right)
201-
end if
202-
end preorder
203-
#### PostOrder
204-
postorder(root)
205-
Pre: root is the root node of the BST
206-
Post: the nodes in the BST have been visited in postorder
207-
if root = ø
208-
postorder(root.left)
209-
postorder(root.right)
210-
yield root.value
211-
end if
212-
end postorder
213-
203+
### Traversal
204+
205+
#### InOrder Traversal
206+
207+
```text
208+
inorder(root)
209+
Pre: root is the root node of the BST
210+
Post: the nodes in the BST have been visited in inorder
211+
if root = ø
212+
inorder(root.left)
213+
yield root.value
214+
inorder(root.right)
215+
end if
216+
end inorder
217+
```
218+
219+
#### PreOrder Traversal
220+
221+
```text
222+
preorder(root)
223+
Pre: root is the root node of the BST
224+
Post: the nodes in the BST have been visited in preorder
225+
if root = ø
226+
yield root.value
227+
preorder(root.left)
228+
preorder(root.right)
229+
end if
230+
end preorder
231+
```
232+
233+
#### PostOrder Traversal
234+
235+
```text
236+
postorder(root)
237+
Pre: root is the root node of the BST
238+
Post: the nodes in the BST have been visited in postorder
239+
if root = ø
240+
postorder(root.left)
241+
postorder(root.right)
242+
yield root.value
243+
end if
244+
end postorder
245+
```
214246
215-
## Big O
247+
## Complexities
216248

217249
### Time Complexity
218250

219-
Access: O(log(n))
220-
Search: O(log(n))
221-
Insert: O(log(n))
222-
Delete: O(log(n))
223-
251+
| Access | Search | Insertion | Deletion |
252+
| :-------: | :-------: | :-------: | :-------: |
253+
| O(log(n)) | O(log(n)) | O(log(n)) | O(log(n)) |
224254

225255
### Space Complexity
226256

227257
O(n)
228258

229-
230259
## References
231260

232261
- [Wikipedia](https://en.wikipedia.org/wiki/Binary_search_tree)

0 commit comments

Comments
 (0)
Please sign in to comment.