-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c38ecd
commit e1184e4
Showing
6 changed files
with
677 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class treeitem { | ||
|
||
private: | ||
int value; | ||
treeitem *l,*r,*f; | ||
treeitem(int x=0) { | ||
value=x; | ||
l=NULL; | ||
r=NULL; | ||
f=NULL; | ||
} | ||
~treeitem() | ||
{ | ||
value=0; | ||
l=NULL; | ||
r=NULL; | ||
f=NULL; | ||
} | ||
friend class tree; | ||
}; | ||
class tree { | ||
private: | ||
treeitem *root; | ||
public: | ||
tree () { | ||
root=new treeitem(); | ||
root= NULL; | ||
} | ||
|
||
void add(int x) { | ||
treeitem *w,*an; | ||
int z; | ||
if(root == NULL) { | ||
root = new treeitem(x); | ||
return; | ||
} | ||
for(w=root;w!=NULL; ) { | ||
if( x == w->value) { | ||
cout << "Odinakoviy Element" << endl; | ||
return; | ||
} | ||
else if(x > w->value) { | ||
an=w; | ||
w=w->r; | ||
z=2; | ||
} | ||
else if(x < w->value) { | ||
an=w; | ||
w=w->l; | ||
z=1; | ||
} | ||
} | ||
w=new treeitem(x); | ||
w->f=an; | ||
if(z==1) { | ||
an->l=w; | ||
} | ||
if(z==2) { | ||
an->r=w; | ||
} | ||
} | ||
|
||
void show () { | ||
if(root == NULL) return; | ||
showr(root); | ||
cout << endl; | ||
} | ||
void showr (treeitem *node) { | ||
cout << node->value << " "; | ||
if(node->l != NULL) { | ||
cout << node->value << "L"; | ||
showr(node->l); | ||
} | ||
if(node->r != NULL) { | ||
cout << node->value << "R"; | ||
showr(node->r); | ||
} | ||
} | ||
treeitem *search (int x) { | ||
treeitem *w; | ||
w=root; | ||
while(w != NULL && w->value != x) { | ||
if(x > w->value) w=w->r; | ||
else w=w->l; | ||
} | ||
if(w == NULL) { | ||
//cout << "Net takoqo elementa" << endl; | ||
return NULL; | ||
} | ||
return w; | ||
} | ||
treeitem* f1(treeitem *root,treeitem *t) { | ||
treeitem *w; | ||
w = t->l; | ||
while(w->r != NULL) { | ||
w = w->r; | ||
} | ||
if(w->l) w->l->f=w->f; | ||
if(!w->f){root=w->l; delete t; return NULL;} | ||
else { | ||
if(w->f->l == w) w->f->l=w->l; | ||
else if (w->f->r == w) w->f->r=w->l; | ||
} | ||
return w; | ||
} | ||
treeitem * f2(treeitem *root,treeitem *t) { | ||
treeitem *w; | ||
w = t->r; | ||
while(w->l != NULL) { | ||
w=w->l; | ||
} | ||
if(w->r) w->r->f = w->f; | ||
if(!w->f) {root=w->r; delete t; return NULL;} | ||
else { | ||
if(w->f->l == w) w->f->l=w->r; | ||
else if (w->f->r == w) w->f->r=w->r; | ||
} | ||
return w; | ||
} | ||
void del(int x) { | ||
treeitem *t, *w; | ||
t=search(x); | ||
if (t==NULL) { | ||
cout << "Net elementa" << endl; | ||
return; | ||
} | ||
if(t->l == NULL && t->r == NULL) { | ||
if(t->f != NULL) { | ||
if(t->f->l == t){t->f->l = NULL;} | ||
if(t->f->r == t) t->f->r = NULL; | ||
} | ||
if(t==root) { | ||
delete t; | ||
root = NULL; | ||
} | ||
t=NULL; | ||
delete t; | ||
show(); | ||
return; | ||
} | ||
else if(t->l == NULL || t->r == NULL) { | ||
if(t->l==NULL) w=f2(root,t); | ||
else w=f1(root,t); | ||
} | ||
else if(t->l != NULL && t->r != NULL) {w=f1(root,t);} | ||
w->f = t->f; | ||
if(t!=root) | ||
{if(t->f->l == t) t->f->l = w; | ||
else t->f->r = w;} | ||
else root = w; | ||
w->l = t->l; | ||
w->r = t->r; | ||
t=NULL; | ||
delete t; | ||
} | ||
}; | ||
|
||
|
||
int main () { | ||
tree a; | ||
//a.add(1); | ||
cout << "*** Do pervoqo udaleniya ***\n" << endl; | ||
a.show(); | ||
//a.del(1); | ||
a.add(8); | ||
a.show(); | ||
a.add(6); | ||
a.show(); | ||
a.add(7); | ||
a.show(); | ||
a.add(4); | ||
a.show(); | ||
a.add(3); | ||
a.show(); | ||
a.add(5); | ||
a.show(); | ||
a.add(2); | ||
a.show(); | ||
cout<<endl<<endl; | ||
a.del(8); | ||
a.show(); | ||
// a.show(); | ||
// cout << "\n\n*** Posle udaleniya i dobovleniya elementov ***\n" << endl; | ||
// a.show(); | ||
// a.del(4); | ||
// cout << "\n\n*** Posle vtoroqo udaleniya ***\n" << endl; | ||
// a.show(); | ||
// a.del(2); | ||
// cout << "\n\n*** Posle tret'eqo udaleniya ***\n" << endl; | ||
// a.show(); | ||
/*a.add(10); | ||
a.del(10); | ||
a.add(19); | ||
a.add(16); | ||
a.add(14); | ||
a.add(18); | ||
a.add(15); | ||
a.add(17); | ||
a.show(); | ||
a.del(10); | ||
a.show();*/ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
#include <iostream> | ||
#include <cstdlib> | ||
using namespace std; | ||
|
||
class Deque { | ||
private: | ||
int size,maxsize,l,r; | ||
int *a; | ||
public: | ||
Deque(int s) { | ||
maxsize=s; | ||
r=0; | ||
l=s-1; | ||
size=0; | ||
a=new int[s]; | ||
} | ||
friend ostream &operator << (ostream &stream , Deque y); | ||
friend int operator + (int z, Deque &y); | ||
void add_l (int x) { | ||
if(size==maxsize) return; | ||
a[l]=x; | ||
l=(maxsize+l-1)%maxsize; | ||
size++; | ||
} | ||
void add_r (int x) { | ||
if(size==maxsize) return; | ||
a[r]=x; | ||
r=(r+1)%maxsize; | ||
size++; | ||
} | ||
void show () { | ||
int i,k; | ||
if(size == 0) cout << "Pustoy deq" << endl; | ||
for(i=(l+1)%maxsize,k=0;k<size;i=(i+1)%maxsize,k++) { | ||
cout << a[i] << " " ; | ||
} | ||
cout << endl; | ||
} | ||
void del_l () { | ||
if(size > 0) { | ||
l=(l+1)%maxsize; | ||
a[l]=0; | ||
size--; | ||
} | ||
else return; | ||
} | ||
void del_r () { | ||
if(size > 0) { | ||
r=(maxsize + r - 1)%maxsize; | ||
a[r]=0; | ||
size--; | ||
} | ||
else return; | ||
} | ||
Deque &operator=(Deque &y) { | ||
delete a; | ||
a = new int [y.maxsize]; | ||
maxsize = y.maxsize; | ||
size = y.size; | ||
l = y.l; | ||
r = y.r; | ||
for(int i=(l+1)%maxsize,k=0;k<size ;k++, i=(i+1)%maxsize) { | ||
a[i]=y.a[i]; | ||
|
||
} | ||
//cout << y.l << endl << y.r << endl; | ||
return *this; | ||
} | ||
Deque operator+(Deque &y) { | ||
int min; | ||
Deque *nd; | ||
nd = new Deque(maxsize + y.maxsize); | ||
if(size <= y.size) min = size; | ||
else min = y.size; | ||
for(int i=0;i<min;i++) { | ||
nd->add_r(a[i]); | ||
nd->add_r(y.a[i]); | ||
} | ||
if (min == size) { | ||
for(int i=min;i<y.size;i++) nd->add_r(y.a[i]); | ||
} | ||
else for(int i=min;i<size;i++) nd->add_r(a[i]); | ||
|
||
return *nd; | ||
} | ||
// Deuqe + number | ||
Deque operator + (int z) { | ||
Deque na(maxsize); | ||
na.size = size; | ||
na.l=l; | ||
na.r=r; | ||
for (int i=(l+1)%maxsize,k=0;k<size;k++,i=(i+1)%maxsize) { | ||
na.a[i] = a[i] + z; | ||
} | ||
return na; | ||
} | ||
|
||
}; | ||
ostream &operator << (ostream &stream , Deque y) { | ||
int i,k; | ||
if(y.size == 0) cout << "Pustoy deq" << endl; | ||
for(i=(y.l+1)%y.maxsize,k=0;k<y.size;i=(i+1)%y.maxsize,k++) { | ||
cout << y.a[i] << " " ; | ||
} | ||
cout << endl; | ||
return stream; | ||
} | ||
//number + Deque | ||
int operator + (int z, Deque &y) { | ||
int sum=z; | ||
for (int i=(y.l+1)%y.maxsize,k=0;k<y.size;k++,i=(i+1)%y.maxsize) { | ||
if(k%2==0) sum += y.a[i]; | ||
else sum -= y.a[i]; | ||
} | ||
return sum; | ||
} | ||
|
||
int main () { | ||
Deque a(10); | ||
int i; | ||
/*for(int i=0;i<5;i++) { | ||
a.add_l(rand()%30); | ||
a.add_r(rand()%30); | ||
}*/ | ||
for(i=1;i<8;i++) { | ||
a.add_r(i); | ||
} | ||
Deque b(4); | ||
for(i=8;i<12;i++) { | ||
b.add_r(i); | ||
} | ||
Deque c=a+b; | ||
//c.show(); | ||
b=c; | ||
//b.show(); | ||
Deque d=a+4; | ||
cout << " a+4" << endl; | ||
cout << "a =" << a ; | ||
cout << "a+4=" << d << endl; | ||
//cout << b << endl; | ||
int k; | ||
|
||
k = 4 + a; | ||
cout << " 4+a" << endl; | ||
cout << "a =" << a ; | ||
cout << "4+a=" << k << endl; | ||
return 0; | ||
} |
Oops, something went wrong.