看到一位大神写的js的搜索树,自己也按照模式写了一个php的二叉搜索树。

<?php
class node{
public $data;
public $key;
public $left=null;
public $right=null;
function __construct($data=null,$key=null)
{
$this->data=$data;
$this->key=$key;
}
}
class binarysearchtree{
public $root=null;
function insert($data){
$newnode=new node($data);
if ($this->root==null) {
$this->root=$newnode;
return 1;
}
$currentnode=$this->root;
$parentnode=null;
while (true) {
$parentnode = $currentnode;
if ($data < $currentnode->data) {
//当前节点的值 > 目标节点的值
//应该向左插,工作节点移到左节点
$currentnode = $currentnode->left;
if ($currentnode == null) {
//没有左节点,则新节点,直接成为左节点
$parentnode->left = $newnode;
// echo "zuo".$newnode->data;
return 1; //退出循环
}
}
else {
//否则向右插,工作节点移到右节点
$currentnode = $currentnode->right;
if ($currentnode == null) {

$parentnode->right = $newnode;
// echo "you".$parentnode->right->data;
return 1;
}
}
}
}
function maxs() //最大值
{
$p = $this->root; //工作节点
while ($p != null && $p->right != null) {
$p = $p->right;
}
return $p;
}
function mins() //最小值
{
$p = $this->root; //工作节点
while ($p != null && $p->left != null) {
$p = $p->left;
}
return $p;
}
//中序遍历
function inorder($rootnode){
if ($rootnode != null) {
$this->inorder($rootnode->left); //先左节点
print($rootnode->data); //再根节点
$this->inorder($rootnode->right); //再右节点
}
}
function toorder($rootnode){
if ($rootnode != null) {
$this->toorder($rootnode->right); //先左节点
print($rootnode->data); //再根节点
$this->toorder($rootnode->left); //再右节点
}
}
function preorder($rootnode){
if ($rootnode != null) {
print($rootnode->data); //先根
$this->preorder($rootnode->left); //再左节点
$this->preorder($rootnode->right); //再右节点
}
}
function postorder($rootnode){
if ($rootnode != null) {
$this->postorder($rootnode->left); //先左节点
$this->postorder($rootnode->right); //再右节点
print($rootnode->data); //再根节点
}
}

}
header("Content-type: text/html; charset=utf-8");
$btree = new binarysearchtree();

$btree->insert(6);
$btree->insert(3);
$btree->insert(8);
$btree->insert(1);
$btree->insert(4);
$btree->insert(9);
print('中序遍历:');
$btree->inorder($btree->root);
print("<br/>");
print('中序后遍历:');

$btree->toorder($btree->root);
print("<br/>");
print("先序遍历:");
$btree->preorder($btree->root);

print("<br/>");

print("后序遍历:");
$btree->postorder($btree->root);

print("<br/>");
$minnode = $btree->mins();
print("最小节点:".($minnode == null ? "不存在" : $minnode->data));

print("<br/>");
$maxnode = $btree->maxs();
print("最大节点:".($maxnode == null ? "不存在" : $maxnode->data));

?>

注:十万个数排序需要23秒

使用php实现二叉搜索树的更多相关文章

  1. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  2. [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  3. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  4. [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  5. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  6. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  7. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  8. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  10. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

随机推荐

  1. Oracle 常用目录结构(10g)

    大多数情况下,了解Oracle常用目录结构,将大大提高数据库管理与维护的工作效率,本文介绍了Oracle 10g 的常用目录. OFA: 下面给出Oracle 最优灵活体系结构OFA(Optimal ...

  2. dock helloworld

    Docker Hello World Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序. 输出Hello world runoob@runoob: ...

  3. reshape column vector data in Matlab

    input: import  data 2. transpose the data 3. reshape the data into array code: matlab load x.dat X=x ...

  4. orcad中注意的事情

    1.地的标识不能放到已经分配了网络的线上. 在用orcad画原理图的时候,把电源放到网络的时候需要特别的注意,如果,将电源地直接放到线上,而这根线又已经被分配了网络标号,那这个地会随已经分配了的网络号 ...

  5. 【转】sizeof()用法总结

    传送门:https://blog.csdn.net/u011677209/article/details/52837065

  6. hdu 3804树链剖分+离线操作

    /* 树链刨分+离线操作 题意:给你一棵树,和询问x,y 从节点x--节点1的小于等于y的最大值. 解:先建一个空树,将树的边权值从小到大排序,将询问y按从小到大排序 对于每次询问y将小于等于y的边权 ...

  7. Mysql中Group By使用Having语句配合查询(where和having区别)

    注意 : having语句一般结合GROUP BY一起使用的..... Having短语与WHERE的区别!!! WHERE子句作用于基表或视图,从中选择满足条件的元组.HAVING短语作用于组,从中 ...

  8. JAVA NIO 之 Selector 组件

    NIO 重要功能就是实现多路复用.Selector是SelectableChannel对象的多路复用器.一些基础知识: 选择器(Selector):选择器类管理着一个被注册的通道集合的信息和它们的就绪 ...

  9. java中static学习总结

    <<java编程思想>>: 1.static方法就是没有this的方法. 2.在static方法内部非静态方法. 3.在没有创建对象的前提下,可以通过类本身来调用static修 ...

  10. 24、Java并发性和多线程-信号量

    以下内容转自http://ifeve.com/semaphore/: Semaphore(信号量) 是一个线程同步结构,用于在线程间传递信号,以避免出现信号丢失(译者注:下文会具体介绍),或者像锁一样 ...