Postorder Traversal of Binary Tree Without Recursion and Stack in C++

sudhir sharma
Updated on 17-Apr-2020 07:03:44

467 Views

In this problem, we are given a Binary tree. Our task is to print the postorder traversal of the binary tree without using recursion and without stack.Binary tree is a special type of tree in which each node can have at max 2 child nodes.Postorder Traversal is a tree traversal technique, in which the first left subtree is traversed than the right subtree and the root is traversed at the end.postorder traversal of the above tree − 8 4 2 7 9 6To traverse the tree without using recursion and stack. We will depth-first search based technique and data will ... Read More

POW Function for Complex Number in C++

sudhir sharma
Updated on 17-Apr-2020 06:58:09

723 Views

pow() or power function is a function used to calculate the power of a number. It is generally used in real numbers. Here, we will see its implementation of complex numbers.Complex numbers are those numbers that can be represented as A + iB, where A is the real part and B is the complex part of the number.The functions for the complex number in c++ are defined in the header file . Here, the pow() method for complex numbers is defined. It finds the complex power of a complex number to some power.The method is applied to complex numbers takes ... Read More

Power in Mathematics in C++

sudhir sharma
Updated on 17-Apr-2020 06:55:32

178 Views

The power of a number is the times a number is multiplied to itself. Also knows as exponent or indices.a to the power b is b times a is multiplied by itself b times. 7 to the power 2 is 72 also known as 7 square is valued 49.Some common power values are −A number to the power 0 gives 1.A number to the power 1 gives the same number, as stated some multiplied once is the same.A number to the negative power is n times division. Example, a -3 = 1/a3 or (1/a)*(1/a)*(1/a)Now, let’s do some programming based on ... Read More

Power of a Prime Number r in n in C++

sudhir sharma
Updated on 17-Apr-2020 06:49:39

230 Views

In this problem, we are given two integer n and r. Our task is to find the power of the given prime number r in the factorial of the number n.Let’s take an example to understand the problemInput − n = 6 r = 2Output − 4Explanation −Factorial n, 6! = 6*5*4*3*2*1 = 720 720 = 24 * 32 * 5, power of 2 is 4To solve this problem, a simple solution would be directly finding the factorial and then finding the power of the prime number. But this is not the best solution.Another efficient solution is using a formula, ... Read More

Power Set in Lexicographic Order in C++

sudhir sharma
Updated on 17-Apr-2020 06:46:51

775 Views

In this problem, we are given string str. Our task is to print the power set of this string’s elements in lexicographical order.Power Set − The power set of a set is the set of all subsets of the set. Denoted by P(S) where s is the set.Example −S = {1, 2, 3} ; p(S) = {{}, {1}, {1, 2}, {1, 3}, {2}, {2, 3}, {3}, {1, 2, 3}}In this problem, we will treat the string as a set. So, its characters will be the elements of the set.Let’s take an example to understand the problemInput − str = “xyz”Output ... Read More

Powers of 2 to Required Sum in C++

sudhir sharma
Updated on 17-Apr-2020 06:43:33

689 Views

In this problem, we are given an integer N. Our task is to print the number which when raised to the power of 2 gives the number.Let’s take an example to understand the problemInput − 17Output − 0, 4Explanation − 17 = 24 + 20 = 16 + 1To solve this problem, we will divide the number with 2 recursively. By this method, every number can be represented as a power of 2. This method is used to convert the number to its binary equivalent.ExampleThe program to show the implementation of our solution Live Demo#include using namespace std; void sumPower(long ... Read More

Powers of Two and Subsequences in C++

sudhir sharma
Updated on 17-Apr-2020 06:41:39

220 Views

In this problem, we are given an array of N integers. Our task is to find the count of subsequences that can be formed such that if their elements are multiplied, they result in a number which is a power of two.Let’s take an example to understand the problem, Input − arr = [2, 5, 4]Output − 3Explanation − subsequences [2], [4] and [2, 4] give the desired result.To solve this problem, we need to understand the logic of power.Only those numbers with are powers of 2 will multiply to give the desired result. So, we have to consider only ... Read More

Rules for External Declarations in JShell in Java 9

raja
Updated on 16-Apr-2020 19:10:32

123 Views

JShell is a command-line tool introduced in Java 9, and it is Java's first official REPL tool to create a simple programming environment that reads the user's inputs, evaluates it, and prints the result.The declarations outside a class or interface (and declarations of classes and interfaces by themselves) have been created under the following rules.Rules for External Declarations:1) Access modifiers like public, protected, and private can be ignored. All declaration snippets can be accessible to all other snippets.jshell> private int i = 10; i ==> 10 jshell> System.out.println(i); 102) The modifier final can be ignored. Changes and inheritance are allowed.jshell> final class A {void m() {} } ... Read More

Create an Instance of VarHandle in Java 9

raja
Updated on 16-Apr-2020 14:11:28

224 Views

In general, a Variable Handle is simply typed reference to a variable. It will be an array element, an instance or static field of the class. VarHandle class can provide write and read access to variables under specific conditions. These are immutable and have no visible condition. In addition, they can't be sub-classified, and each VarHandle has a generic type T, which is the type of each variable represented by this VarHandle. The objective of VarHandle is to define a standard for calling equivalents of java.util.concurrent.atomic and sun.misc.Unsafe operations on fields and array elements.In the below example, we can use ... Read More

Implement Flow Publisher Interface in Java 9

raja
Updated on 16-Apr-2020 10:36:29

823 Views

A Publisher interface is a provider of an unbounded number of sequenced elements, publishing them according to the demand received from its Subscriber(s). In response to call Publisher.subscribe(Subscriber), the possible invocation sequences for methods on the Subscriber. It means that the onSubscribe() method, followed by the unbounded number of onNext() methods (as requested by Subscriber) followed by an onError() method, if there is a failure or an onComplete() method when no more elements available as long as Subscription is not canceled.Syntaxpublic interface Publisher {    public void subscribe(Subscriber

Advertisements