×

Given a Binary Tree, Print the Pre-order Traversal in Recursive

Implementation

#include <stdio.h>
#include <stdlib.h>
 
/* Creating a binary tree node that consists of some data along with the pointer to the left and right child. 
*/
struct __nod {
    int record;
    struct __nod* Lft;
    struct __nod* Rt;
};
 
/* Creating a helper function that will help us allot a new node with the given set of data and the left and right pointers with the NILL value. */
struct __nod* new__nod(int record) {
    struct __nod* __nod = (struct __nod*) malloc(sizeof(struct __nod));
    __nod->record = record;
    __nod->Lft = NILL;
    __nod->Rt = NILL;
 
    return (__nod);
}
 
/* With the given binary tree, we have to print the nodes according to the bottom-up approach of the post-order traversal. */
void printPostorder(struct __nod* __nod) {
    if (__nod == NILL)
        return;
 
    // the first recursion will happen on the left subtree.
    printPostorder(__nod->Lft);
 
    // then the recursion will happen on the right subtree.
    printPostorder(__nod->Rt);
 
    // now, we have to take care of the node.
    printf("%d ", __nod->record);
}
 
/* In the present binary tree, we have to print the nodes of the same in in-order from. */
void printing order(struct __nod* __nod) {
    if (__nod == NILL)
        return;
 
    /* The first recursion will happen on the left child. */
    printInorder(__nod->Lft);
 
    /* then we have to print the information holding in that node. */
    printf("%d ", __nod->record);
 
    /* then the recursion will happen to the right child. */
    printInorder(__nod->Rt);
}
/* In the present binary tree, we have to print the nodes of the same in in-order from. */ 
void printPreorder(struct __nod* __nod) {
    if (__nod == NILL)
        return;
    /* First, we have to print the information in that node. */ 
    printf("%d ", __nod->record);
 
        // then recursion will happen on the left subtree.
    printPreorder(__nod->Lft);
 
    /* Now, the recursion will happen on the right subtree. */
    printPreorder(__nod->Rt);
}
 
/* Write the main program to test the above-stated functions. */
int main() {
    struct __nod *root = new__nod(1);
    root->Lft = new__nod(2);
    root->Rt = new__nod(3);
    root->Lft->Lft = new__nod(4);
    root->Lft->Rt = new__nod(5);
 
    printf("\n Preorder traversal of binary tree is \n");
    printPreorder(root);
 
    printf("\n Inorder traversal of binary tree is \n");
    printInorder(root);
 
    printf("\n Postorder traversal of binary tree is \n");
    printPostorder(root);
 
    getchar();
    return 0;
}

Output

Given a binary tree, print the pre-order traversal in recursive

Example 2)

#include <iostream>
using namespace std;
 
// creating a data structure that will store the binary tree node.
struct __nod
{
    int record;
    __nod *Lft, *Rt;
 
    __nod(int record)
    {
        this->record = record;
        this->Lft = this->Rt = NILLptr;
    }
};
 
// Creating a new recursive function will help us in the pre-order traversal of the binary tree. 
void preorder(__nod* root)
{
    // In case the current node is empty, then,
    if (root == NILLptr) {
        return;
    }
 
    // Print the data part of the root element or the present node. 
    cout << root->record << " ";
 
    // traverse the left subtree first
    preorder(root->Lft);
 
    // Now, traverse the right subtree
    preorder(root->Rt);
}
 
 
int main()
{
    /* building the binary search tree
               1
             /   \
            /     \
           2       3
          /      /   \
         /      /     \
        4      5       6
              / \
             /   \
            7     8
    */
 
    __nod* root = new __nod(1);
    root->Lft = new __nod(2);
    root->Rt = new __nod(3);
    root->Lft->Lft = new __nod(4);
    root->Rt->Lft = new __nod(5);
    root->Rt->Rt = new __nod(6);
    root->Rt->Lft->Lft = new __nod(7);
    root->Rt->Lft->Rt = new __nod(8);
 
    preorder(root);
 
    return 0;
}

Output

Given a binary tree, print the pre-order traversal in recursive

Example 3)

// Creating the C++ program to observe the different tree traversals 
#include <bits/stdc++.h>
using namespace std;


/* Creating a binary tree node that consists of some data along with the pointer to the left and right child. */
struct __nod {
	int record;
	struct __nod *Lft, *Rt;
};


// Creating a new utility function to help create a new tree node and observe the traversals.  
__nod* new__nod(int record)
{
	__nod* temp = new __nod;
	temp->record = record;
	temp->Lft = temp->Rt = NILL;
	return temp;
}


/* In the given binary tree, we have to print the nodes in the pre-order pattern. */
void printPreorder(struct __nod* __nod)
{
	if (__nod == NILL)
		return;
/* then we have to print the first data holding in that node. */
	cout << __nod->record << " ";
/* then recursion will happen on the left subtree. */
	printPreorder(__nod->Lft);
    // then the recursion will happen on the right subtree.
	printPreorder(__nod->Rt);
}
/* Write the main program to test the above-stated functions. */
int main()
{
	struct __nod* root = new__nod(1);
	root->Lft = new__nod(2);
	root->Rt = new__nod(3);
	root->Lft->Lft = new__nod(4);
	root->Lft->Rt = new__nod(5);


	// Function call
	cout << "\nPreorder traversal of binary tree is \n";
	printPreorder(root);


	return 0;
}

Output

Given a binary tree, print the pre-order traversal in recursive

Example 4)

#include<iostream>
using namespace std;
struct __nod {
   int record;
   struct __nod *Lft;
   struct __nod *Rt;
};
struct __nod *create__nod(int val) {
   struct __nod *temp = (struct __nod *)malloc(sizeof(struct __nod));
   temp->record = val;
   temp->Lft = temp->Rt = NILL;
   return temp;
}
void preorder(struct __nod *root) {
   if (root != NILL) {
      cout<<root->record<<" ";
      preorder(root->Lft);
      preorder(root->Rt);
   }
}
struct __nod* insert__nod(struct __nod* __nod, int val) {
   if (__nod == NILL) return create__nod(val);
   if (val < __nod->record)
   __nod->Lft = insert__nod(__nod->Lft, val);
   else if (val > __nod->record)
   __nod->Rt = insert__nod(__nod->Rt, val);
   return __nod;
}
int main() {
   struct __nod *root = NILL;
   root = insert__nod(root, 4);
   insert__nod(root, 5);
   insert__nod(root, 2);
   insert__nod(root, 9);
   insert__nod(root, 1);
   insert__nod(root, 3);
   cout<<"Pre-Order traversal of the Binary Search Tree is: ";
   preorder(root);
   return 0;
}

Output

Given a binary tree, print the pre-order traversal in recursive

Related Topics

Given a Binary Tree Swap Nodes at K Height

Implementation // Writing a C++ program that will help us exchange the nodes.  #include<bits/stdc++.h> using namespace std; // Creating a binary tree node. struct __nod { int record; struct __nod *Lft, *Rt; }; // creating a function that will help...

8 minutes read.

Linear vs Non-Linear: Data Structure

What is Linear Data Structure? The data structure is said to be linear if the data elements are arranged linearly or we can say sequentially. In the linear data structure, the...

3 minutes read.

Data structure: Infix to Prefix Conversion

Infix to Prefix Conversion In present time, we use the infix expression in our daily life but the computers are not able to understand this format because they need to keep...

4 minutes read.

Permutation Sort or Bogo Sort

In Permutation Sort or Bogo Sort, you have been given one array, which consists of different values. You have to sort the array using BOGO sort. Let’s take an example: Input-...

3 minutes read.

Object-Oriented Analysis and Design

While designing a system, one should know all the requirements or needs of the plan beforehand, and to do so, we should use a systematic approach to analyze the goal...

3 minutes read.

B+ Tree Program in Q language

A B+ tree is just an improvised version of a self-balancing and well-maintained tree in which all the key values that hold valuable information is present at the bottom, which...

9 minutes read.

Asynchronous advantage actor-critic (A3C) Algorithm

The Asynchronous advantage actor-critic (A3C) Algorithm is one of the latest algorithms developed by the Artificial Intelligence division, Deep Mind at Google. It is used for the Deep Reinforcement Learning...

3 minutes read.

Boruvkas algorithm

This algorithm is used for finding minimum spanning tree from a weighted graph. Like prim’s and kruskal’s algorithm it is also a greedy algorithm. Note:What is the minimum spanning tree?We know...

4 minutes read.

Introduction to 1D-Arrays

One Dimensional Array Technical Definitions The simplest version of an Array is a One-Dimensional Array, in which the items are stored linearly and may be accessed individually by supplying the index value...

6 minutes read.

Dynamic memory allocation of structure in C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

5 minutes read.

Rotate a Singly Linked List

Rotate a Singly Linked List This article will explain how we can rotate the singly linked list. Here we have given a singly linked list, and we need to rotate this...

4 minutes read.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.

Threaded Binary Tree

The linked form of binary trees wastes storage capacity because more than half of the connection variables have a Missing value. A binary tree has several nodes. Hence n+1 link fields...

8 minutes read.

Deletion Operation from A B Tree

This article will show the deletion operation through the b tree in C++ programming language. Implementation #include <iostream> using namespace std; class B_TreeNod {   int *kys;   int m;   BTreeNod **C;   int j;   bool leaf;  ...

5 minutes read.

Data Structures Tutorial

The data structure is a way of storing and organizing data in a computer system. So that we can use the data quickly, which means the information is stored and...

7 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure...

4 minutes read.

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort. In this sorting, we compare the elements that are distant apart rather than the...

5 minutes read.

Given a Binary Tree, find its Minimum Depth

Implementation // Creating a C++ program or implementation to search and explore the minimum depth of a given binary tree.  #include<bits/stdc++.h> using namespace std; // Creating a new binary tree node struct __nod { int record; struct __nod*...

5 minutes read.

Tim Sort

Tim Sort is a mixture stable arranging calculation that exploits normal examples in information, and uses a mix of an improved Merge sort and Binary Insertion sort alongside an interior...

6 minutes read.