Skip to content

Commit 1bb35f0

Browse files
committed
Fixed bugs with parent pointer tree insert and search functions
1 parent 77b7a36 commit 1bb35f0

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

pydatastructs/trees/m_ary_trees.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,15 @@ def __new__(cls, key=None, root_data=None, comp=None, **kwargs):
204204
raise_if_backend_is_not_python(
205205
cls, kwargs.get('backend', Backend.PYTHON))
206206
obj = object.__new__(cls)
207-
if key is None and root_data is not None:
208-
raise ValueError('Key required.')
209-
key = None if root_data is None else key
207+
208+
# Empty tree
209+
if key is None:
210+
obj.root_idx = None
211+
obj.tree, obj.size = ArrayForTrees(ParentPointerTreeNode, []), 0
212+
obj.comparator = lambda key1, key2: key1 < key2 \
213+
if comp is None else comp
214+
return obj
215+
210216
root = ParentPointerTreeNode(key, root_data)
211217
root.is_root = True
212218
obj.root_idx = 0
@@ -238,16 +244,29 @@ def insert(self, parent_key, key, data=None):
238244
239245
None
240246
"""
241-
if parent_key is None:
242-
raise ValueError("Parent key is required.")
243247
if key is None:
244248
raise ValueError("Key is required.")
249+
250+
# Empty tree
251+
if self.size == 0:
252+
if parent_key is not None:
253+
raise ValueError("Parent key should be None.")
254+
255+
root = ParentPointerTreeNode(key, data)
256+
root.is_root = True
257+
self.tree.append(root)
258+
self.size += 1
259+
return
260+
261+
if parent_key is None:
262+
raise ValueError("Parent key is required.")
263+
245264
if self.search(key) is not None:
246265
raise ValueError("Key already exists.")
247-
248-
parent_node = self.search(parent_key, parent=True)
266+
267+
parent_node = self.search(parent_key)
249268
new_node = ParentPointerTreeNode(key, data, parent_node)
250-
269+
251270
self.tree.append(new_node)
252271
self.size += 1
253272

@@ -278,11 +297,12 @@ def delete(self, key):
278297
The node is deleted means that the connection to that
279298
node are removed but the it is still in tree.
280299
"""
281-
for idx in range(self.size):
282-
if self.tree[idx].key == key:
300+
for idx in range(self.tree._last_pos_filled + 1):
301+
if self.tree[idx] and self.tree[idx].key == key:
283302
self.tree.delete(idx)
303+
self.size -= 1
284304
return True
285-
305+
286306
return None
287307

288308
def search(self, key, **kwargs):
@@ -295,7 +315,7 @@ def search(self, key, **kwargs):
295315
296316
key
297317
The key for searching.
298-
parent: bool
318+
get_parent: bool
299319
If true then returns node of the
300320
parent of the node with the passed
301321
key.
@@ -312,19 +332,19 @@ def search(self, key, **kwargs):
312332
"""
313333
parent = kwargs.get('parent', False)
314334

315-
for idx in range(self.size):
335+
for idx in range(self.tree._last_pos_filled + 1):
316336
node = self.tree[idx]
317337
if node is not None and node.key == key:
318338
if parent:
319339
return node.parent
320340
return node
321341

322342
return None
323-
343+
324344

325345
def least_common_ancestor(self, first_child_key, second_child_key):
326346
"""
327-
Finds the least common ancestor of two nodes in
347+
Finds the least common ancestor of two nodes in
328348
the tree.
329349
330350
Parameters
@@ -343,16 +363,13 @@ def least_common_ancestor(self, first_child_key, second_child_key):
343363
None
344364
If either of the nodes doesn't exist in the tree.
345365
"""
346-
first_node_idx = self.search(first_child_key)
347-
second_node_idx = self.search(second_child_key)
366+
first_node = self.search(first_child_key)
367+
second_node = self.search(second_child_key)
348368

349369
# One or both nodes do not exist
350-
if first_node_idx is None or second_node_idx is None:
351-
return None
370+
if first_node is None or second_node is None:
371+
return None
352372

353-
first_node = self.tree[first_node_idx]
354-
second_node = self.tree[second_node_idx]
355-
356373
first_ancestors = set()
357374

358375
while first_node is not None:
@@ -367,9 +384,9 @@ def least_common_ancestor(self, first_child_key, second_child_key):
367384
return None # No common ancestor found
368385

369386
def __str__(self):
370-
to_be_printed = ['' for i in range(self.tree._last_pos_filled + 1)]
387+
to_be_printed = []
371388
for i in range(self.tree._last_pos_filled + 1):
372389
if self.tree[i] is not None:
373390
node = self.tree[i]
374-
to_be_printed[i] = (node.key, node.data, node.parent)
391+
to_be_printed.append((node.key, node.data, str(node.parent)))
375392
return str(to_be_printed)

0 commit comments

Comments
 (0)