看到一位大神写的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. 基于springmvc、ajax,后台连接数据库的增删改查

    前言 前段时间在博客园上找了一个springmvc的例子,照着学了一下,算是对springmvc有了一个初步的了解,打一个基础,下面是链接.(我只看了博客,视频太耗时间了) 博客链接:http://w ...

  2. Java 中 break和 continue 的使用方法及区别

    break break可用于循环和switch...case...语句中. 用于switch...case中: 执行完满足case条件的内容内后结束switch,不执行下面的语句. eg: publi ...

  3. 【OpenCV, MFC】利用MFC和OpenCV通过系统对话框打开和保存图片

    打开图片: void CImageProDlg::OnImageopen() { // TODO: 在此添加命令处理程序代码 Invalidate(); CFileDialog dlg(TRUE, N ...

  4. ViewService

    ViewService 在分布式系统中,最常见的场景就是主备架构.但是如果主机不幸宕机,如何正确的通知客户端当前后端服务器的状况成为一个值得研究的问题.本文描述了一种简单的模型用于解决此问题. 背景 ...

  5. [K/3Cloud]DBServiceHelper.ExecuteDataSet(this.Context, sql)) 返回数据问题

    例如下面代码: int sQty = 0; string sql = string.Format(@" Select FMATERIALID ,FBASEUNITID ,FAUXPROPID ...

  6. java虚拟机(一)-java内存区域与内存溢出异常

    1.简述:java虚拟机在执行java程序的过程中,会把他所管理的内存分为以下几个区域, 1.1.程序计数器 1.2.虚拟机栈 1.3.本地方法栈 1.4.java堆 1.5.方法区 如图所示: 2. ...

  7. R - Milking Time DP

    Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...

  8. poj——1330 Nearest Common Ancestors

    Nearest Common Ancestors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30082   Accept ...

  9. Abstract factory抽象工厂--对象创建型

    意图: 提供一个创建一系列相关或者相互依赖对象的接口,而无需指定它们具体的类. 别名:Kit 补充: 抽象产品A : (产品A1 和产品 A2) 抽象产品B : ( 产品B1 和 产品B2) 一般情况 ...

  10. Bootstrap基础教程:tutorialspoint-bootstrap

    来自turorialspoint的Boostrap基础教程(英文),官网:https://www.tutorialspoint.com/bootstrap/index.htm 中文版:https:// ...