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";
}
@neo9830
Copy link

neo9830 commented Jul 23, 2016

Is this code correct because both C and C++ methods are used!

@norielsantiago1990
Copy link

it doesnt work

@norielsantiago1990
Copy link

its not working

@ViRu-ThE-ViRuS
Copy link

    root = Insert(root,'M'); root = Insert(root,'B');
    root = Insert(root,'Q'); root = Insert(root,'Z'); 
    root = Insert(root,'A'); root = Insert(root,'C');

How is 'A' inserted as a child of 'B' and not 'Z', as the 'root' passed in root = Insert(root,'A'); is 'Z' and not 'B' due to
root = Insert(root,'Z'); being called before it.

@burakisk
Copy link

you must add "return root" in 45. line

@SajjidAtta
Copy link

Cooool!!!!!!!!

@JasmineNicole
Copy link

Thank you.

@sakthivel-priya
Copy link

IN ORDER AND PREORDER TRAVERSAL OF A BINARY TREE:
In order :D,B,H,E,A,I,F,J,C,G
pre-order:A,B,D,E,H,C,F,I,J,G
Answer:

@Pravinron
Copy link

why in line 47 the code is else if(data <= root->data) this not as like thiselse if(data == root->data). Explanation needed

@SameekshaS
Copy link

C code:
#include<stdio.h>
#include<stdlib.h>

struct Node
{
char data;
struct Node *right;
struct Node *left;
};
void Preorder(struct Node *root)
{
if (root==NULL)
{
return;
}
printf("%c",root->data);
Preorder(root->left);
Preorder(root->right);
}
void Inorder(struct Node *root)
{
if (root == NULL)
{
return;
}
Inorder(root->left);
printf("%c",root->data);
Inorder(root->right);
}
void Postorder(struct Node root)
{
if (root==NULL)
{
return;
}
Postorder(root->left);
Postorder(root->right);
printf("%c",root->data);
}
Node
Insert(struct Node root,char data)
{
if(root==NULL)
{
struct Node root=(struct Node)malloc(sizeof(struct Node));
root->data=data;
root->left=root->right=NULL;
return root;
}
else if (data<=root->data)
{
root->left=Insert(root->left,data);
}
else
{
root->right=Insert(root->right,data);
}
return root;
}
int main()
{
Node
root=NULL;
root = Insert(root,'M');
root = Insert(root,'B');
root = Insert(root,'Q');
root = Insert(root,'A');
root = Insert(root,'C');
root = Insert(root,'Z');
printf("Preorder:");
Preorder(root);
printf("\n");
printf("Inorder:");
Inorder(root);
printf("\n");
printf("Postorder:");
Postorder(root);
printf("\n");

}
All thanks to mycodeschool!

@Beerappa565
Copy link

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
int data;
struct node *left;
struct node *right;
};

struct node *
createNode (int value)
{
struct node *newNode = malloc (sizeof (struct node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}
struct node *
insert (struct node *root, int data)
{
if (root == NULL)
return createNode (data);

if (data < root->data)
root->left = insert (root->left, data);
else if (data > root->data)
root->right = insert (root->right, data);

return root;
}
void inorder (struct node *root)
{

if (root == NULL)
return;

inorder (root->left);
printf (":%d -> ", root->data);
inorder(root->right);
}
void preorder (struct node *root)
{

if (root == NULL)
return;
printf (" :%d ->", root->data);
preorder(root->left);
preorder(root->right);

}
void postorder (struct node *root)
{

if (root == NULL)
return;

postorder(root->left);
postorder(root->right);
printf (":%d ->", root->data);
}
int findmin(struct node* root)
{
if(root==NULL)
{

    return -1;
  
}
else if(root->left==NULL)
{
    return root->data;
}

   return findmin(root->left);

}
int findmax(struct node* root)
{
if(root==NULL)
{

    return -1;
  
}
else if(root->right==NULL)
{
    return root->data;
}

   return findmax(root->right);

}
int findheight(struct node* root)
{
int x,y;
if(root==NULL)
{
return-1;
}
x=findheight(root->left);
y=findheight(root->right);
if(x>y)
return x+1;
else
return y+1;
}

int
main ()
{
struct node *root = NULL;
root = insert (root, 8);
root=insert(root,7);
root=insert(root,6);
root = insert (root, 3);
root = insert (root, 1);
root = insert (root, 6);

root = insert (root, 10);
root = insert (root, 14);
root = insert (root, 4);
root = insert (root, 56);
root = insert (root, 334);
root = insert (root, 48);
printf("inorder traversal");
inorder(root);
printf("\npreorder traversal");
preorder(root);
printf("\npostorder traversal");
postorder(root);
printf("\nminelement:%d",findmin(root));
printf("\nmaxelement:%d",findmax(root));
printf("\nheight:%d",findheight(root));

}

@was33m443
Copy link

thanks, dear it's very informative
best coding

@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