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):
= root_node
current_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.left
current_nodereturn insert_node(current_node, target)
else: # go left: if not exist, insert
=TreeNode(target)
current_node.leftprint(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.right
current_nodereturn insert_node(current_node, target)
else: # go right: if not exist, insert
=TreeNode(target)
current_node.rightprint(f"c[{current_node.data}]|t[{target}]: c[{current_node.data}] has no right-child: \tinserted t[{target}]cl[{current_node.right.data}]")
return
1. TreeNode
Class & insert_node
Function: Setup
Previously introduced.
2. Insert: Manual
= TreeNode(50)
root 50)
insert_node(root, print()
10)
insert_node(root, print()
10)
insert_node(root, print()
51)
insert_node(root, print()
53)
insert_node(root, print()
52)
insert_node(root, print()
52)
insert_node(root, 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
= [50,10,10,51,53,52,52]
nodes_to_insert_list = TreeNode(50)
root
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()
= [50,10,10,51,53,52,52]
nodes_to_insert_list = TreeNode(50)
root 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!