InOrder Traversal

Well, here you go for InOrder Traversal…

#include<iostream>
using namespace std;

class node{
public:
int data;
node* left;
node* right;

node(int d){
this -> data = d;
this -> left = NU…


This content originally appeared on DEV Community and was authored by Ankit Rattan

Well, here you go for InOrder Traversal...

#include<iostream>
using namespace std;

class node{
    public:
        int data;
        node* left;
        node* right;

    node(int d){
        this -> data = d;
        this -> left = NULL;
        this -> right = NULL; 
    }    
};

node* buildTree(node* root){
    cout<<"Enter the data: "<<endl;
    int data;
    cin>>data;

    root = new node(data);

    if(data == -1) return NULL;

    cout<<"Enter the data for the left of "<<data<<endl;
    root -> left = buildTree(root -> left);
    cout<<"Enter the data for the right of "<<data<<endl;
    root -> right = buildTree(root -> right);
    return root;
}

void inorder(node* root){
    if(root == NULL) return;

    inorder(root -> left);     // -> Left (L)
    cout<< root -> data << " ";
    inorder(root -> right);  // -> Right (R)

}

int main()
{
    node* root = NULL;
    root = buildTree(root);

    inorder(root);


    return 0;
}


This content originally appeared on DEV Community and was authored by Ankit Rattan


Print Share Comment Cite Upload Translate Updates
APA

Ankit Rattan | Sciencx (2024-08-30T12:24:28+00:00) InOrder Traversal. Retrieved from https://www.scien.cx/2024/08/30/inorder-traversal/

MLA
" » InOrder Traversal." Ankit Rattan | Sciencx - Friday August 30, 2024, https://www.scien.cx/2024/08/30/inorder-traversal/
HARVARD
Ankit Rattan | Sciencx Friday August 30, 2024 » InOrder Traversal., viewed ,<https://www.scien.cx/2024/08/30/inorder-traversal/>
VANCOUVER
Ankit Rattan | Sciencx - » InOrder Traversal. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/30/inorder-traversal/
CHICAGO
" » InOrder Traversal." Ankit Rattan | Sciencx - Accessed . https://www.scien.cx/2024/08/30/inorder-traversal/
IEEE
" » InOrder Traversal." Ankit Rattan | Sciencx [Online]. Available: https://www.scien.cx/2024/08/30/inorder-traversal/. [Accessed: ]
rf:citation
» InOrder Traversal | Ankit Rattan | Sciencx | https://www.scien.cx/2024/08/30/inorder-traversal/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.