BST

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
953 B
0
Indexable
Never
#include <iostream>
using namespace std;

int bst[1024]; //= {0,38,14,56,8,23,45,82};
bool isFound = false;
int pos = -1;

void find(int loc, int n){
    if(bst[loc]==-1){
        pos = loc;
        return;
    }
    else if(bst[loc]==n){
        isFound = true;
        pos = loc;
        return;
    } 
    else if(bst[loc] > n){
        find(2*loc, n);
    }
    else{
        find(2*loc+1, n);
    }
}

void insert_in_bst(int n){
    isFound = false;
    pos = -1;
    find(1,n);

    if(!isFound){
        bst[pos] = n;
    }
    else {
        cout<<"Found: "<< pos<<endl;
    }
}

void print_Array(){
    for( int i=1;i<=20;i++){
        cout<<i<< " "<< bst[i]<<endl;
    }
}

int main(){

    for(int i = 0; i<1024; i++){
        bst[i] = -1;
    }

    int x;

    while (1)
    {
        cin>> x;
        if(x==-1) break;

        insert_in_bst(x);
    }

    print_Array();
    
}