Description:print all elements of binary search tree.
#Python Program to print all the elements in Binary Search tree.
class Node:
# Create a new Node using constructor of Node class. it have 2 parameters
def __init__(self, data):
self.left = None
self.right = None
self.node = data
#if element is less than root it will be inserted into left subtree of BST and it checks weather a left node is None or Not if left node is none then value is inserted otherwise value of left child passed in insert parameter in recursive call.
#if element is greater than root it will be inserted into subtree of BST and it checks weather a right node is None or Not if right node is none then value is inserted otherwise value of right child passed in insert parameter in recursive call.
def insert(self, data):
if self.node:
if data < self.node:
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.node:
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
else:
self.node = data
# Print the binary search tree in inorder manner .
def Print_Tree(self):
if self.left:
self.left.Print_Tree()
print(self.node, end=' ')
if self.right:
self.right.Print_Tree()
root = Node(18)
root.insert(5)
root.insert(6)
root.insert(2)
root.insert(24)
root.insert(28)
root.insert(20)
root.Print_Tree()
Output:2 5 6 18 20 24 28

## Comments