0% found this document useful (0 votes)
16 views1 page

Insertion BT

The document contains a Python implementation of a binary tree with functions to create nodes, perform an inorder traversal, and insert new values into the tree. It initializes a tree with specific nodes and demonstrates the insertion of a new node. The inorder function is used to print the keys of the tree before and after insertion.

Uploaded by

saiabhishek711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

Insertion BT

The document contains a Python implementation of a binary tree with functions to create nodes, perform an inorder traversal, and insert new values into the tree. It initializes a tree with specific nodes and demonstrates the insertion of a new node. The inorder function is used to print the keys of the tree before and after insertion.

Uploaded by

saiabhishek711
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

class Node:

def __init__(self,key):
self.left=None
self.right=None
self.key=key
def inorder(temp):
if not temp:
return
inorder(temp.left)
print(temp.key,end=' ')
inorder(temp.right)

def insert(temp,val):
if not temp:
root=Node(val)
return
q=[]
q.append(temp)
while len(q):
k=q[0]
q.pop(0)
if not k.left:
k.left=Node(val)
return
else:
q.append(k.left)
if not k.right:
k.right=Node(val)
return
else:
q.append(k.right)

if __name__=='__main__':
root=Node(1)
print("Root Created")
root.left=Node(2)
root.right=Node(3)
root.left.left=Node(10)
root.right.right=Node(11)
root.right.left=Node(12)
inorder(root)
key=25
insert(root,key)
print("tree created")
inorder(root)

You might also like