//here is a simple program that will create a binary search tree

#include<iostream>
#include<conio.h>
using namespace std;

class bst
{
    struct tree
    {
        int data;
        struct tree *left;
        struct tree *right;
    };

    tree *root;

public:

    bst()
    {
        root=NULL;
    }
    bool IsEmpty()const
    {
        return root == NULL;
    }
    void insert(int num)
    {
        tree *temp,*parent;
        temp=new tree;
        temp->data=num;
        temp->left=NULL;
        temp->right=NULL;
        parent=NULL;
        if(IsEmpty())
        {
            root=temp;
            return;
        }
        else
        {
            tree *curr;
            curr=root;
            while(curr)
            {
                parent=curr;
                if(temp->data>curr->data)
                    curr=curr->right;
                else
                    curr=curr->left;
            }
            if(temp->data>parent->data)
                parent->right=temp;
            else
                parent->left=temp;
        }
        return;
    }

};


int main()
{
    bst b;
    b.insert(10);
    b.insert(15);
    b.insert(5);
    return 0;
}

No comments:

Post a Comment

Kruskal Algorithm

Minimum Cost Spanning Tree By Kruskal Algorithm using priority_queue in c++ #include<bits/stdc++.h> using namespace std; typedef p...