Skip to content

Commit 953eaf8

Browse files
moshejstrekhleb
authored andcommitted
Update README.md (trekhleb#165)
Add Pseudocode and Big O
1 parent 7207bce commit 953eaf8

File tree

1 file changed

+200
-0
lines changed
  • src/data-structures/tree/binary-search-tree

1 file changed

+200
-0
lines changed

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

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,206 @@ 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
31+
32+
### Insertion
33+
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
43+
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
59+
60+
### Searching
61+
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
76+
77+
### Deletion
78+
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
113+
114+
### 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
140+
141+
### 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
157+
158+
### 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+
169+
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
180+
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+
214+
215+
## Big O
216+
217+
### Time Complexity
218+
219+
Access: O(log(n))
220+
Search: O(log(n))
221+
Insert: O(log(n))
222+
Delete: O(log(n))
223+
224+
225+
### Space Complexity
226+
227+
O(n)
228+
229+
30230
## References
31231

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

0 commit comments

Comments
 (0)