Python has a built-in method called values() that returns a view object. The dictionary values are listed in the view object. We can use the values() method in Python to retrieve all values from a dictionary. Python's built-in values() method returns a view object that represents a list of dictionaries containing every value. In this article, we are going to see about the various ways to print all the values of the given dictionary in Python. Using dict.values() Method Python's dict.values() method can be used to retrieve the dictionary values which can be printed using the print() function. Example Following ... Read More
A binary tree is a non-linear data structure where each node can have at most two children i.e. left children and right children. In this article, we are going to discuss how to count nodes in a complete binary tree using JavaScript. What is a Complete Binary Tree? A complete binary tree is a binary tree in which all the nodes are completely filled except possibly the last level of the tree, and the last node has all its nodes on the left side as possible. Below are examples to demonstrate the above problem, clearly: Example 1 Input: ... Read More
In Python, the try, except, and else statements are used to handle exceptions and define specific blocks of code that should execute based on whether an error occurs or not. This helps you manage errors and separate successful code from error-handling logic. Using "try" and "except" The try block contains code that might raise an exception. If an exception occurs, the control jumps to the except block, which contains code to handle that exception. Example: Handling division by zero In the following example, we are dividing a number by zero, which raises an exception that is handled by the except ... Read More
A narcissistic number (also known as an Armstrong number) is a number that equals the sum of its digits, each raised to the power of the number of digits. For example, 370 - 33+73+03 = 370. The algorithm to check for an Armstrong number is as follows - Determine the number of digits for the mentioned number. Extract each digit and calculate the power of that digit with the exponent equal to the number of digits. Calculate the sum of the power. Compare ... Read More
In Java, a List is an interface that extends the Collection interface and represents a sequence of elements. Since the List is an interface. To create a List object, we need to instantiate a class that implements the List interface, such as ArrayList. The List provides various methods that help to check or find the element in it. We will discuss those methods in the coming section with suitable examples. Below is a list of various ways to find an element in the Java List: Using the get() Method Using ... Read More
What is Sparse Matrix?A sparse matrix is a matrix in which the majority of the elements are zero. In other words, if more than half of the elements in a matrix are 0, it is called a sparse matrix. In this article, we will show you how to write a C++ program to check whether a given matrix is sparse or not. Let's understand this with an example. The matrix shown below contains 5 zeros. Since the number of zeros is more than half of the total elements (9), it is a sparse matrix: 1 0 2 5 0 ... Read More
A structure is a collection of items of different data types. It is very useful in creating complex data structures with different data type records. A structure is defined with the struct keyword. In this article, we will store and display information of an employee using a structure. An example of a structure is as follows: struct employee { int empID; char name[50]; int salary; char department[50]; }; Store Information in Structure and Display It In this program, we store and display employee information using a ... Read More
In this article, we'll show you how to write a C++ program to perform a preorder recursive traversal of a binary tree. A binary tree is a tree where each node has zero, one, or two children. Even an empty tree is called a valid binary tree. Traversal means visiting all the nodes in a tree exactly once. In preorder traversal, we visit the root first, then the left child, and finally the right child. Let's take a small binary tree and see an example: Here, we start at the root(3), then go to the left child (6), ... Read More
A multi-dimensional array is nothing but an array of arrays, and yes, it is supported by the Java programming language. It is used to store data within a table, grid, or matrix having rows and columns. In Java, these arrays are also called ragged arrays or jagged arrays. Let's understand why Java supports multi-dimensional arrays and their types. Uses of Multidimensional Arrays Multidimensional arrays in Java are used for various purposes, including: Storing data in a tabular format, such as matrices, chessboards, or spreadsheets. Representing complex relationships in data like 3D models. Managing multiple sets of data, like scores ... Read More
Tree traversal is a form of graph traversal. It involves checking or printing each node in the tree exactly once. The inorder traversal of a binary tree involves visiting each of the nodes in the tree in the order (Left, Root, Right). An example of Inorder traversal of a binary tree is as follows. Here, we start at the leftmost node(5), move up to 6, then to 2, then to root 3, and continue to the right side with 9 then 4 and 8. Using Recursion for Inorder Traversal Recursion is a technique where a function ... Read More