Create bst_insert_node.py

This commit is contained in:
marina 2023-08-03 13:47:22 -07:00 committed by GitHub
parent fb8081ae42
commit 8e8f934d12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

42
trees/bst_insert_node.py Normal file
View file

@ -0,0 +1,42 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def bst_insert_iterative(root, val):
new_node = Node(val)
this_node = root
while this_node:
if val > this_node.val:
if not this_node.right:
this_node.right = new_node
return root
else:
this_node = this_node.right
else:
if not this_node.left:
this_node.left = new_node
return this_node
else:
this_node = this_node.left
return new_node
def bst_insert_recursive(root, val):
if not root:
return Node(val)
if val > root.val:
root.right = self.insertIntoBST(root.right, val)
else:
root.left = self.insertIntoBST(root.left, val)
return root