0% found this document useful (0 votes)
55 views

Binary Tree Algorithms

The document discusses binary tree algorithms implemented in Python code. It describes binary tree data structures and operations like searching, inserting, deleting, and traversing nodes in various orders. It provides code to demonstrate how to create binary tree nodes as objects in a class and implement the main binary tree algorithms, including insertion, searching, deletion, and traversing nodes in preorder, inorder, and postorder sequences.

Uploaded by

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

Binary Tree Algorithms

The document discusses binary tree algorithms implemented in Python code. It describes binary tree data structures and operations like searching, inserting, deleting, and traversing nodes in various orders. It provides code to demonstrate how to create binary tree nodes as objects in a class and implement the main binary tree algorithms, including insertion, searching, deletion, and traversing nodes in preorder, inorder, and postorder sequences.

Uploaded by

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

BINARY TREE

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

class BST: # Creating class binary search tree node


def __init__(self, key): # defining the initiallisation function
self.key = key
self.lchild = None
self.rchild = None
Binary tree Operations
1. Insertion function
 Conditions and steps
• Check to see whether the tree is empty
If yes insert data as the first node
If not Check the Position of the new nod
Given the data is smaller than the root the data will have to be inserted in the left
child. And Vice versa goes for the right child.
Given there exists a value on any of the children, the program will have to repeat the procedure
with the given child as the new root.
Once an empty child is identified, a node will be created and data entered.
INSERTION CODE
def insert(self, data): # function to insert elements
if self.key is None: # Checking if tree is empty
self.key = data
return
if self.key == data: # Given there is duplicate data return/ignore
return
if self.key > data: # conditioning loop to insert elements to left branch
if self.lchild: # Boolean
self.lchild.insert(data) # Given data already exists repeat loop with lchild as root
else:
self.lchild = BST(data) # Given data does not exist insert data
else:
if self.rchild: # Boolean
self.rchild.insert(data) # Given data already exists repeat loop with rchild as root
else:
self.rchild = BST(data) # Given data does not exist insert data
2. Searching
 Conditions and steps
• Check to see if data is equal to the root
• If they’re not equal, condition the program to execute the code to search for the data either in the left
node given the data is less than the root, or the right node given the data is greater than the root.
• Given the data is not found repeat the loop function with either node as the root until the value is
found.
Search Code
def search(self, val): # Function for searching elements
if self.key == val: # Comparing the root with the given data
return True
if val < self.key: # Decision loop to search value in left node
if self.lchild: # Boolean
return self.lchild.search(val) # Given there exists a value repeat the function with lchild as root
else:
return False
if val > self.key: # Decision loop to search value in right node
if self.rchild:
return self.rchild.search(val) # Given there exists a value repeat the function with rchild as root
else:
return False
3. Deletion
 Conditions and steps
• The first operation will be to find the node.
• Check to see if value is equal to root, less than the root or greater than the root.
• After the node is found the next operation will be to delete that node
• HOWEVER, given the node has children, one of either the two children will have to
replace the parent node.
• In this code it was decided to replace the root with the left child should there be two
children
Deletion code
def delete(self, value): # delete function
if self.key is None: # Checking to see if tree is empty
print("Tree is empty")
return
if value < self.key: # loop to check key on left branch
if self.lchild: #Boolean
self.lchild = self.lchild.delete(value)
elif value > self.key: # loop to check key on right branch
if self.rchild:
self.rchild = self.rchild.delete(value)
else:
if self.lchild is None: # if no node replace with key to the right
temp = self.rchild # Boolean
self = None # Deletion excecution
return temp
elif self.rchild is None: # if no node replace with key to the left
temp = self.lchild
self = None # Deletion excecution
return temp
node = self.rchild
while node.lchild:
node = node.lchild
self.key = node.key
self.rchild = self.rchild.delete(node.key) # otherwise delete the node
return self
4. Traversal

 This operation consists of 3 sub operations namely:


 Preorder
 Postorder
 Inorder
INORDER
•Check whether left subtree is present(Call method recursively)
•Print the root key
•Check whether right subtree is present( Call method recursively)

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

 def preorder(self): # Pre-order function


print(self.key, end=" ")
if self.lchild:
self.lchild.preorder()
if self.rchild:
self.rchild.preorder()
 Postorder
• Check whether left subtree is present(Call method recursively)
• Check whether right subtree is present( Call method recursively)
• Print the root key

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

You might also like