mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-15 05:48:03 +00:00
Updated bst to keep track of its parent and to return the node it inserts.
This commit is contained in:
@@ -4,6 +4,7 @@ class Node(object):
|
||||
self.data = data
|
||||
self.left = None
|
||||
self.right = None
|
||||
self.parent = None
|
||||
|
||||
def __str__(self):
|
||||
return str(self.data)
|
||||
@@ -13,10 +14,14 @@ def insert(root, data):
|
||||
if data <= root.data:
|
||||
if root.left is None:
|
||||
root.left = Node(data)
|
||||
root.left.parent = root
|
||||
return root.left
|
||||
else:
|
||||
insert(root.left, data)
|
||||
return insert(root.left, data)
|
||||
else:
|
||||
if root.right is None:
|
||||
root.right = Node(data)
|
||||
root.right.parent = root
|
||||
return root.right
|
||||
else:
|
||||
insert(root.right, data)
|
||||
return insert(root.right, data)
|
||||
Reference in New Issue
Block a user