Guest User

Untitled

a guest
Oct 30th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. Node flatten(Node &root, Node n, int rand){
  2.     Node child = (rand) ? preNode(n) : postNode(n) ;
  3.     Node p = child->parent;
  4.     Node tmp = (child->left) ? child->left : child->right;
  5.     if(tmp){
  6.         tmp->parent = p;
  7.     }
  8.     if(p == NULL){
  9.         root = tmp;
  10.     }
  11.     if(p->left == child){
  12.         p->left = tmp;
  13.     } else if(p->right == child){
  14.         p->right = tmp;
  15.     }
  16.  
  17.  
  18.     return child;
  19.  
  20. }
  21.  
  22.  
  23. Node removeNode(Node &root, Node n){
  24.     if(n == NULL) return NULL;
  25.     Node p = n->parent;
  26.     Node tmp;
  27.     if(n->left && n->right){
  28.         tmp = flatten(root, n, rand()%2);
  29.         tmp->left = n->left; if(n->left != NULL){ n->left->parent = tmp; };
  30.         tmp->right = n->right; if(n->right != NULL){ n->right->parent = tmp; };
  31.     } else {
  32.         tmp = (n->left) ? n->left : n->right;
  33.     }
  34.     if(tmp){ // jak zmienna jest lisciem to nie wstawiac parent
  35.         tmp->parent = p;
  36.     }
  37.     if(p == NULL){ // jak rodzic jest rootem
  38.         root = tmp;
  39.     } else if(p->left == n){
  40.         p->left = tmp;
  41.     } else if(p->right == n){
  42.         p->right = tmp;
  43.     }
  44.  
  45.     return n;
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment