
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 33676 Articles for Programming

445 Views
When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Demofrom collections import deque def add_edge(adj: list, u, v): adj[u].append(v) adj[v].append(u) def detect_cycle(adj: list, s, V, visited: list): parent = [-1] * ... Read More

723 Views
When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Democlass Graph_structure: def __init__(self, V): self.V = V self.adj = [[] for i in range(V)] def DFS_Utility(self, temp, ... Read More

787 Views
When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Demofrom collections import deque def add_edge(v, w): global visited_node, adj adj[v].append(w) adj[w].append(v) def BFS_operation(component_num, src): global visited_node, adj queue ... Read More

243 Views
When it is required to find the sum of all the nodes of a tree, a class is created, and it contains methods to set the root node, add elements to the tree, search for a specific element, and add elements of the tree to find the sum and so on. An instance of the class can be created to access and use these methods.Below is a demonstration of the same −Example Live Democlass Tree_struct: def __init__(self, data=None): self.key = data self.children = [] def set_root(self, data): self.key = ... Read More

900 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 −Example Live Democlass Tree_struct: def __init__(self, data=None): self.key = data self.children = [] def set_root(self, data): self.key = data ... Read More

338 Views
When it is required to print all the permutations of a string in lexicographic order using recursion, a method is defined, that uses the ‘for’ loop to iterate over the sequence of elements, and use the ‘join’ method to join the elements.Below is the demonstration of the same −Example Live Demofrom math import factorial def lexicographic_permutation_order(s): my_sequence = list(s) for _ in range(factorial(len(my_sequence))): print(''.join(my_sequence)) next = next_in_permutation(my_sequence) if next is None: my_sequence.reverse() else: my_sequence = next def ... Read More

277 Views
When it is required to print the nodes in the left subtree, a class can be created that consists of methods can be defined to set the root node, perform in order traversal, insert elements to the right of the root node, to the left of the root node, and so on. An instance of the class is created, and the methods can be used to perform the required operations.Below is a demonstration of the same −Example Live Democlass BinaryTree_struct: def __init__(self, data=None): self.key = data self.left = None self.right = ... Read More

459 Views
When it is required to print all the permutations of a string in the lexicographic order without using recursion, a method is defined, that takes the string as the parameter. It uses a simple ‘for’ loop to iterate over the string elements and uses ‘while’ condition to check for certain constraints.Below is the demonstration of the same −Example Live Demofrom math import factorial def lex_permutation(my_string): for i in range(factorial(len(my_string))): print(''.join(my_string)) i = len(my_string) - 1 while i > 0 and my_string[i-1] > my_string[i]: i -= 1 my_string[i:] = reversed(my_string[i:]) if i > 0: ... Read More

722 Views
When it is required to form a new string that is made from the first two and last two characters of a given string, a counter can be defined, and indexing can be used to access specific range of elements.Below is the demonstration of the same −Example Live Demomy_string = "Hi there how are you" my_counter = 0 for i in my_string: my_counter = my_counter + 1 new_string = my_string[0:2] + my_string [my_counter - 2: my_counter ] print("The string is ") print(my_string) print("The new string is ") print(new_string)OutputThe string is Hi there how are you The new ... Read More

665 Views
When it is required to create a mirror copy of a tree, and display it using breadth first search, a binary tree class is created with methods set the root element, insert element to left, insert element to right, search for a specific element, and perform post order traversal and so on. An instance of the class is created, and it can be used to access the methods.Below is the demonstration of the same −Example Live Democlass BinaryTree_struct: def __init__(self, key=None): self.key = key self.left = None self.right = None ... Read More