Untitled

 avatar
unknown
plain_text
5 months ago
3.1 kB
3
Indexable
#include<bits/stdc++.h>

using namespace std;

struct BST_Node{
    int val;
    BST_Node *left, *right;
};

BST_Node* Insert(BST_Node *root, int x)
{
    BST_Node *newnode = new BST_Node();
    newnode -> val = x;
    newnode -> left = NULL;
    newnode -> right = NULL;

    BST_Node *currnode;
    currnode = root;

    if(root == NULL)
    {
        root = newnode;
        return root;
    }
    while(1)
    {
        if(x < currnode -> val)
        {
            if(currnode -> left != NULL)
            {
                currnode = currnode -> left;
            }
            else
            {
                currnode -> left = newnode;
                return root;
            }
        }
        else
        {
            if(currnode -> right != NULL)
            {
                currnode = currnode -> right;
            }
            else
            {
                currnode -> right = newnode;
                return root;
            }
        }
    }
}

void Preorder(BST_Node *parent)
{
    if(parent != NULL)
        cout<<parent -> val << " ";
    if(parent -> left != NULL)
        Preorder(parent -> left);
    if(parent -> right != NULL)
        Preorder(parent -> right);
}

void Inorder(BST_Node *parent)
{
    if(parent -> left != NULL)
        Inorder(parent -> left);
    if(parent != NULL)
        cout<<parent -> val << " ";
    if(parent -> right != NULL)
        Inorder(parent -> right);
}

void Postorder(BST_Node *parent)
{
    if(parent -> left != NULL)
        Postorder(parent -> left);
    if(parent -> right != NULL)
        Postorder(parent -> right);
    if(parent != NULL)
        cout<<parent -> val << " ";
}

int Search(BST_Node *root, int x)
{
    BST_Node *currnode;
    currnode = root;
    if(root == NULL)
    {
        return 0;
    }
    while(1)
    {
        if(x < currnode -> val)
        {
            if(currnode -> left != NULL)
            {
                currnode = currnode -> left;
            }
            else
            {
                return 0;
            }
        }
        else if(x > currnode -> val)
        {
            if(currnode -> right != NULL)
            {
                currnode = currnode -> right;
            }
            else
            {
                return 0;
            }
        }
        else
        {
            return 1;
        }
    }
}

//3 5 1 7 10 13 6 15 12 9 0 -1 2

int main()
{
    BST_Node *root = NULL;
    root = Insert(root,3);
    root = Insert(root,5);
    root = Insert(root,1);
    root = Insert(root,7);
    root = Insert(root,10);
    root = Insert(root,13);
    root = Insert(root,6);
    root = Insert(root,15);
    root = Insert(root,12);
    root = Insert(root,9);
    root = Insert(root,0);
    root = Insert(root,-1);
    root = Insert(root,2);
    Preorder(root);
    cout<<endl;
    Inorder(root);
    cout<<endl;
    Postorder(root);
    cout<<endl;
    cout<<Search(root,12)<<endl;
    cout<<Search(root,16)<<endl;
}
Editor is loading...
Leave a Comment