Ejemplo n.º 1
0
 bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
     BinarySearchTree btree;
     for (int i = 0; i < nums.size(); i++) {
         if (btree.search_or_add(Node(nums[i], t)))
             return true;
         //btree.debug();
         if (btree.size() > k) {
             btree.del(Node(nums[i-k], t));
         }
     }
     return false;
 }
Ejemplo n.º 2
0
int main()
{
    BinarySearchTree bst;
    bstNode* t = bst.Root();
    hcnode *h = NULL;
    hcnode a, b, c;
    int g=0;
    int t1=1;
    cout<<"enter '.' "<<"to end input"<<endl;
    while(t1)
    {                                                  //loop for input values
        cout<<"Enter char: ";
        cin>>a.c;
        if(a.c=='.')
        break;
        cout<<"Enter freq: ";
        cin>>a.freq;
        a.left = NULL;
        a.right = NULL;
        bst.add(t,a);
        g++;
    }
    if(g==1)                                          //if only one node is entered there is no traversal as why a code there is only one char;
    {
    	cout<<"only one element "<<endl;
    	exit(1);
    }

    while(!bst.check(t))                              //check upon number of nodes and finally only one bstnode remains with the hcnode with all data
    {
        a = bst.minimum(t);                           //gets first minimum hcnode from bst
        bst.del(t,a);                                //deletes the mininmum node from bst as it is already accessed
        b = bst.minimum(t);                           //next min node
        bst.del(t,b);                                 //del last node ie it is in hcnode b
        c = bst.minimum(t);                           //next min node
        bst.del(t,c);                                 //del last node ie it is in hcnode c
        add(h,a,b,c);                                 //adds the three nodes ie create a combined node
        bst.add(t,*h);                                //adds combined node to bst
    }
    int count=0;
    inorderc(t,count);                                    //if count is two add function has only two hcnodes
    if(count==2)
    {
    	if(t->lchild!=NULL)
    	{
    		add(h,t->data,t->lchild->data);               //h=node;a=node with max freq;b=node with less freq(h,a,b)
    	}
    	if(t->rchild!=NULL)
    	{
    		add(h,t->rchild->data,t->data);
    	}
    }
    hcnode*f=h;
    cout<<endl<<"printing level order "<<endl;
    levelorder(h);
    cout<<endl;
    string s;
    cout<<endl<<"Enter string: ";
    cin>>s;
    for(int i=0;i<s.length();i++)
    {
        if(s[i]=='1')
        {
            f = f->mid;
        }
        else if(s[i]=='0')
        {
            f = f->left;
        }
        else
        {
        	f=f->right;
        }
        if(f->left==NULL && f->right==NULL&&f->mid==NULL)
        {
            cout<<f->c;
            f = h;
        }
    }
    return 0;
}