Exemplo n.º 1
0
  // Insert a new element in the tree
  public boolean Insert(int v_key) {
    Tree new_node;
    boolean ntb;
    boolean cont;
    int key_aux;
    Tree current_node;

    new_node = new Tree();
    ntb = new_node.Init(v_key);
    current_node = this;
    cont = true;
    while (cont) {
      key_aux = current_node.GetKey();
      if (v_key < key_aux) {
        if (current_node.GetHas_Left()) current_node = current_node.GetLeft();
        else {
          cont = false;
          ntb = current_node.SetHas_Left(true);
          ntb = current_node.SetLeft(new_node);
        }
      } else {
        if (current_node.GetHas_Right()) current_node = current_node.GetRight();
        else {
          cont = false;
          ntb = current_node.SetHas_Right(true);
          ntb = current_node.SetRight(new_node);
        }
      }
    }
    return true;
  }
Exemplo n.º 2
0
  // Copy the child key to the parent until a leaf is
  // found and remove the leaf. This is done with the
  // right subtree
  public boolean RemoveRight(Tree p_node, Tree c_node) {
    boolean ntb;

    while (c_node.GetHas_Right()) {
      // auxtree01 = c_node.GetRight() ;
      // auxint02 = auxtree01.GetKey();
      // ntb = c_node.SetKey(auxint02);
      ntb = c_node.SetKey((c_node.GetRight()).GetKey());
      p_node = c_node;
      c_node = c_node.GetRight();
    }
    ntb = p_node.SetRight(my_null);
    ntb = p_node.SetHas_Right(false);
    return true;
  }
Exemplo n.º 3
0
  // Check if the element to be removed will use the
  // righ or left subtree if one exists
  public boolean Remove(Tree p_node, Tree c_node) {
    boolean ntb;
    int auxkey1;
    int auxkey2;

    if (c_node.GetHas_Left()) ntb = this.RemoveLeft(p_node, c_node);
    else if (c_node.GetHas_Right()) ntb = this.RemoveRight(p_node, c_node);
    else {
      auxkey1 = c_node.GetKey();
      // auxtree01 = p_node.GetLeft() ;
      // auxkey2 = auxtree01.GetKey() ;
      auxkey2 = (p_node.GetLeft()).GetKey();
      if (this.Compare(auxkey1, auxkey2)) {
        ntb = p_node.SetLeft(my_null);
        ntb = p_node.SetHas_Left(false);
      } else {
        ntb = p_node.SetRight(my_null);
        ntb = p_node.SetHas_Right(false);
      }
    }
    return true;
  }