dongpang1898 2015-03-24 01:23
浏览 36
已采纳

使用存储在大型XML文件(PHP)中的二叉树林

I have an array like 'var1'=>1.05, 'var2'=>0.76,... and a forest of binary trees stored in a 100+ MB XML file.

<Tree id="1">
<Node id="2">
   <SimplePredicate field="var1" operator="lessOrEqual" value="1.41"/>
   <Node id="4">
     <SimplePredicate field="var2" operator="lessOrEqual" value="1.43"/>
     .......
     </Node>
</Node>
<Node id="3">
   <SimplePredicate field="var1" operator="greaterThan" value="1.41"/>
   .......
</Node>
</Tree>

What I'd like to do in PHP is for each tree to store properties of a leaf in which I'll end up based on the conditions given by each node. So in this example the path will be (2)->(4)->...

Because of the file size it's clear XMLReader is the proper tool for reading each tree. Because the trees are quite small, they can be stored into memory while working with each. What would be the most straightforward way to work with the trees?

  • 写回答

1条回答 默认 最新

  • dqzg62440 2015-03-24 07:01
    关注

    You're on the right track with XMLReader. Rather conveniently it includes the method expand() which will return a copy of the current node as a DOMNode. This will let you handle each individual Tree in memory with the DOM API.

    As for handling nodes - evaluate and descend recursively.


    Example:

    $data = [
        'var1' => 1.05,
        'var2' => 0.76
    ];
    
    $dom    = new DOMDocument();
    $xpath  = new DOMXPath($dom);
    $reader = new XMLReader();
    $reader->open('forest.xml');
    
    // Read until reaching the first Tree.
    while ($reader->read() && $reader->localName !== 'Tree');
    
    while ($reader->localName === 'Tree') {
        $tree = $dom->importNode($reader->expand(), true);
    
        echo evaluateTree($data, $tree, $xpath), "
    ";
    
        // Move on to the next.
        $reader->next('Tree');
    }
    
    $reader->close();
    
    function evaluateTree(array $data, DOMElement $tree, DOMXPath $xpath)
    {
        foreach ($xpath->query('./Node', $tree) as $node) {
            $field    = $xpath->evaluate('string(./SimplePredicate/@field)', $node);
            $operator = $xpath->evaluate('string(./SimplePredicate/@operator)', $node);
            $value    = $xpath->evaluate('string(./SimplePredicate/@value)', $node);
    
            if (evaluatePredicate($data[$field], $operator, $value)) {
                // Descend recursively.
                return evaluateTree($data, $node, $xpath);
            }
        }
    
        // Reached the end of the line.
        return $tree->getAttribute('id');
    }
    
    function evaluatePredicate($left, $operator, $right)
    {
        switch ($operator) {
            case "lessOrEqual":
                return $left <= $right;
            case "greaterThan":
                return $left > $right;
            default:
                return false;
        }
    }
    

    Output:

    4
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算