AmitDiwan

AmitDiwan

8,392 Articles Published

Articles by AmitDiwan

Page 131 of 840

Python Program to Count Number of Non Leaf Nodes of a given Tree

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 280 Views

When it is required to find the count of the non-leaf nodes in a Tree, a Tree_structure class is created with methods to set a root value, add nodes, and count non-leaf nodes. A non-leaf node is any node that has at least one child. Below is a demonstration of the same − What are Non-Leaf Nodes? In a tree data structure: Leaf nodes − nodes with no children Non-leaf nodes − nodes that have at least one child (also called internal nodes) 34 Non-leaf ...

Read More

Python Program to Count Number of Leaf Node in a Tree

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 726 Views

A leaf node in a tree is a node that has no children. In this program, we'll create a tree structure and implement a method to count all leaf nodes using recursive traversal. Understanding Leaf Nodes A leaf node is identified by having an empty children list. We'll use a helper function to recursively traverse the tree and collect all leaf nodes ? Tree Structure Implementation class Tree_structure: def __init__(self, data=None): self.key = data self.children ...

Read More

Python Program to Implement Queues using Stacks

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 459 Views

When it is required to implement a queue using stacks, we can create a queue class that uses two stack instances. This approach leverages the LIFO (Last In First Out) nature of stacks to achieve FIFO (First In First Out) behavior of queues. A queue has two main operations: enqueue (add element) and dequeue (remove element). We'll use two stacks − one for input operations and another for output operations. How It Works The algorithm uses two stacks: Input Stack: Handles all enqueue operations Output Stack: Handles all dequeue operations When dequeuing, if output stack ...

Read More

Python Program to Implement Stack Using Two Queues

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 643 Views

A stack follows Last-In-First-Out (LIFO) principle, while a queue follows First-In-First-Out (FIFO). This tutorial shows how to implement a stack using two queues, demonstrating an interesting data structure transformation. Stack and Queue Classes We need two classes: one for queue operations and another for stack implementation using two queues ? class Queue_structure: def __init__(self): self.items = [] self.size = 0 def check_empty(self): ...

Read More

Python Program to Reverse a Stack using Recursion

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 734 Views

A stack is a Last-In-First-Out (LIFO) data structure. To reverse a stack using recursion, we need two key functions: one to reverse the stack and another to insert elements at the bottom of the stack. Stack Implementation First, let's create a stack class with basic operations ? class Stack: def __init__(self): self.items = [] def is_empty(self): return len(self.items) == 0 ...

Read More

Python Program to Select the nth Largest Element from a List in Expected Linear Time

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 469 Views

When it is required to select the nth largest element from a list in linear time complexity, we can use the QuickSelect algorithm. This algorithm uses a partitioning approach similar to QuickSort but only recurses on one side, achieving expected O(n) time complexity. The algorithm works by partitioning the list around a pivot element and then deciding which partition contains the nth largest element ? How the Algorithm Works The QuickSelect algorithm consists of two main functions: select_largest − The main function that finds the ith largest element start_partition − A helper function that partitions ...

Read More

Python Program to Select the nth Smallest Element from a List in Expected Linear Time

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 542 Views

Finding the nth smallest element from a list in expected linear time requires implementing the Quickselect algorithm. This algorithm uses partitioning similar to quicksort but only searches one partition, achieving O(n) average time complexity. The algorithm works by selecting a pivot element, partitioning the list around it, and then recursively searching only the partition that contains the nth smallest element. Algorithm Implementation Here's the implementation using two functions − one for selection and another for partitioning ? def select_smallest(numbers, beg, end, i): if end - beg k: ...

Read More

Python Program to Check String is Palindrome using Stack

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 997 Views

When it is required to check if a string is a palindrome using stack data structure, a stack class is created, and push and pop methods are defined to add and delete values from stack. Another method checks to see if the stack is empty or not. A palindrome is a word that reads the same forwards and backwards, like "madam" or "racecar". Using a stack's LIFO (Last In, First Out) property, we can reverse a string and compare it with the original. Stack Implementation First, let's create a stack class with basic operations ? ...

Read More

Python Program to Reverse only First N Elements of a Linked List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 293 Views

When it is required to reverse a specific set of elements in a linked list, a method named 'reverse_list' is defined. This iterates through the list and reverses the first N elements while keeping the remaining elements unchanged. Below is a demonstration for the same − Example class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: def __init__(self): self.head ...

Read More

Python Program to Find Number of Occurrences of All Elements in a Linked List

AmitDiwan
AmitDiwan
Updated on 25-Mar-2026 247 Views

When working with linked lists, it's often useful to find the number of occurrences of elements. This implementation demonstrates how to create a linked list and count occurrences of specific elements using a custom count_elem method. Below is a demonstration for the same − Linked List Implementation First, we create the basic structure with Node and LinkedList classes: class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: ...

Read More
Showing 1301–1310 of 8,392 articles
« Prev 1 129 130 131 132 133 840 Next »
Advertisements