InOrder Traversal in a binary tree

    by nikoo28

    We discussed about the tree traversal techniques in this post-

    Please see pre-order traversal to understand the basics of visiting nodes. We have our same sample tree
    binary_tree_2
    Now let us try to understand the In-order traversal. In in-order traversal, the root is traversed between the sub trees. In-order traversal is defined as follows.

    • Traverse the left sub-tree
    • Visit the node
    • Traverse the right sub-tree

    So when we apply the in-order traversal on our example tree this will be done as:-
    in_order_traversal_image(1)in_order_traversal_2inorder taversal image 3
    We get the final in-order traversal as:- 4, 2, 5, 1, 6, 3, 7

    The recursive version of in-order traversal can be written as:-

    void inOrderRecursive(struct binaryTreeNode * root)
    {
        if(root)
        {
            // In-order traversal of left sub-tree (Step 1)
            inOrderRecursive(root -> left);
    
    		// Visit the root (Step 2)
            printf("%d ", root -> data);
    
            // In-order traversal of right sub-tree(Step 3) 
            inOrderRecursive(root -> right);
        }
    }
    

    Non-Recursive In-order Traversal

    Non-recursive version of In-order traversal us very much similar to Pre-order. The only change is instead of processing the node before going to left sub-tree, process it after popping (which indicates after completion of left sub-tree processing).

    void inOrderNonRecursive(struct binaryTreeNode * root)
    {
    	// Create a new empty stack
    	struct stackNode * S = NULL;
    	
    	// We need to loop until the stack is empty
    	while(1)
    	{
    		// If the root is not null
    		while(root)
    		{
    			// Push the first element onto the stack
    			S = push(S, root);
    			
    			// Go to left sub-tree and keep on adding it to the stack
    			root = root -> left; // (Step 1)
    		}
    		
    		// Check if the stack is empty
    		if(isStackEmpty(S))
    			break;
    		
    		// else we need to pop out the element
    		root = S -> data;
    		
    		// Remove the element from the stack
    		S = S -> next;
    		
    		// After popping process the current node
    		printf("%d ",root -> data); // Step 2
    		
    		// After printing, left sub-tree and root have been completed
    		// Go to the right sub-tree
    		root = root -> right; // Step 3
    	}
    	
    	// We need to delete the stack
    	free(S);
    }
    

    Time Complexity:- O(n)
    Space Complexity:- O(n)

    You can download the complete working code here.

    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • TheoryTrees

    PreOrder Traversal in a binary tree

    by nikoo28
    4 minutes read

    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 …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Can we use % operator on floating point numbers?

    by nikoo28
    1 minutes read

    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? #include <stdio.h> int main() { float f = 9.9f, m = 3.3f; float c = f % m; printf("%f",c); return 0; } The above program fails in compilation and compiler report the following error in line 5: Output: invalid operands …

    0 FacebookTwitterLinkedinWhatsappEmail
  • TheoryTrees

    Binary Tree Traversals

    by nikoo28
    3 minutes read

    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 …

    0 FacebookTwitterLinkedinWhatsappEmail
  • TheoryTrees

    Structure of Binary Trees

    by nikoo28
    2 minutes read

    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: // Defining the basic structure for a binary tree node struct binaryTreeNode { int data; struct binaryTreeNode …

    0 FacebookTwitterLinkedinWhatsappEmail
  • TheoryTrees

    The basics of Binary Trees

    by nikoo28
    2 minutes read

    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 …

    0 FacebookTwitterLinkedinWhatsappEmail
  • TheoryTrees

    The TREE Data Structure

    by nikoo28
    3 minutes read

    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 …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More