mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
40 lines
718 B
Python
40 lines
718 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# author: bt3gl
|
|
|
|
|
|
def bst_insert_iterative(root, val):
|
|
|
|
node = root
|
|
while node:
|
|
|
|
if val > node.val:
|
|
if not node.right:
|
|
node.right = Node(val)
|
|
break
|
|
else:
|
|
node = node.right
|
|
|
|
else:
|
|
if not node.left:
|
|
node.left = Node(val)
|
|
break
|
|
else:
|
|
node = node.left
|
|
|
|
return root
|
|
|
|
|
|
def bst_insert_recursive(root, val):
|
|
|
|
if root is None:
|
|
return Node(val)
|
|
|
|
if val > root.val:
|
|
root.right = self.bst_insert_recursive(root.right, val)
|
|
|
|
else:
|
|
root.left = self.bst_insert_recursive(root.left, val)
|
|
|
|
return root
|