We learned about the different type of traversals in this post Binary Tree Traversals In pre-order traversal, each node is processed before (pre) either of its sub-trees. This is the simplest traversal to understand. However, even though each node is processed before the sub-trees, it still requires that some information must be maintained while moving down the tree. Let us consider an example In the example tree shown above, the node “1” is processed first, then the left sub-tree, followed by the right sub-tree. Therefore, processing must return to the …
-
-
We know that the ‘%’ (modulus) operator returns the remainder when a number a is divided by b. Example: 12 % 10 = 2 So, considering this in mind does the % operator work with floating point values as well? Can % be used with floating point numbers in C? The above program fails in compilation and compiler report the following error in line 5: What about Java? This behavior is different in Java. % operator can be used on floating point numbers in JAVA language. Consider following example of …
-
We have covered some basics about the tree in the previous posts- The TREE data structure Binary Trees Structure of Binary Trees Now let us try to see, how we can traverse a tree. In order to process trees, we need a mechanism for traversing them and that forms the subject of this post. The process of visiting all nodes of a tree is called tree traversal. Each of the nodes is processed only once but they maybe visited more than once. As we have already seen that in linear …
-
We learned about trees and binary trees in the following posts:- The tree data structure The basics of Binary Tree Now let us try to define the structure of the binary tree. For simplicity, assume that the data of the nodes are integers. One way to represent a node (which contains the data) is to have two links which points to the left and right children along with data fields as shown below: Please note that in trees, the default flow is from parent to children and showing directed branches …
-
A tree is called a binary tree if each node has zero child, one child or two children. Empty tree is also a valid binary tree. We can visualize a binary tree as consisting of a root and two disjoint binary trees, called the left and right sub-trees of the root. Generic Binary Tree Types of Binary Trees Strict Binary Tree: A tree is called a strict binary tree if each node has exactly two children or no children. Full Binary Tree: A binary tree is called a full binary …
-
A tree is a data structure similar to a linked list but instead of each node simply pointing to the next node in a linear fashion, each node points to a number of nodes. Tree is an example of a non-linear data structure. A tree structure is a way of representing the hierarchical nature of a structure in a graphical form. In trees ADT (Abstract Data Type), order of the elements is not important. If we need ordering information, linear data structures like linked lists, stacks, queues etc can be …
-
Question: Given an array arr[], that contains only 0’s and 1’s. Write a function that separate 0’s and 1’s in the array. Input: 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1 Output: 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1 The objective of the problem is to segregate the array in two parts, and it is more popularly known as a variation of Dutch National Flag Problem. Method 1: The most naive method is to count …
