PHP兌現二叉樹,線索二叉樹

來源:互聯網
上載者:User
PHP實現二叉樹,線索二叉樹
createThreadTree();    echo $tree->threadList() . "\n";從第一個結點開始遍曆線索二叉樹    echo $tree->threadListReserv();從最後一個結點開始反向遍曆?>


biTree.phpdata = $data;        }               //我不喜歡使用魔術方法         public function getData(){            return $this->data;        }        public function setData($data){            $this->data = $data;        }        public function getLeft(){            return $this->left;        }        public function setLeft($left){            $this->left = $left;        }        public function getRight(){            return $this->right;        }        public function setRight($right){            $this->right = $right;        }        public function getLTag(){            return $this->lTag;        }        public function setLTag($tag){            $this->lTag = $tag;        }        public function getRTag(){            return $this->rTag;        }        public function setRTag($tag){            $this->rTag = $tag;        }    }    //線索二叉樹類    class BiTree{        private $datas = NULL;//要匯入的字串;        private $root = NULL; //根結點        private $leafCount = 0;//葉子結點個數        private $headNode = NULL; //線索二叉樹的頭結點        private $preNode = NULL;//遍曆線索化二叉樹時儲存前一個遍曆的結點        public function BiTree($datas){            is_array($datas) || $datas = str_split($datas);            $this->datas = $datas;             $this->backupData = $this->datas;             $this->createTree(TRUE);                  }                //前序走訪建立樹        //$root 判斷是不是要建立根結點        public function createTree($root = FALSE){            if(empty($this->datas)) return NULL;                        $first = array_shift($this->datas);            if($first == '#'){                return NULL;            }else{                $node = new Node($first);                $root && $this->root = $node;                                $node->setLeft($this->createTree());                $node->setRight($this->createTree());                return $node;            }        }            //返回二叉樹葉子結點的個數        public function getLeafCount(){            $this->figureLeafCount($this->root);            return $this->leafCount;         }            private function figureLeafCount($node){            if($node == NULL)                return false;            if($this->checkEmpty($node)){                $this->leafCount ++;            }else{                $this->figureLeafCount($node->getLeft());                $this->figureLeafCount($node->getRight());            }        }                 //判斷結點是不是葉子結點        private function checkEmpty($node){            if($node->getLeft() == NULL && $node->getRight() == NULL){                return true;            }            return false;        }        //返回二叉樹深度        public function getDepth(){            return $this->traversDepth($this->root);        }                //遍曆求二叉樹深度        public function traversDepth($node){            if($node == NULL){                return 0;               }            $u = $this->traversDepth($node->getLeft()) + 1;            $v = $this->traversDepth($node->getRight()) + 1;            return $u > $v ? $u : $v;        }             //返回遍曆結果,以字串的形式        //$order 按遍曆形式返回,前中後         public function getList($order = 'front'){            if($this->root == NULL) return NULL;            $nodeList = array();            switch ($order){                case "front":                    $this->frontList($this->root, $nodeList);                    break;                                    case "middle":                    $this->middleList($this->root, $nodeList);                    break;                                case "last":                    $this->lastList($this->root, $nodeList);                    break;                                     default:                    $this->frontList($this->root, $nodeList);                    break;             }                        return implode($nodeList);        }        //建立線索二叉樹        public function createThreadTree(){            $this->headNode = new Node();            $this->preNode = & $this->headNode;            $this->headNode->setLTag(0);            $this->headNode->setLeft($this->root);            $this->headNode->setRTag(1);                        $this->threadTraverse($this->root);            $this->preNode->setRight($this->headNode);            $this->preNode->setRTag(1);            $this->headNode->setRight($this->preNode);        }             //線索化二叉樹        private function threadTraverse($node){            if($node != NULL){                if($node->getLeft() == NULL){                    $node->setLTag(1);                    $node->setLeft($this->preNode);                }else{                    $this->threadTraverse($node->getLeft());                }                                if($this->preNode != $this->headNode && $this->preNode->getRight() == NULL){                    $this->preNode->setRTag(1);                    $this->preNode->setRight($node);                }                                $this->preNode = & $node;//注意傳引用                $this->threadTraverse($node->getRight());            }        }        //從第一個結點遍曆中序線索二叉樹        public function threadList(){            $arr = array();            for($node = $this->getFirstThreadNode($this->root); $node != $this->headNode; $node = $this->getNextNode($node)){                $arr[] = $node->getData();            }            return implode($arr);        }        //從尾結點反向遍曆中序線索二叉樹        public function threadListReserv(){            $arr = array();                        for($node = $this->headNode->getRight(); $node != $this->headNode; $node = $this->getPreNode($node)){                $arr[] = $node->getData();             }            return implode($arr);        }        //返回某個結點的前驅        public function getPreNode($node){            if($node->getLTag() == 1){                return $node->getLeft();            }else{                return $this->getLastThreadNode($node->getLeft());            }        }        //返回某個結點的後繼        public function getNextNode($node){            if($node->getRTag() == 1){                return $node->getRight();            }else{                return $this->getFirstThreadNode($node->getRight());            }        }        //返回中序線索二叉樹的第一個結點        public function getFirstThreadNode($node){            while($node->getLTag() == 0){                $node = $node->getLeft();            }            return $node;        }               //返回中序線索二叉樹的最後一個結點        public function getLastThreadNode($node){            while($node->getRTag() == 0){                $node = $node->getRight();             }            return $node;        }               //前序走訪        private function frontList($node, & $nodeList){            if($node !== NULL){                $nodeList[] = $node->getData();                $this->frontList($node->getLeft(), $nodeList);                $this->frontList($node->getRight(), $nodeList);            }        }        //中序遍曆        private function middleList($node, & $nodeList){            if($node != NULL){                $this->middleList($node->getLeft(), $nodeList);                $nodeList[] = $node->getData();                $this->middleList($node->getRight(), $nodeList);            }        }                //後序遍曆        private function lastList($node, & $nodeList){            if($node != NULL){                $this->lastList($node->getLeft(), $nodeList);                $this->lastList($node->getRight(), $nodeList);                $nodeList[] = $node->getData();            }        }        public function getData(){            return $this->data;        }        public function getRoot(){            return $this->root;        }    }?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.