php-数据结构-BST二叉搜索树的创建遍历

<?php

class Node {
    public $key;
    public $left;
    public $right;

    public function __construct($key) {
        $this->key    = $key;
        $this->left   = null;
        $this->right  = null;
    }
}

class Bst {
    public $root;

    /**
     * 初始化树结构
     *
     * @param $arr 初始化树结构的数组
     *
     * @return null
     */
    public function init($arr) {
        $this->root = new Node($arr[0]);
        for($i = 1; $i < count($arr); $i++) {
            $ret = $this->Insert($arr[$i]);
        }
        return $ret;
    }

    /**
     * 将$key插入树中
     *
     * @param $key 待插入树的数字
     *
     * @return null
     */
    function Insert($key) {
        if(!is_null($this->search($key))){
            throw new  Exception();
        }
        $root = $this->root;
        $innode = new Node($key);
        $current = $root;
        $preNode = null;

        //先找到适合key插入的节点
        while($current !=null){
            $preNode = $current;
            if($current->key>$innode->key){
                $current = $current->left;
            }else{
                $current = $current->right;
            }
        }
        //将key 插入到找出的节点左右
        if($preNode == null){
            $this->root = $innode;
        }else{
            if($preNode->key > $innode->key){
                $preNode->left = $innode;
            }else{
                $preNode->right = $innode;
            }
        }
        return $root;
    }
    /**
     * 前序遍历二叉树
     * @param $node
     * User: huanghao@zuoyebang.com
     * Date: 2019/3/7
     * Time: 11:59
     */
    function preTransTree($node,$string){

        if(!is_null($node->left)){
            $string = $this->preTransTree($node->left,$string);
        }
        if(!is_null($node->key)){
            $string .=$node->key."->";
        }
        if(!is_null($node->right)){
            $string = $this->preTransTree($node->right,$string);
        }

        return $string;
    }
    /**
     * 中序遍历二叉树
     * @param $node
     * User: huanghao@zuoyebang.com
     * Date: 2019/3/7
     * Time: 11:59
     */
    function midTransTree($node,$string){
        if(!is_null($node->key)){
            $string .=$node->key."->";
        }
        if(!is_null($node->left)){
            $string = $this->midTransTree($node->left,$string);
        }
        if(!is_null($node->right)){
            $string = $this->midTransTree($node->right,$string);
        }

        return $string;
    }
    /**
     * 后序遍历二叉树
     * @param $node
     * User: huanghao@zuoyebang.com
     * Date: 2019/3/7
     * Time: 11:59
     */
    function latTransTree($node,$string){

        if(!is_null($node->left)){
            $string = $this->latTransTree($node->left,$string);
        }
        if(!is_null($node->right)){
            $string = $this->latTransTree($node->right,$string);
        }
        if(!is_null($node->key)){
            $string .=$node->key."->";
        }

        return $string;
    }


    /**
     * 查找树中是否存在$key对应的节点
     *
     * @param $key 待搜索数字
     *
     * @return $key对应的节点
     */
    function search($key) {
        $current = $this->root;
        while($current != null) {
            if($current->key == $key) {
                return $current;
            } elseif($current->key > $key) {
                $current = $current->left;
            } else {
                $current = $current->right;
            }
        }

        return $current;
    }

}
$arr = array(5, 3, 8, 2, 4, 7, 9, 1, 6);
$obj = new Bst();
$ret = $obj->init($arr);
$string = "前序".$obj->preTransTree($obj->root,"");
var_dump($string);
$string = "中序".$obj->midTransTree($obj->root,"");
var_dump($string);
$string = "后序".$obj->latTransTree($obj->root,"");
var_dump($string);
//

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值