Quick actions

cmd+k|ctrl+k

Navigation

Languages

BST operatios

Snippet info

Language

Cpp

Visibility

public

Author

jameeulla3

Created

2024-08-07T01:25:20.451959Z

Updated

2024-08-07T16:41:41.071862Z

#include <iostream>
using namespace std;
struct node 
{
    int data;
    struct node *left; 
    struct node *right;
};
class BinarySearchTree
{
    
    public:
    struct node *root;
    BinarySearchTree()
    {
        root = NULL;
    }
    void insert(struct node *root,int value)
    {
        if(root == NULL)
        {
            root = new struct node; 
            root->data = value; 
            root->left = NULL;
            root->right = NULL;
        }
        else if(root->data>value)
        {
            insert(root->left,value);
        }
        else
        {
            insert(root->right,value);
        }
    }
    void display(struct node *root)
    {
        if(root == NULL) return;
        
            display(root->left);
            cout<<root->data<<"\t";
            display(root->right);
        
    }
};
int main() {
    BinarySearchTree BST; 
    BST.insert(BST.root,5);
    BST.insert(BST.root,6);
    BST.display(BST.root);
    return 0;
}
INFO