Python Program to Find the Sum of All Nodes in a Binary Tree

AmitDiwan
Updated on 25-Mar-2026 19:13:30

321 Views

When working with binary trees, finding the sum of all nodes is a common operation. We can implement this using a tree class that contains methods to build the tree and calculate the sum recursively. Each node stores data and maintains references to its children. Below is a demonstration of the same − Tree Class Implementation First, let's create a tree structure class with methods for adding nodes and calculating the sum ? class Tree_struct: def __init__(self, data=None): self.key = data ... Read More

Python Program to solve Maximum Subarray Problem using Kadane's Algorithm

AmitDiwan
Updated on 25-Mar-2026 19:13:21

422 Views

The Maximum Subarray Problem finds the contiguous subarray within a one-dimensional array of numbers that has the largest sum. Kadane's Algorithm solves this problem efficiently in O(n) time complexity using dynamic programming principles. Understanding Kadane's Algorithm Kadane's algorithm maintains two variables: the maximum sum ending at the current position and the overall maximum sum seen so far. At each position, it decides whether to extend the existing subarray or start a new one. Basic Implementation Here's a simple version that returns only the maximum sum ? def kadane_simple(arr): max_ending_here = ... Read More

Python Program to Find if Undirected Graph contains Cycle using BFS

AmitDiwan
Updated on 25-Mar-2026 19:12:53

532 Views

A cycle in an undirected graph occurs when you can start at a vertex and return to it by following edges without retracing the same edge. We can detect cycles using Breadth-First Search (BFS) by tracking parent nodes during traversal. How BFS Cycle Detection Works The algorithm uses a queue to traverse the graph level by level. For each visited vertex, we track its parent. If we encounter a visited vertex that is not the current vertex's parent, we've found a cycle. Implementation Helper Functions First, let's define a function to add edges to our ... Read More

Python Program to Find All Connected Components using BFS in an Undirected Graph

AmitDiwan
Updated on 25-Mar-2026 19:12:28

808 Views

Connected components in an undirected graph are maximal sets of vertices where every vertex is reachable from every other vertex in the same component. This article demonstrates finding all connected components using BFS (Breadth-First Search) traversal. Understanding Connected Components A connected component is a subgraph where: Every vertex can reach every other vertex in the component No vertex in the component can reach vertices outside it The component cannot be extended by adding more vertices BFS Implementation for Connected Components Here's how to find all connected components using BFS traversal ? ... Read More

Python Program to Display the Nodes of a Tree using BFS Traversal

AmitDiwan
Updated on 25-Mar-2026 19:11:37

1K+ Views

When it is required to display the nodes of a tree using the breadth first search traversal, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, perform BFS (breadth first search) and so on. An instance of the class can be created to access and use these methods. Below is a demonstration of the same ? Tree Structure Class First, let's create a simplified version that demonstrates the core BFS concept ? class Tree_struct: def __init__(self, data=None): ... Read More

Python Program to Print only Nodes in Left SubTree

AmitDiwan
Updated on 25-Mar-2026 19:11:08

361 Views

When it is required to print the nodes in the left subtree, a binary tree class can be created with methods to perform operations like setting the root node, inserting elements, and traversing the tree. This program demonstrates how to build a binary tree and display only the nodes in the left subtree. A binary tree is a hierarchical data structure where each node has at most two children: a left child and a right child. The left subtree contains all nodes that are descendants of the left child of the root node. Binary Tree Implementation ... Read More

Python Program to Find the Largest value in a Tree using Inorder Traversal

AmitDiwan
Updated on 25-Mar-2026 19:10:35

285 Views

Finding the largest value in a binary tree using inorder traversal is a common tree operation. This approach visits nodes in left-root-right order while tracking the maximum value encountered during traversal. Below is a complete implementation using a binary tree class with methods for tree construction and largest value finding ? Complete Implementation class BinaryTree: def __init__(self, key=None): self.key = key self.left = None self.right = None ... Read More

Python Program to Remove the Characters of Odd Index Values in a String

AmitDiwan
Updated on 25-Mar-2026 19:10:12

757 Views

When working with strings in Python, you might need to remove characters at odd index positions (1, 3, 5, etc.). This can be accomplished using various approaches including loops, string slicing, and list comprehensions. Understanding String Indexing In Python, string indexing starts from 0. So for a string "Hello": Index 0: 'H' (even) Index 1: 'e' (odd) Index 2: 'l' (even) Index 3: 'l' (odd) Index 4: 'o' (even) Method 1: Using While Loop This approach iterates through the string and skips characters at odd indices ? def remove_odd_index_characters(my_str): ... Read More

Python Program for Depth First Binary Tree Search using Recursion

AmitDiwan
Updated on 25-Mar-2026 19:09:53

499 Views

Depth First Search (DFS) is a tree traversal algorithm that explores nodes by going as deep as possible before backtracking. In binary trees, DFS can be implemented recursively by visiting the current node, then traversing the left subtree, and finally the right subtree. Binary Tree Class Structure First, let's define a binary tree class with methods for insertion and DFS traversal ? class BinaryTree_struct: def __init__(self, key=None): self.key = key self.left = None ... Read More

Python Program to Sort using a Binary Search Tree

AmitDiwan
Updated on 25-Mar-2026 19:09:28

878 Views

A Binary Search Tree (BST) is a tree data structure where the left subtree contains values less than the root, and the right subtree contains values greater than or equal to the root. The inorder traversal of a BST gives elements in sorted order, making it useful for sorting. How BST Sorting Works BST sorting involves two main steps: Insertion: Add elements to the BST maintaining the BST property Inorder Traversal: Visit nodes in left-root-right order to get sorted sequence Implementation We'll create two classes: BinSearchTreeNode for individual nodes and BinSearchTree for the ... Read More

Advertisements