Binary Tree Algorithms
Binary Tree Algorithms
ALGORITHMS
QSN: USE PYTHON CODE TO DEMONSTRATE THE MAJOR BINARY
TREE ALGORITHMS
Basic introduction to Binary trees
A binarytree is a special type of tree data structure in which
every node can have a maximum of 2 children.
Each child being known as the Left child and the right child.
The major operations of Binary trees are
• Searching
• Inserting
• Deleting
• Traversing inorder and preorder and post order
__________________________________________________________________________________
To implement the Binary tree, the class and object concept was used
Each node will be taken as an object,
And the nodes are namely:
key/data(root)
left child
right child
Implemenation
• This is the basic implementation of how the class and object concept
was used
Inorder Code
def in_order_traversal(self): # in-order function
if self.lchild:
self.lchild.in_order_traversal()
print(self.key, end=" ")
if self.rchild:
self.rchild.in_order_traversal()
PREORDER
• Print the root key
• Check whether left subtree is present(Call method recursively)
• Check whether right subtree is present( Call method recursively)
PREORDER CODE
Postorder code
def post_order(self):
if self.lchild:
self.lchild.post_order()
if self.rchild:
self.rchild.post_order()
print(self.key, end=" ")
return