2010-09-13 122 views
4

我需要在PHP中實現「完美二叉樹」。PHP二叉樹實現

目前,我有這樣的:

<?php 
    $teams = 8; 
    $num_rounds = round(log($teams, 2)) + 1; 

    for ($i = 0; $i < $num_rounds; ++$i) 
    { 
    $matches = $teams * pow(.5, $i - 1)/2; 
    for ($j = 0; $j < $matches; ++$j) 
    { 
    echo "<div style=\"border-style: inset;\"><span>Round $i Match $j</span></div>\n"; 
    } 
    } 
?> 

您可以查看它here。我使用Frank Mich jQuery Binary Tree插件來顯示數據,但正如我之前所說,我相信我需要二叉樹才能正確顯示它。

如果有更好的方法,或者我只是做錯了嗎?解決方案是什麼?

+0

括號內所有在這裏顯示。 – zneak 2010-09-13 04:31:23

+0

也許我應該改寫一下,即使我按順序遍歷它們,標籤也是不正確的。支架顯示本身很棒。 – Zack 2010-09-13 04:33:52

+0

預期的輸出將是每列按照0 1 2 3的順序排列,並且每列中按順序顯示匹配。 – Zack 2010-09-13 05:05:03

回答

0

Frank Mich Binary Tree page,看來你的樹條目會顯示如下:

0 
    \ 
    1 
/ \ 
2  \ 
     \ 
      3 
     / \ 
4 / \ 
    \ /  \ 
    5   \ 
/   \ 
6    \ 
        \ 
        7 
       /
8    /
    \   /
    9  /
/ \  /
10  \ /
     \ /
      11 
     /
12 /
    \ /
    13 
/
14 

凡在樹中的每個數字代表其輸入數組中條目的索引。請注意,倒數第一個圓柱,索引增加2.在第二列,他們增加了4,並在第三列8.

我會創建一個字符串數組的名稱爲每場比賽都在其中。然後做這樣的事情:

num_rounds = x 
num_games = (2^num_rounds) - 1 
game_names = array(num_games) 
for round = 0 to num_rounds - 1 
    index = (2^round) - 1 
    increment = 2^(round + 1) 
    game_number = 0 
    while index < num_games 
     game_names[index] = sprintf("round %s, game %s", round, game_number) 
     game_number++ 
     index += increment 
display_tree(game_names) 
1

這是在PHP執行二進制樹(數據結構)的代碼:

<?php 
class Node 
{ 
    public $data; 
    public $leftChild; 
    public $rightChild; 

    public function __construct($data) 
    { 
     $this->data = $data; 
     $this->leftChild = null; 
     $this->rightChild = null; 
    } 

    public function disp_data() 
    { 
     echo $this->data; 
    } 
} 

class BinaryTree 
{ 
    public $root; 

    public function __construct() 
    { 
     $this->root = null; 
    } 

    /** 
    * function to display the tree 
    */ 
    public function display() 
    { 
     $this->display_tree($this->root); 
    } 

    public function display_tree($local_root) 
    { 
     if ($local_root == null) { 
      return; 
     } 
     $this->display_tree($local_root->leftChild); 
     echo $local_root->data."<br/>"; 
     $this->display_tree($local_root->rightChild); 
    } 

    /** 
    * function to insert a new node 
    */ 
    public function insert($key) 
    { 
     $newnode = new Node($key); 
     if ($this->root == null) { 
      $this->root = $newnode; 

      return; 
     } else { 
      $parent = $this->root; 
      $current = $this->root; 
      while (true) { 
       $parent = $current; 
       if ($key == ($this->find_order($key, $current->data))) { 
        $current = $current->leftChild; 
        if ($current == null) { 
         $parent->leftChild = $newnode; 

         return; 
        }//end if2 
       } else { 
        $current = $current->rightChild; 
        if ($current == null) { 
         $parent->rightChild = $newnode; 

         return; 
        } 
       } 
      } 
     } 
    } 

    /** 
    * function to search a particular Node 
    */ 
    public function find($key) 
    { 
     $current = $this->root; 
     while ($current->data != $key) { 
      if ($key == $this->find_order($key, $current->data)) { 
       $current = $current->leftChild; 
      } else { 
       $current = $current->rightChild; 
      } 
      if ($current == null) { 
       return (null); 
      } 
     } 

     return ($current->data); 
    } 

    public function delete1($key) 
    { 
     $current = $this->root; 
     $parent = $this->root; 

     $isLeftChild = true; 
     while ($current->data != $key) { 
      $parent = $current; 
      if ($key == ($this->find_order($key, $current->data))) { 
       $current = $current->leftChild; 
       $isLeftChild = true; 
      } else { 
       $current = $current->rightChild; 
       $isLeftChild = false; 
      } 
      if ($current == null) { 
       return (null); 
      } 
     } 

     echo "<br/><br/>Node to delete:".$current->data; 
     //to delete a leaf node 
     if ($current->leftChild == null && $current->rightChild == null) { 
      if ($current == $this->root) { 
       $this->root = null; 
      } else { 
       if ($isLeftChild == true) { 
        $parent->leftChild = null; 
       } else { 
        $parent->rightChild = null; 
       } 
      } 

      return ($current); 
     } else { //to delete a node having a leftChild 
      if ($current->rightChild == null) { 
       if ($current == $this->root) { 
        $this->root = $current->leftChild; 
       } else { 
        if ($isLeftChild == true) { 
         $parent->leftChild = $current->leftChild; 
        } else { 
         $parent->rightChild = $current->leftChild; 
        } 
       } 

       return ($current); 
      } else { //to delete a node having a rightChild 
       if ($current->leftChild == null) { 
        if ($current == $this->root) { 
         $this->root = $current->rightChild; 
        } else { 
         if ($isLeftChild == true) { 
          $parent->leftChild = $current->rightChild; 
         } else { 
          $parent->rightChild = $current->rightChild; 
         } 
        } 

        return ($current); 
       } else { //to delete a node having both childs 
        $successor = $this->get_successor($current); 
        if ($current == $this->root) { 
         $this->root = $successor; 
        } else { 
         if ($isLeftChild == true) { 
          $parent->leftChild = $successor; 
         } else { 
          $parent->rightChild = $successor; 
         } 
        } 
        $successor->leftChild = $current->leftChild; 

        return ($current); 
       } 
      } 
     } 
    } 

    /** 
    * Function to find the successor node 
    */ 
    public function get_successor($delNode) 
    { 
     $succParent = $delNode; 
     $successor = $delNode; 
     $temp = $delNode->rightChild; 
     while ($temp != null) { 
      $succParent = $successor; 
      $successor = $temp; 
      $temp = $temp->leftChild; 
     } 

     if ($successor != $delNode->rightChild) { 
      $succParent->leftChild = $successor->rightChild; 
      $successor->rightChild = $delNode->rightChild; 
     } 

     return ($successor); 
    } 

    /** 
    * function to find the order of two strings 
    */ 
    public function find_order($str1, $str2) 
    { 
     $str1 = strtolower($str1); 
     $str2 = strtolower($str2); 
     $i = 0; 
     $j = 0; 

     $p1 = $str1[$i]; 
     $p2 = $str2[$j]; 
     while (true) { 
      if (ord($p1) < ord($p2) || ($p1 == '' && $p2 == '')) { 
       return ($str1); 
      } else { 
       if (ord($p1) == ord($p2)) { 
        $p1 = $str1[++$i]; 
        $p2 = $str2[++$j]; 
        continue; 
       } 

       return ($str2); 
      } 
     } 
    } 

    public function is_empty() 
    { 
     return $this->root === null; 
    } 
} 
+0

您需要了解如何在編輯器中使用格式化工具,以便您的代碼可以正確顯示。看[這裏](http://stackoverflow.com/editing-help)。 – BoltClock 2010-12-05 09:48:00