Skip to content

Instantly share code, notes, and snippets.

@mycodeschool
Last active October 29, 2024 05:29
Show Gist options
  • Save mycodeschool/10016271 to your computer and use it in GitHub Desktop.
Save mycodeschool/10016271 to your computer and use it in GitHub Desktop.
Binary tree traversal: Preorder, Inorder, Postorder
/* Binary Tree Traversal - Preorder, Inorder, Postorder */
#include<iostream>
using namespace std;
struct Node {
char data;
struct Node *left;
struct Node *right;
};
//Function to visit nodes in Preorder
void Preorder(struct Node *root) {
// base condition for recursion
// if tree/sub-tree is empty, return and exit
if(root == NULL) return;
printf("%c ",root->data); // Print data
Preorder(root->left); // Visit left subtree
Preorder(root->right); // Visit right subtree
}
//Function to visit nodes in Inorder
void Inorder(Node *root) {
if(root == NULL) return;
Inorder(root->left); //Visit left subtree
printf("%c ",root->data); //Print data
Inorder(root->right); // Visit right subtree
}
//Function to visit nodes in Postorder
void Postorder(Node *root) {
if(root == NULL) return;
Postorder(root->left); // Visit left subtree
Postorder(root->right); // Visit right subtree
printf("%c ",root->data); // Print data
}
// Function to Insert Node in a Binary Search Tree
Node* Insert(Node *root,char data) {
if(root == NULL) {
root = new Node();
root->data = data;
root->left = root->right = NULL;
}
else if(data <= root->data)
root->left = Insert(root->left,data);
else
root->right = Insert(root->right,data);
return root;
}
int main() {
/*Code To Test the logic
Creating an example tree
M
/ \
B Q
/ \ \
A C Z
*/
Node* root = NULL;
root = Insert(root,'M'); root = Insert(root,'B');
root = Insert(root,'Q'); root = Insert(root,'Z');
root = Insert(root,'A'); root = Insert(root,'C');
//Print Nodes in Preorder.
cout<<"Preorder: ";
Preorder(root);
cout<<"\n";
//Print Nodes in Inorder
cout<<"Inorder: ";
Inorder(root);
cout<<"\n";
//Print Nodes in Postorder
cout<<"Postorder: ";
Postorder(root);
cout<<"\n";
}
@farzana-orin
Copy link

can anyone solve this code for me??

  1. Create a Bitree by AB#D##CE##F##
  2. Traverse this tree by preorder, inorder and postorder.

@00nicho00
Copy link

For me, i added the curly braces between the curly braces and it works fine for me.

I tried to put curly braces in between the if else statements and it works perfectly fine for me. The bottom part is the code.

`/* Binary Tree Traversal - Preorder, Inorder, Postorder */
#include
#include<bits/stdc++.h>
using namespace std;

struct Node {
char data;
struct Node *left;
struct Node *right;
};

//Function to visit nodes in Preorder
void Preorder(struct Node *root) {
// base condition for recursion
// if tree/sub-tree is empty, return and exit
if(root == NULL) {
return;
}
printf("%c ",root->data); // Print data
Preorder(root->left); // Visit left subtree
Preorder(root->right); // Visit right subtree
}

//Function to visit nodes in Inorder
void Inorder(Node *root) {
if(root == NULL) {
return;
}
Inorder(root->left); //Visit left subtree
cout << " " << root->data; //Print data
Inorder(root->right); // Visit right subtree
}

//Function to visit nodes in Postorder
void Postorder(Node *root) {
if(root == NULL) {
return;
}
Postorder(root->left); // Visit left subtree
Postorder(root->right); // Visit right subtree
cout << " " << root->data; // Print data
}

// Function to Insert Node in a Binary Search Tree
Node* Insert(Node *root,char data) {
if(root == NULL) {
root = new Node();
root->data = data;
root->left = root->right = NULL;
}
else if(data <= root->data){
root->left = Insert(root->left,data);
}
else {
root->right = Insert(root->right,data);
}

return root;

}

int main() {
/*Code To Test the logic
Creating an example tree
M
/
B Q
/ \
A C Z
/
Node
root = NULL;
root = Insert(root,'M'); root = Insert(root,'B');
root = Insert(root,'Q'); root = Insert(root,'Z');
root = Insert(root,'A'); root = Insert(root,'C');
//Print Nodes in Preorder.
cout<<"Preorder: ";
Preorder(root);
cout<<"\n";
//Print Nodes in Inorder
cout<<"Inorder: ";
Inorder(root);
cout<<"\n";
//Print Nodes in Postorder
cout<<"Postorder: ";
Postorder(root);
cout<<"\n";
}`

@0xabdulkhaliq
Copy link

$$\large\textcolor{yellow}{\textsf{JavaScript} \space \color{white}\textsf{Version is here..!}}$$

class Node {
  constructor(data) {
    this.data = data;
    this.left = null;
    this.right = null;
  }

  // Method to insert a node into the Binary Search Tree rooted at this node
  insert(data) {
    if (data <= this.data) {
      this.left === null ? this.left = new Node(data) : this.left.insert(data);
    } else {
      this.right === null ? this.right = new Node(data) : this.right.insert(data);
    }
  }
}


// Creating functions for 3 types of Traversals.

function preOrder(root, nodelist = []) {  // D L R
    if (root) {
        nodelist.push(root.data);         // Push Data
        preOrder(root.left, nodelist);    // Visit Left Subtree
        preOrder(root.right, nodelist);   // Visit Right Subtree
    }

    return nodelist;
}

function inOrder(root, nodelist = []) {  // L D R
    if (root) {
        inOrder(root.left, nodelist);    // Visit Left Subtree
        nodelist.push(root.data);        // Push Data
        inOrder(root.right, nodelist);   // Visit Right Subtree
    }

    return nodelist;
}

function postOrder(root, nodelist = []) {  // L R D
    if (root) {
        postOrder(root.left, nodelist);    // Visit Left Subtree
        postOrder(root.right, nodelist);   // Visit Right Subtree
        nodelist.push(root.data);          // Push Data
    }

    return nodelist;
}


/* Creating the root node 
   with example tree for testing

	    M
           / \
	  B   Q
         / \   \
	A   C   Z
*/

const root = new Node('M');

// Insert nodes into the BST using the insert method
root.insert('B');
root.insert('Q');
root.insert('Z');
root.insert('A');
root.insert('C');

preOrder(root)    // [ 'M', 'B', 'A', 'C', 'Q', 'Z' ]
inOrder(root)     // [ 'A', 'B', 'C', 'M', 'Q', 'Z' ]
postOrder(root)   // [ 'A', 'C', 'B', 'Z', 'Q', 'M' ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment