diff --git a/CorrectTree.cpp b/CorrectTree.cpp new file mode 100644 index 0000000..359a54c --- /dev/null +++ b/CorrectTree.cpp @@ -0,0 +1,207 @@ +#include + +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< +#include +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 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;kadd_r(a[i]); + nd->add_r(y.a[i]); + } + if (min == size) { + for(int i=min;iadd_r(y.a[i]); + } + else for(int i=min;iadd_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"; + cout << a[i] << " "; + } + cout << endl; + } + void del() { + if (l <= 0) return ; + l--; + a[l]=0; + } + Stack sum (Stack b) { + int min; + Stack t(l+b.l); + if (l <= b.l) min = l; + else min = b.l; + for( int i=0; i + +using namespace std; + +class Stack { + private: + int size, l; + int *a; + public: + Stack (int s) { + size = s; + l=0; + a = new int[size]; + } + int add(int el) { + if (l >= size) return -1; + a[l]=el; + l++; + return 0; + } + int del() { + int b; + if (l <= 0) return 0; + l--; + b = a[l]; + a[l]=0; + return b; + } +}; + +void proverka(int * b, int n) { + int i=1; + for(i=i;b[i] > b[i-1] && i<=n;i++); + i++; + for(i=i;b[i] < b[i-1] && i<=n;i++); + if(i < n){ + cout << "Ne po poryadku zabiti wari" << endl; + return; + } + cout << "wari zabiti v pravilnom poryadke" << endl; +} + +int main () { + int n,i=0,*b,k,t,j; + cout << "Skolko warov" << endl; + cin >> n; + b = new int[n]; + Stack a(n); + for(j=n;j>0;j--) { + cout << "Kakoy war zakatit?" << endl; + cin >> k; + a.add(k); + cout << "Pomownik vitawil?" << endl; + cin >> t; + if(t != 0) { + k = a.del(); + b[i] = k; + i++; + } + } + for(k = a.del();k!=0;k = a.del()) { + b[i] = k; + i++; + } + for(i=0;i + +using namespace std; + +void hashadd (int *a,int n,int x) { + int k,kol=0; + k = x%n; + cout << endl << "x = " << x << endl; + while(a[k] != 0) { + k=(k+1)%n; + kol++; + if (k == x%n) { + cout << "Polniy massiv" << endl; + kol=0; + return; + } + } + cout << "Kolizii= " << kol << endl; + a[k]=x; + for(int i=0; i> n; + a = new int [n]; + for (int i=0;i +#include +void piramid (int *a,int n) { + int i,r,l,tmp,pmt,b; + for(i=(n-2)/2;i>=0;i--) { + b=i; + while (b=n) break; + if(ra[l]) l=r; + if(a[l]>a[b]) { + tmp=a[l]; + a[l]=a[b]; + a[b]=tmp; + b=l; + } + else break; + } + } + while(n>1) { + tmp=a[0]; + a[0]=a[n-1]; + a[n-1]=tmp; + n--; + b=0; + while(b=n) break; + if(ra[l]) l=r; + if(a[l]>a[b]) { + pmt=a[b]; + a[b]=a[l]; + a[l]=pmt; + b=l; + } + else break; + } + } +} + + + + +int main() { + + int *a,n,i; + scanf("%d", &n); + a=(int*)malloc(n*sizeof(int)); + for(i=0; i