Feedjit

Articles for you

Tuesday, December 31, 2013

Doubly LinkList in C++, Object Oriented Programming, Data Structure in C++ Addition, Deletion, Search

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<iostream>
using namespace std;
struct node
{

 node *next;
    int data ;
    node * prev;
 node () {  data=0; next=prev=NULL; } 
} ;
//////////////////////////////////////////////
class Dlinklist
{  
node *head ,*tail ;

public:

Dlinklist() { head = tail =NULL; }

void add_to_tail ();
void add_to_head();
void del_from_tail();
void del_from_head();
void show_from_head();
void show_from_tail();  
 };
/////////////////////////////////////////
void Dlinklist::add_to_head()
{
 
 node* temp = new node ();
cout<<"Enter the value"<<endl;
cin>>temp->data; 
 if(head==0)
{ head=tail=temp; }

else { 
 temp->next =head;
 head->prev=temp;
 head=temp; }
}
/////////////////////////////////////////////////////
void Dlinklist::add_to_tail()
{ 
 node* temp =new node();
cout<<"Enter the value"<<endl;
cin>>temp->data; 


if( tail==0)
{ head=tail=temp; }
else {

 tail->next=temp;
 temp->prev = tail;
 tail =temp ;
  } 
}
////////////////////////////////////////////////////
void Dlinklist::del_from_head()
{ if (head==0)
cout<<"Empty list"<<endl;
else 
{ node* temp =head;
head=head->next; head->prev=0; delete temp; }
}
//////////////////////////////////////////////////
void Dlinklist::del_from_tail()
{ if(tail==0)
{  cout<<"Empty List"<<endl; }

else {  node* temp=tail;  tail= tail->prev;  tail->next = 0; delete temp; } }
////////////////////////////////////////////////////
void Dlinklist::show_from_head()
{
 node* temp = head;
 while(temp!=0)
 {   cout<<endl<<temp->data;
 temp=temp->next;  } }
//////////////////////////////
void Dlinklist::show_from_tail()
 {   node* temp = tail;

while(temp!=0)
{ cout<<endl<<temp->data;
temp=temp->prev; } }
//////////////////////////

void main()
{ Dlinklist d1;
 int c;


 cout<<endl<<"---------MENU---------"<<endl;
do{ cout<<"1. Add value to head"<<endl;
  cout<<"2. Add value to tail"<<endl;
   cout<<"3. Show values from head"<<endl;
   cout<<"4. Show values from tail"<<endl;
   cout<<"5. Delete values from head"<<endl; 
   cout<<"6. Delete values from tail"<<endl;
   cout<<"Enter Your Choice"<<endl;
    
 
cin>>c;
switch(c)
 {
 case 1:d1.add_to_head();break;
 case 2:d1.add_to_tail();break;
 case 3:d1.show_from_head();break;
 case 4:d1.show_from_tail();break;
 case 5:d1.del_from_head();break;
 case 6: d1.del_from_tail(); break;
 default:cout<<"Invalid"<<endl;break;
}
 } while(1);
}

Easiest Implementation of AVL Tree in C++ Insertion, Deletion and Balancing AVL_Tree

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* Write a C++ program to perform the following operations on AVL-trees:
a) Insertion.
b) Deletion. */

#include<iostream>
#include<stdlib.h>
using namespace std;
#define TRUE 1
#define FALSE 0
#define NULL 0
class AVL;
class AVLNODE
{
 friend class AVL;
 private:
  int data;
  AVLNODE *left,*right;
  int bf;
};
class AVL
{
 private:
  AVLNODE *root;
 public:
  AVLNODE *loc,*par;
  AVL()
  {
   root=NULL;
  }
  int insert(int);
  void displayitem();
  void display(AVLNODE *);
  void removeitem(int);
  void remove1(AVLNODE *,AVLNODE *,int);
  void remove2(AVLNODE *,AVLNODE *,int);
  void search(int x);
  void search1(AVLNODE *,int);
};
int AVL::insert(int x)
{
 AVLNODE *a,*b,*c,*f,*p,*q,*y;
 int found,unbalanced;
 int d;
 if(root==0)   //special case empty tree
 {          y=new AVLNODE;
  y->data=x;
  root=y;
  root->bf=0;
  root->left=root->right=NULL;
  return TRUE; }
 //phase 1:locate insertion point for x.a keeps track of the most
 // recent node with balance factor +/-1,and f is the parent of a
 // q follows p through the tree.
 f=NULL;
 a=p=root;
 q=NULL;
 found=FALSE;
 while(p&&!found)
 {                 //search for insertion point for x
  if(p->bf)
  {
   a=p;
   f=q;
  }
  if(x<p->data)    //take left branch
  {
   q=p;
   p=p->left;
  }
  else if(x>p->data)
  {
   q=p;
   p=p->right;
  }
  else
  {
   y=p;
   found=TRUE;
  }
 }               //end while
 //phase 2:insert and rebalance.x is not in the tree and
 // may be inserted as the appropriate child of q.
 if(!found)
 {
  y = new AVLNODE;
  y->data=x;
  y->left=y->right=NULL;
  y->bf=0;
  if(x<q->data)    //insert as left child
  q->left=y;
  else
  q->right=y;    //insert as right child
  //adjust balance factors of nodes on path from a to q
  //note that by the definition of a,all nodes on this
  //path must have balance factors of 0 and so will change
  //to +/- d=+1 implies that x is inserted in the left
  // subtree of a d=-1 implies
  //to that x inserted in the right subtree of a.
 
 
if(x>a->data)
  {
   p=a->right;
   b=p;
   d=-1;
  }
  else
  {
   p=a->left;
   b=p;
   d=1;
  }
  while(p!=y)
  if(x>p->data)          //height of  right increases by 1
  {
   p->bf=-1;
   p=p->right;
  }
  else                 //height of left increases by 1
  {
   p->bf=1;
   p=p->left;
  }
  //is tree unbalanced
  unbalanced=TRUE;
  if(!(a->bf)||!(a->bf+d))
  {                   //tree still balanced
   a->bf+=d;
   unbalanced=FALSE;
  }
  if(unbalanced)   //tree unbalanced,determine rotation type
  {
   if(d==1)
   {         //left imbalance
    if(b->bf==1)      //rotation type LL
    {
     a->left=b->right;
     b->right=a;
     a->bf=0;
     b->bf=0;
    }
    else    //rotation type LR
    {
     c=b->right;
     b->right=c->left;
     a->left=c->right;
     c->left=b;
     c->right=a;
 
 
switch(c->bf)
     {
      case 1: a->bf=-1;  //LR(b)
       b->bf=0;
       break;
      case -1:b->bf=1;  //LR(c)
       a->bf=0;
       break;
      case 0: b->bf=0;  //LR(a)
       a->bf=0;
       break;
     }
     c->bf=0;
     b=c; //b is the new root
    } //end of LR
   }         //end of left imbalance
        else    //right imbalance
        {
    if(b->bf==-1)      //rotation type RR
    {
     a->right=b->left;
     b->left=a;
     a->bf=0;
     b->bf=0;
    }
    else    //rotation type LR
    {
     c=b->right;
     b->right=c->left;
     a->right=c->left;
     c->right=b;
     c->left=a;
     switch(c->bf)
     {
      case 1: a->bf=-1;  //LR(b)
       b->bf=0;
       break;
      case -1:b->bf=1;  //LR(c)
       a->bf=0;
       break;
      case 0: b->bf=0;  //LR(a)
       a->bf=0;
       break;
     }
     c->bf=0;
     b=c; //b is the new root
    } //end of LR
      }
//subtree with root b has been rebalanced and is the new subtree
 
if(!f)
   root=b;
   else if(a==f->left)
   f->left=b;
   else if(a==f->right)
   f->right=b;
  }   //end of if unbalanced
  return TRUE;
 }         //end of if(!found)
 return FALSE;
}     //end of AVL INSERTION
 
void AVL::displayitem()
{
 display(root);
}
void AVL::display(AVLNODE *temp)
{
 if(temp==NULL)
 return;
 cout<<temp->data<<" ";
 display(temp->left);
 display(temp->right);
}
void AVL::removeitem(int x)
{
 search(x);
 if(loc==NULL)
 {
  cout<<"\nitem is not in tree";
  return;
 }
 if(loc->right!=NULL&&loc->left!=NULL)
 remove1(loc,par,x);
 else
 remove2(loc,par,x);
}
void AVL::remove1(AVLNODE *l,AVLNODE *p,int x)
{
 AVLNODE *ptr,*save,*suc,*psuc;
 ptr=l->right;
 save=l;
 while(ptr->left!=NULL)
 {
  save=ptr;
  ptr=ptr->left;
 }
 suc=ptr;
 psuc=save;
 remove2(suc,psuc,x);
 if(p!=NULL)
  if(l==p->left)
   p->left=suc;
  else
   p->right=suc;
 else
  root=l;
  suc->left=l->left;
  suc->right=l->right;
   return;
}
void AVL::remove2(AVLNODE *s,AVLNODE *p,int x)
{
 AVLNODE *child;
 if(s->left==NULL && s->right==NULL)
  child=NULL;
 else if(s->left!=NULL)
  child=s->left;
 else
  child=s->right;
 if(p!=NULL)
  if(s==p->left)
   p->left=child;
  else
   p->right=child;
 else
  root=child;
 
}
void AVL::search(int x)
{
 search1(root,x);
}
void AVL::search1(AVLNODE *temp,int x)
{
       AVLNODE *ptr,*save;
       int flag;
       if(temp==NULL)
       {
  cout<<"\nthe tree is empty";
  return;
       }
       if(temp->data==x)
       {
  cout<<"\nthe item is root and is found";
  par=NULL;
  loc=temp;
  par->left=NULL;
  par->right=NULL;
  return;       }
       if( x < temp->data)
       {
  ptr=temp->left;
  save=temp;
       }
       else
       {
  ptr=temp->right;
  save=temp;
       }
       while(ptr!=NULL)
       {
  if(x==ptr->data)
  {       flag=1;
   cout<<"\nitemfound";
   loc=ptr;
   par=save;
 
  }
  if(x<ptr->data)
  ptr=ptr->left;
  else
  ptr=ptr->right;
       }
       if(flag!=1)
       {
  cout<<"\nitem is not there in tree";
  loc=NULL;
  par=NULL;
  cout<<loc;
  cout<<par;
     
}
}
 
void main()
{
 AVL a;
 int x,y,c;
        char ch; 
 do
 {
  cout<<"\n1.insert";
  cout<<"\n2.display";
  cout<<"\n3.delete";
  cout<<"\n4.search";
  cout<<"\n5.exit";
  cout<<"\nEnter u r choice to perform on AVL tree";
  cin>>c;
 
 
switch(c)
  {
   case 1:cout<<"\nEnter an element to insert into tree";
    cin>>x;
    a.insert(x);
    break;
   case 2:a.displayitem(); break;
   case 3:cout<<"\nEnter an item to deletion";
          cin>>y;
          a.removeitem(y);
          break;
   case 4:cout<<"\nEnter an element to search";
    cin>>c;
    a.search(c);
    break;
   case 5:exit(0); break;
        default :cout<<"\nInvalid option try again";
  }
  cout<<"\ndo u want to continue";
  cin>>ch;
 }
 while(ch=='y'||ch=='Y');
}

Fastest and Simplest Binary Search Tree in C++, Data Structure and Algorithm C++ Program

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//Binary Search Tree Program

#include <iostream>
#include <cstdlib>
using namespace std;

class BinarySearchTree
{
    private:
        struct tree_node
        {
           tree_node* left;
           tree_node* right;
           int data;
        };
        tree_node* root;
    
    public:
        BinarySearchTree()
        {
           root = NULL;
        }
       
        bool isEmpty() const { return root==NULL; }
        void print_inorder();
        void inorder(tree_node*);
        void print_preorder();
        void preorder(tree_node*);
        void print_postorder();
        void postorder(tree_node*);
        void insert(int);
        void remove(int);
};

// Smaller elements go left
// larger elements go right
void BinarySearchTree::insert(int d)
{
    tree_node* t = new tree_node;
    tree_node* parent;
    t->data = d;
    t->left = NULL;
    t->right = NULL;
    parent = NULL;
    
    // is this a new tree?
    if(isEmpty()) root = t;
    else
    {
        //Note: ALL insertions are as leaf nodes
        tree_node* curr;
        curr = root;
        // Find the Node's parent
        while(curr)
        {
            parent = curr;
            if(t->data > curr->data) curr = curr->right;
            else curr = curr->left;
        }

        if(t->data < parent->data)
           parent->left = t;
        else
           parent->right = t;
    }
}

void BinarySearchTree::remove(int d)
{
    //Locate the element
    bool found = false;
    if(isEmpty())
    {
        cout<<" This Tree is empty! "<<endl;
        return;
    }
    
    tree_node* curr;
    tree_node* parent;
    curr = root;
    
    while(curr != NULL)
    {
         if(curr->data == d)
         {
            found = true;
            break;
         }
         else
         {
             parent = curr;
             if(d>curr->data) curr = curr->right;
             else curr = curr->left;
         }
    }
    if(!found)
   {
        cout<<" Data not found! "<<endl;
        return;
    }


   // 3 cases :
    // 1. We're removing a leaf node
    // 2. We're removing a node with a single child
    // 3. we're removing a node with 2 children

    // Node with single child
    if((curr->left == NULL && curr->right != NULL)|| (curr->left != NULL
&& curr->right == NULL))
    {
       if(curr->left == NULL && curr->right != NULL)
       {
           if(parent->left == curr)
           {
             parent->left = curr->right;
             delete curr;
             cout<<" Item Deleted"<<endl;
     }
           else
           {
             parent->right = curr->right;
             delete curr;
             cout<<" Item Deleted"<<endl;
     }
       }
       else // left child present, no right child
       {
          if(parent->left == curr)
           {
             parent->left = curr->left;
             delete curr;
             cout<<" Item Deleted"<<endl; 
    }
           else
           {
             parent->right = curr->left;
             delete curr;
             cout<<" Item Deleted"<<endl;
     }
       }
     return;
    }

   //We're looking at a leaf node
   if( curr->left == NULL && curr->right == NULL)
    {
        if(parent->left == curr) parent->left = NULL;
        else parent->right = NULL;
      delete curr;
      cout<<" Item Deleted"<<endl;
     return;
    }


    //Node with 2 children
    // replace node with smallest value in right subtree
    if (curr->left != NULL && curr->right != NULL)
    {
        tree_node* chkr;
        chkr = curr->right;
        if((chkr->left == NULL) && (chkr->right == NULL))
        {
            curr = chkr;
            delete chkr;
            cout<<" Item Deleted"<<endl;
   curr->right = NULL;
        }
        else // right child has children
        {
            //if the node's right child has a left child
            // Move all the way down left to locate smallest element

            if((curr->right)->left != NULL)
            {
                tree_node* lcurr;
                tree_node* lcurrp;
                lcurrp = curr->right;
                lcurr = (curr->right)->left;
                while(lcurr->left != NULL)
                {
                   lcurrp = lcurr;
                   lcurr = lcurr->left;
                }
  curr->data = lcurr->data;
                delete lcurr;
                cout<<" Item Deleted"<<endl;
    lcurrp->left = NULL;
           }
           else
           {
               tree_node* tmp;
               tmp = curr->right;
               curr->data = tmp->data;
        curr->right = tmp->right;
               delete tmp;
           cout<<" Item Deleted"<<endl;
     }

        }
   return;
    }

}

void BinarySearchTree::print_inorder()
{
  inorder(root);
}

void BinarySearchTree::inorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) inorder(p->left);
        cout<<" "<<p->data<<" ";
        if(p->right) inorder(p->right);
    }
    else return;
}

void BinarySearchTree::print_preorder()
{
    preorder(root);
}

void BinarySearchTree::preorder(tree_node* p)
{
    if(p != NULL)
    {
        cout<<" "<<p->data<<" ";
        if(p->left) preorder(p->left);
        if(p->right) preorder(p->right);
    }
    else return;
}

void BinarySearchTree::print_postorder()
{
    postorder(root);
}

void BinarySearchTree::postorder(tree_node* p)
{
    if(p != NULL)
    {
        if(p->left) postorder(p->left);
        if(p->right) postorder(p->right);
        cout<<" "<<p->data<<" ";
    }
    else return;
}

int main()
{
    BinarySearchTree b;
    int ch,tmp,tmp1;
    while(1)
    {
       cout<<endl<<endl;
       cout<<" Binary Search Tree Operations "<<endl;
       cout<<" ----------------------------- "<<endl;
       cout<<" 1. Insertion/Creation "<<endl;
       cout<<" 2. In-Order Traversal "<<endl;
       cout<<" 3. Pre-Order Traversal "<<endl;
       cout<<" 4. Post-Order Traversal "<<endl;
       cout<<" 5. Removal "<<endl;
       cout<<" 6. Exit "<<endl;
       cout<<" Enter your choice : ";
       cin>>ch;
       switch(ch)
       {
           case 1 : cout<<" Enter Number to be inserted : ";
                    cin>>tmp;
                    b.insert(tmp);
                    break;
           case 2 : cout<<endl;
                    cout<<" In-Order Traversal "<<endl;
                    cout<<" -------------------"<<endl;
                    b.print_inorder();
                    break;
           case 3 : cout<<endl;
                    cout<<" Pre-Order Traversal "<<endl;
                    cout<<" -------------------"<<endl;
                    b.print_preorder();
                    break;
           case 4 : cout<<endl;
                    cout<<" Post-Order Traversal "<<endl;
                    cout<<" --------------------"<<endl;
                    b.print_postorder();
                    break;
           case 5 : cout<<" Enter data to be deleted : ";
                    cin>>tmp1;
                    b.remove(tmp1);
                    break;
           case 6 : 
                    return 0;
                    
       }
    }
}

Read More

Articles for you