Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <string>
- #include <ctime>
- using namespace std;
- string cr,cl,cp;
- struct node
- {
- int value;
- node *left;
- node *right;
- };
- void push(node **&tab, node *H, int &top)
- {
- top++;
- tab[top]=H;
- }
- void pop(int &top)
- {
- top--;
- }
- void add(node *&H, int x)
- {
- if (H == NULL)
- {
- H = new node;
- H->left = NULL;
- H->right = NULL;
- H->value = x;
- }
- else
- {
- if ( x < H->value)
- add(H->left, x);
- else
- add(H->right, x);
- }
- }
- void add(node *&H, int x, node *&p)
- {
- if (H == NULL)
- {
- H = new node;
- H->left = NULL;
- H->right = NULL;
- H->value = x;
- p=H;
- }
- else
- {
- if ( x < H->value)
- add(H->left, x, p);
- else
- add(H->right, x, p);
- }
- }
- node* min(node *H)
- {
- if(H!=NULL)
- {
- node *temp=H;
- while(temp->left!=NULL)
- temp=temp->left;
- return temp;
- }
- }
- node* max(node *H)
- {
- if(H!=NULL)
- {
- node *temp=H;
- while(temp->right!=NULL)
- temp=temp->right;
- return temp;
- }
- }
- node* poprzednik(node *H, node*p)
- {
- if(H!=NULL)
- {
- node *temp=H;
- while(temp->left!=p && temp->right!=p)
- {
- if(p->value<temp->value)
- temp=temp->left;
- else
- temp=temp->right;
- }
- return temp;
- }
- }
- node* nastepnik(node *H, node *p)
- {
- if(p->right)
- {
- node *temp=p->right;
- if(temp->left==NULL)
- {
- return temp;
- }
- else
- {
- while(temp->left!=NULL)
- temp=temp->left;
- return temp;
- }
- }
- }
- void removetree(node *&H, node *p)
- {
- if(H!=NULL)
- {
- node *temp=NULL;
- temp=poprzednik(H, p);
- if(temp->left==p)
- temp->left=NULL;
- else
- temp->right=NULL;
- }
- }
- void remove(node *&H, node *p)
- {
- if(H!=NULL)
- {
- if(H==p)
- {
- node *temp=nastepnik(H, p);
- remove(H,nastepnik(H, p));
- temp->left=H->left;
- temp->right=H->right;
- H=temp;
- }
- else
- {
- node *temp=poprzednik(H,p);
- if(temp->left==p)
- {
- if(p->left==NULL && p->right==NULL)
- temp->left=NULL;
- else if(p->left==NULL && p->right!=NULL)
- temp->left=p->right;
- else if(p->left!=NULL && p->right==NULL)
- temp->left=p->left;
- else if(p->left!=NULL && p->right!=NULL)
- {
- node *tmp2=nastepnik(H, p);
- remove(H, nastepnik(H,p));
- node *tmp=p->left;
- node *tmp1=p->right;
- tmp2->left=tmp;
- tmp2->right=tmp1;
- temp->left=tmp2;
- }
- }
- else
- {
- if(p->left==NULL && p->right==NULL)
- temp->right=NULL;
- else if(p->left==NULL && p->right!=NULL)
- temp->right=p->right;
- else if(p->left!=NULL && p->right==NULL)
- temp->right=p->left;
- else if(p->left!=NULL && p->right!=NULL)
- {
- node *tmp2=nastepnik(H,p);
- remove(H, nastepnik(H,p));
- node *tmp=p->left;
- node *tmp1=p->right;
- tmp2->left=tmp;
- tmp2->right=tmp1;
- temp->right=tmp2;
- }
- }
- }
- }
- }
- void lustro(node *&H)
- {
- if(H!=NULL)
- {
- lustro(H->left);
- lustro(H->right);
- node *temp=H->left;
- H->left=H->right;
- H->right=temp;
- }
- }
- void deleteleaf(node *&H, node **&tab, int &top)
- {
- if(H!=NULL)
- {
- deleteleaf(H->left, tab, top);
- if(H->left==NULL && H->right==NULL)
- {
- push(tab, H, top);
- }
- deleteleaf(H->right, tab, top);
- }
- }
- void leaf(node *&H, node **&tab, int &top)
- {
- if(H!=NULL)
- {
- while(top>=0)
- {
- node *temp=tab[top];
- remove(H, temp);
- pop(top);
- }
- }
- }
- void inorder(node *H)
- {
- if(H)
- {
- inorder(H->left);
- cout<<H->value<<" ";
- inorder(H->right);
- }
- }
- void printBT(string sp, string sn, node * v)
- {
- string s;
- if(v)
- {
- s = sp;
- if(sn == cr) s[s.length() - 2] = ' ';
- printBT(s + cp, cr, v->right);
- s = s.substr(0,sp.length()-2);
- cout << s << sn << v->value << endl;
- s = sp;
- if(sn == cl) s[s.length() - 2] = ' ';
- printBT(s + cp, cl, v->left);
- }
- }
- int main()
- {
- srand(time(NULL));
- node *H = NULL;
- node *p=NULL;
- node **tab=new node *[40];
- int top=-1;
- int i, k;
- cr = cl = cp = " ";
- cr[0] = 218; cr[1] = 196;
- cl[0] = 192; cl[1] = 196;
- cp[0] = 179;
- for(int i=0;i<40;i++)
- {
- if(i==3)
- add(H, rand()%100, p);
- add(H, rand()%100);
- }
- printBT("","",H);
- lustro(H);
- inorder(H);
- printBT("","",H);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement