Guest User

Untitled

a guest
May 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. TreeLink deleteNode(TreeLink t, char* word){
  2.     TreeLink temp;
  3.     if (t == NULL){
  4.         return NULL;
  5.     }
  6.     else if (strcmp(word, t->key) < 0){
  7.         t->left = deleteNode(t->left, word);
  8.     //  if (depth(t->right) - depth(t->left) == 2){
  9.     //      if (strcmp(word, t->right->key) < 0){
  10.     //          t = leftRotate(t);
  11.     //      }
  12.     //      else{
  13.     //          t = rightLeftRotate(t);
  14.     //      }
  15.     //  }
  16.     }
  17.     else if (strcmp(word,t->key) > 0){
  18.         t->right = deleteNode(t->right, word);
  19.     //  if (depth(t->left) - depth(t->right) == 2){
  20.     //      if (strcmp(word, t->left->key) > 0){
  21.     //          rightRotate(t);
  22.     //      }
  23.     //      else{
  24.     //          t = leftRightRotate(t);
  25.     //      }
  26.     //  }
  27.     }
  28.     else{// if (strcmp(word, t->key) == 0){
  29.         //if (t->left == NULL && t->right == NULL){ //if we delete a leaf
  30.             //deletelist(t->rowlist);
  31.         //  free(t);
  32.             //t = NULL;
  33.     //  }
  34.         if (t->left != NULL &&  t->right != NULL){ // if we delete node with 2 childs
  35.                 temp = maxValue(t->left); // we choose biggest element at the left subtree
  36.                 t->key = temp->key;
  37.                 t->rowlist = temp->rowlist;
  38.                 //deletelist(temp->rowlist);
  39.                 free(temp);
  40.                 t->left = deleteNode(t->left, t->key); // continue so we can fall into one of the o
  41.  
  42.         }
  43.         else{ // when we want to delete a node with no childs or 1 child.
  44.             temp = t;
  45.             if (t->left == NULL){
  46.                 t = t->right; // linking nodes together
  47.             }
  48.             else if (!t->right == NULL){
  49.                 t = t->left; // linking nodes together
  50.             }
  51.             //deletelist(temp->rowlist);
  52.             free(temp);
  53.         }
  54.     }
  55.     return t;
  56. }
Add Comment
Please, Sign In to add comment