DSA 43: Binary Search Trees - Sir-Insert-A-Lot [Part 6]

Insert a List of Integers into a Binary-Tree
data structures
algorithms
Author

Tony Phung

Published

February 27, 2025

1. TreeNode Class & insert_node Function: Setup

Previously introduced.

class TreeNode:
    def __init__(self, data, left=None, right=None):
        self.data=data
        self.left=left
        self.right=right

def insert_node(root_node: TreeNode, target: int):
    current_node = root_node
    if current_node.data==target:
        print(f"c[{current_node.data}]|t[{target}]: \t\t\t\t\t\t\tNo Duplicate Allowed!")
        return   
    
    if target<current_node.data: # t<c -> left
        if current_node.left: # go left: if exists 
            #   50
            #  1
            print(f"c[{current_node.data}]|t[{target}]: c[{current_node.data}] has 1 left-child: \tgoing left c[{current_node.left.data}]")
            current_node=current_node.left
            return insert_node(current_node, target)
        else: # go left: if not exist, insert
            current_node.left=TreeNode(target)
            print(f"c[{current_node.data}]|t[{target}]: c[{current_node.data}] has no left-child: \tinserted t[{target}]cl[{current_node.left.data}]")
            return
    else:  #t<c -> go right
        if current_node.right: # go right: if exists 
            #   50
            #  1  51
            print(f"c[{current_node.data}]|t[{target}]: c[{current_node.data}] has 1 right-child: \tgoing right c[{current_node.right.data}]")
            current_node=current_node.right
            return insert_node(current_node, target)
        else: # go right: if not exist, insert
            current_node.right=TreeNode(target)
            print(f"c[{current_node.data}]|t[{target}]: c[{current_node.data}] has no right-child: \tinserted t[{target}]cl[{current_node.right.data}]")
            return

2. Insert: Manual

root = TreeNode(50)
insert_node(root, 50) 
print()
insert_node(root, 10) 
print()
insert_node(root, 10) 
print()
insert_node(root, 51) 
print()
insert_node(root, 53) 
print()
insert_node(root, 52) 
print()
insert_node(root, 52) 
print()
c[50]|t[50]:                            No Duplicate Allowed!

c[50]|t[10]: c[50] has no left-child:   inserted t[10]cl[10]

c[50]|t[10]: c[50] has 1 left-child:    going left c[10]
c[10]|t[10]:                            No Duplicate Allowed!

c[50]|t[51]: c[50] has no right-child:  inserted t[51]cl[51]

c[50]|t[53]: c[50] has 1 right-child:   going right c[51]
c[51]|t[53]: c[51] has no right-child:  inserted t[53]cl[53]

c[50]|t[52]: c[50] has 1 right-child:   going right c[51]
c[51]|t[52]: c[51] has 1 right-child:   going right c[53]
c[53]|t[52]: c[53] has no left-child:   inserted t[52]cl[52]

c[50]|t[52]: c[50] has 1 right-child:   going right c[51]
c[51]|t[52]: c[51] has 1 right-child:   going right c[53]
c[53]|t[52]: c[53] has 1 left-child:    going left c[52]
c[52]|t[52]:                            No Duplicate Allowed!

3. Insert: for-loop

nodes_to_insert_list = [50,10,10,51,53,52,52]
root = TreeNode(50)

for node in nodes_to_insert_list:
    insert_node(root, node)
    print()
c[50]|t[50]:                            No Duplicate Allowed!

c[50]|t[10]: c[50] has no left-child:   inserted t[10]cl[10]

c[50]|t[10]: c[50] has 1 left-child:    going left c[10]
c[10]|t[10]:                            No Duplicate Allowed!

c[50]|t[51]: c[50] has no right-child:  inserted t[51]cl[51]

c[50]|t[53]: c[50] has 1 right-child:   going right c[51]
c[51]|t[53]: c[51] has no right-child:  inserted t[53]cl[53]

c[50]|t[52]: c[50] has 1 right-child:   going right c[51]
c[51]|t[52]: c[51] has 1 right-child:   going right c[53]
c[53]|t[52]: c[53] has no left-child:   inserted t[52]cl[52]

c[50]|t[52]: c[50] has 1 right-child:   going right c[51]
c[51]|t[52]: c[51] has 1 right-child:   going right c[53]
c[53]|t[52]: c[53] has 1 left-child:    going left c[52]
c[52]|t[52]:                            No Duplicate Allowed!

4. Insert: Function

def insert_nodes_list(nodes_list: list[int]):
    for node in nodes_list:
        insert_node(root, node)
        print()

nodes_to_insert_list = [50,10,10,51,53,52,52]
root = TreeNode(50)
insert_nodes_list(nodes_to_insert_list)
c[50]|t[50]:                            No Duplicate Allowed!

c[50]|t[10]: c[50] has no left-child:   inserted t[10]cl[10]

c[50]|t[10]: c[50] has 1 left-child:    going left c[10]
c[10]|t[10]:                            No Duplicate Allowed!

c[50]|t[51]: c[50] has no right-child:  inserted t[51]cl[51]

c[50]|t[53]: c[50] has 1 right-child:   going right c[51]
c[51]|t[53]: c[51] has no right-child:  inserted t[53]cl[53]

c[50]|t[52]: c[50] has 1 right-child:   going right c[51]
c[51]|t[52]: c[51] has 1 right-child:   going right c[53]
c[53]|t[52]: c[53] has no left-child:   inserted t[52]cl[52]

c[50]|t[52]: c[50] has 1 right-child:   going right c[51]
c[51]|t[52]: c[51] has 1 right-child:   going right c[53]
c[53]|t[52]: c[53] has 1 left-child:    going left c[52]
c[52]|t[52]:                            No Duplicate Allowed!