From 8e8f934d12a6503d458cea908c07cae20b68fc95 Mon Sep 17 00:00:00 2001 From: marina <138340846+bt3gl-cryptographer@users.noreply.github.com> Date: Thu, 3 Aug 2023 13:47:22 -0700 Subject: [PATCH] Create bst_insert_node.py --- trees/bst_insert_node.py | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 trees/bst_insert_node.py diff --git a/trees/bst_insert_node.py b/trees/bst_insert_node.py new file mode 100644 index 0000000..79b3685 --- /dev/null +++ b/trees/bst_insert_node.py @@ -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 +