0

For several days now I am trying to cope with the algorithm implementation at the online shop which I am writing in PHP. I do not know whether the problem is only the implementation, or perhaps bad algorithm design. Hovewer, for me it seems fine. I only haven`t checked its complexity of it, but it's such a problem.

After a long deliberation on the same algorithm, without thinking on implementation I came up with the use of binary search tree (bst) with additional data inserted into list consist of user defined info (later about it). The whole orders list would be displayed, or returned using inorder method.

I write it like that:

  • If the input object date is greater than current object go right

  • If the input object date is less than current object go left

  • If the dates are the same stay at place

  • If the field is blank check if the product is in stock

    • If it is put into place and finish

    • If there is not do nothing and exit

  • If the field is full

{Check if on the list is this user id

  • If yes than check order priority

  • If no do nothing and exit

    • Check if there is product on stock
  • If yes replace record and exit

  • If no do nothing and exit }

{If there is not user id on the list check if product is on stock

  • If yes then put element on the end

  • If no do nothing and exit

}

Maybe it looks a little bad, but I was not able to do indentation.

Data is transferred into algorithm in a loop until the end of orders list. The list is unordered.

This is my implementation:

class BinaryTree {    
    private $predescor = array(
        'd'=>array('data'=>0),
        'p'=>null,
        'r'=>null,
        'l'=>null
    );

    private $ancestor = array(
        'd'=>array('data'=>0),
        'p'=>null,
        'r'=>null,
        'l'=>null
    );

    private $i = 0;

    public function insertIntoTree(&$root,$element)
    {
        $this->predescor = $root;
        $this->predescor;

        while($this->predescor)
        {
            if($element['d']['data']==$this->predescor['d']['data'])
            {
                $this->inertIntoList($element,$this->predescor['d']);
                return true;
            }

            $this->predescor = $this->predescor;
            if($element['d']['data']<$this->predescor['d']['data'])
            {
                $this->predescor = $this->predescor['l'];
            }
            else
            {
                $this->predescor = $this->predescor['r'];
            }
        }

        $element['p'] = $this->predescor;

        if(!$this->predescor)
        {
            $root = $element;
        }
        else if($element['d']['data']<$this->predescor['d']['data'])
        {
            $this->predescor['l'] = $element;
        }
        else
        {
            $this->predescor['r'] = $element;
        }

        return true;
    }    
    public function putList(&$list,$root)
    {
        if($root!=null)
        {
            $this->putList($list, $root['l']);
            $lista[$this->i] = $root;
            $this->i++;
            $this->putList($list, $root['r']);
        }
        return;
    }

    private function insertIntoList($element,&$position)
    {
        if($position == null)
        {
            $position = $element;
            return true;
        }

        foreach($position['user'] as &$key)
        {
            if($key == $element['d']['user'])
            {
                if($key['priority']<$element['d']['user']['priority'])
                {
                    return false;
                }
                else if($key['priority']==$element['d']['user']['priority'])
                {
                    return false;
                }
                else
                {
                    if(Orders::checkOrder($element['d']['user']['order']))
                    {
                        $key['order'] = $element['d']['user']['order'];
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        //@todo add at the end
        return true;
    }
}

I would like to advise whether there is a simpler way than using bst consisting of a quite complex arrays, which would also be easier to implement? Because now I can not inplement it in PHP.

Thank you in advance.

4
  • 1
    tl;dr.... make your posts (especially your first ones) short and to the point at first or else it just gets too long to read... Commented Dec 5, 2011 at 14:53
  • Changed. I hope now it is shorter. Commented Dec 5, 2011 at 14:58
  • If it seems fine to you, what is the problem? What are you trying to do? Commented Dec 5, 2011 at 15:10
  • Yeap, but I have god problems with implementing it into php. Algorithm alone is correct, maybe not beautiful but correct. Commented Dec 5, 2011 at 16:36

1 Answer 1

1

I wouldn't start by coding this in php at all.

I'd start by building this into the database. ("Orders" implies a database.) I'd start by clarifying a couple of points. Assuming that one order can have many line items . . .

  • The number of days since the last order seems to clearly apply to the order, not to individual products.
  • The user can have "only one request carried at a time". Request for what? Doesn't seem to make sense for this to apply either to an order or to an order's line item.
  • The order priority seems to clearly apply to the order, not to line items. But a line-item priority might make more sense. (What products does the customer need first?)
  • Whether the product is in stock seems to apply to the line items, not to the order as a whole.

I'd start by creating two views. (Not because you'll eventually need two views, but because some things are still unclear.)

One view, which has to do with "ranking" as applied to an order, would calculate or display three things.

  • Number of days since the last order.
  • Is this order the "one request carried at a time"?
  • The order priority.

If the numbers assigned to these three things are consistent in scale, you can just sort on those three columns. But that's not likely. You'll probably need to weight each factor, possibly by multiplying by a "weighting" factor. A calculation on the result should let you put these in a useful order. It's not yet clear whether the calculation is best done in the view or in a stored procedure.

The other view would have to do with whether a line item is in stock. It's not clear whether one line item out of stock means the whole order is incomplete, or whether one line item out of stock changes the calculation of a weighted number that scales along with the others above. (You can make a good argument for each of those approaches.)

Sign up to request clarification or add additional context in comments.

1 Comment

So to clarify somethings: only one request carried at a time - user can have only one order proceeded at a time so one day you proceed with order x of the user and the other order y of this same user. And the second thing - if only one product isnt on stock then whole order isnt valid. So to sum this up you propose to make all operations in Database? Quite good attitude, but I don`t know what would happen if you would like to get let say only 10 orders out of 1000. Do you have to count all?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.