Linked list implementation of Binary Min Heap (Having trouble with manipulation…)

后端 未结 5 640
囚心锁ツ
囚心锁ツ 2021-01-25 21:14

So i\'m trying to implement a binary min heap. I understand what the binary min heap entails in terms of it\'s structure and it\'s properties. However I\'m hitting a wall when I

5条回答
  •  佛祖请我去吃肉
    2021-01-25 22:18

    Use this function to reach desired node:

    function find_node($n)
    {
    $current_node = $n;
    while($current_node > 1)
    {
        if($current_node % 2 == 1) // if node is odd it is a right child
        {
          push($stack,"Right");
        }
        else // otherwise it is even and left child
        {
          push($stack,"Left");
        }
        $current_node = floor($current_node / 2); // set the current node to the parent
    }
    return $stack; // this stack now contains the path to node n
    }
    

提交回复
热议问题