Код Perl: sub _move_unit { my ($self, $data) = @_; if ($data->{'near'} >= $data->{'left'} && $data->{'near'} <= $data->{'right'}) {return 0} my $skew_tree = $data->{'right'} - $data->{'left'} + 1; my $skew_level = $data->{'level_new'} - $data->{'level'}; if ($data->{'right'} < $data->{'near'}) { my $skew_edit = $data->{'near'} - $data->{'left'} + 1 - $skew_tree; $self->{'DBI'}->do('UPDATE '.$self->{'table'}. ' SET '. $self->{'left'}.' = CASE WHEN '.$self->{'right'}.' <= '.$data->{'right'}.' THEN '. $self->{'left'}.' + '.$skew_edit.' ELSE CASE WHEN '.$self->{'left'}.' > '.$data->{'right'}.' THEN '. $self->{'left'}.' - '.$skew_tree.' ELSE '.$self->{'left'}.' END END, '. $self->{'level'}.' = CASE WHEN '.$self->{'right'}.' <= '.$data->{'right'}.' THEN '. $self->{'level'}.' + '.$skew_level.' ELSE '.$self->{'level'}.' END, '. $self->{'right'}.' = CASE WHEN '.$self->{'right'}.' <= '.$data->{'right'}.' THEN '. $self->{'right'}.' + '.$skew_edit.' ELSE CASE WHEN '.$self->{'right'}.' <= '.$data->{'near'}.' THEN '. $self->{'right'}.' - '.$skew_tree.' ELSE '.$self->{'right'}.' END END WHERE '. $self->{'right'}.' > '.$data->{'left'}.' AND '. $self->{'left'}.' <= '.$data->{'near'}. ($self->{'type'} eq 'M' ? ' AND '.$self->{'multi'}.'='.$data->{'multi'} : '')) || die $DBI::errstr; } else { my $skew_edit = $data->{'near'} - $data->{'left'} + 1; $self->{'DBI'}->do('UPDATE '.$self->{'table'}. ' SET '. $self->{'right'}.' = CASE WHEN '.$self->{'left'}.' >= '.$data->{'left'}.' THEN '. $self->{'right'}.' + '.$skew_edit.' ELSE CASE WHEN '.$self->{'right'}.' < '.$data->{'left'}.' THEN '. $self->{'right'}.' + '.$skew_tree.' ELSE '.$self->{'right'}.' END END, '. $self->{'level'}.' = CASE WHEN '.$self->{'left'}.' >= '.$data->{'left'}.' THEN '. $self->{'level'}.' + '.$skew_level.' ELSE '.$self->{'level'}.' END, '. $self->{'left'}.' = CASE WHEN '.$self->{'left'}.' >= '.$data->{'left'}.' THEN '. $self->{'left'}.' + '.$skew_edit.' ELSE CASE WHEN '.$self->{'left'}.' > '.$data->{'near'}.' THEN '. $self->{'left'}.' + '.$skew_tree.' ELSE '.$self->{'left'}.' END END WHERE '. $self->{'right'}.' > '.$data->{'near'}.' AND '. $self->{'left'}.' < '.$data->{'right'}. ($self->{'type'} eq 'M' ? ' AND '.$self->{'multi'}.'='.$data->{'multi'} : '')) || die $DBI::errstr; } return 1 } Код PHP: // Внутренняя процедура перемещения узла function _move_unit ($data = array()) { if ($data['near'] >= $this->unit['left'] && $data['near'] <= $this->unit['right']) {return 'ERR';} $skew_tree = $this->unit['right'] - $this->unit['left'] + 1; $skew_level = $data['level_new'] - $this->unit['level']; // Перемещение вверх по дереву if ($this->unit['right'] < $data['near']) { $skew_edit = $data['near'] - $this->unit['left'] + 1 - $skew_tree; $sql = 'UPDATE '.$this->table.' SET '. $this->left.' = CASE WHEN '.$this->right.'<='.$this->unit['right'].' THEN '. $this->left.'+'.$skew_edit.' ELSE CASE WHEN '.$this->left.'>'.$this->unit['right'].' THEN '. $this->left.'-'.$skew_tree.' ELSE '.$this->left.' END END, '. $this->level.' = CASE WHEN '.$this->right.'<='.$this->unit['right'].' THEN '. $this->level.'+'.$skew_level.' ELSE '.$this->level.' END, '. $this->right.' = CASE WHEN '.$this->right.'<='.$this->unit['right'].' THEN '. $this->right.'+'.$skew_edit.' ELSE CASE WHEN '.$this->right.'<='.$data['near'].' THEN '. $this->right.'-'.$skew_tree.' ELSE '.$this->right.' END END WHERE '. $this->right.'>'.$this->unit['left'].' AND '. $this->left.'<='.$data['near']. ($this->type == 'M' ? ' AND '.$this->multi.'='.$this->unit['multi'] : ''); $mysql_query = mysql_query($sql); } else { // Перемещение вниз по дереву $skew_edit = $data['near'] - $this->unit['left'] + 1; $sql = 'UPDATE '.$this->table.' SET '. $this->right.' = CASE WHEN '.$this->left.'>='.$this->unit['left'].' THEN '. $this->right.'+'.$skew_edit.' ELSE CASE WHEN '.$this->right.'<'.$this->unit['left'].' THEN '. $this->right.'+'.$skew_tree.' ELSE '.$this->right.' END END, '. $this->level.' = CASE WHEN '.$this->left.'>='.$this->unit['left'].' THEN '. $this->level.'+'.$skew_level.' ELSE '.$this->level.' END, '. $this->left.' = CASE WHEN '.$this->left.'>= '.$this->unit['left'].' THEN '. $this->left.'+'.$skew_edit.' ELSE CASE WHEN '.$this->left.'>'.$data['near'].' THEN '. $this->left.'+'.$skew_tree.' ELSE '.$this->left.' END END WHERE '. $this->right.'>'.$data['near'].' AND '. $this->left.'<'.$this->unit['right']. ($this->type == 'M' ? ' AND '.$this->multi.'='.$this->unit['multi'] : ''); $mysql_query = mysql_query($sql); } return 'OK'; }
|