Binary Search Tree(Unorder input)

#include<bits/stdc++.h>
using namespace std;

struct node{
    int value;
    node *left;
    node *right;
};

node* newnode(int value)
{
    node *temp = (node*) malloc(sizeof(node));
    temp->value = value;
    temp->left = temp->right = NULL;

    return temp;
}

node* putin(node* vertex, int value)
{
    if(vertex == NULL)
        return newnode(value);

    if(value < vertex->value)
        vertex->left = putin(vertex->left, value);

    else if(value > vertex->value)
        vertex->right = putin(vertex->right, value);

    return vertex;
}

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

    inorder(vertex->left);
    cout << vertex->value << " ";
    inorder(vertex->right);
}

void preorder(node* vertex)
{
    if(vertex == NULL)
        return;

    cout << vertex->value << " ";
    preorder(vertex->left);
    preorder(vertex->right);
}

void postorder(node* vertex)
{
    if(vertex == NULL)
        return;

    postorder(vertex->left);
    postorder(vertex->right);
    cout << vertex->value << " ";
}

int main()
{
    node *root = NULL;
    root = putin(root, 50);

    putin(root, 30);
    putin(root, 20);
    putin(root, 40);
    putin(root, 70);
    putin(root, 60);
    putin(root, 80);

    cout << "In-order :: ";
    inorder(root);
    cout << "\n";

    cout << "Pre-order :: ";
    preorder(root);
    cout << "\n";

    cout << "Post-order :: ";
    postorder(root);
    cout << "\n";

    return 0;
}

Comments