使用php实现二叉搜索树
看到一位大神写的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实现二叉搜索树的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [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 ...
- [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 ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [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 ...
- [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. 这道 ...
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- [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 ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
随机推荐
- sscanf 与 sscanf_s
sscanf 与 sscanf_s 之间的Details sscanf sscanf函数想必大家用的很熟练吧 sscanf函数原型: sscanf(const char* src,format,... ...
- Python异常捕捉的一个小问题
问题: names = ['taotao','songwenjing','liu','li']I = iter(names)while True: try: s = next(I) except Ex ...
- mysql数据库变更监控(canal)
背景: 1. 一些项目的基础功能会有Audit Trace, 以记录系统用户所做过的所有记录. 2. 实时备份数据,比如mysql主从复制,一个用于面向应用,一个用于对应用数据库的实时备份. 3. 实 ...
- dev的动态汉化
放控件TcxLocalizer.将其FIlename设定成汉化文件.ini.选择Locale的值是中文,然后active=true.OK了文件如下 ini如下: [2052] CHINA_STR=&q ...
- javaHttp请求,接收到的是中文乱码如何处理
可在service()方法中加日志,看哪种不是乱码 例如,中文乱码的话,中文编码一般有 UTF-8,GBK,ISO-8859-1 加日志为 List<String> list = new ...
- Java面试题大全(javaSe,HTML,CSS,js,Spring框架等)
目录 1. Java基础部分 7 1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 7 2.Java有没有goto? 7 3.说说&和& ...
- ZOJ 3684 Destroy
Destroy Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID: 36 ...
- [luoguP2387] 魔法森林(LCT + 并查集)
传送门 并查集真是一个判断连通的好东西! 连通性用并查集来搞. 把每一条边按照 a 为关键字从小到大排序. 那么直接枚举,动态维护 b 的最小生成树 用 a[i] + 1 ~ n 路径上最大的 b[i ...
- hdu poj KMP简单题目总结
hdu 3336 题意:输入一个字符串求每个前缀在串中出现的次数和 sol:只要稍微理解下next 数组的含义就知道只要把每个有意义的next值得个数加起来即可 PS:网上有dp解法orz,dp[i] ...
- scrapy的User-Agent中间件、代理IP中间件、cookies设置、多个爬虫自定义settings设置
在scrapy的反爬中,常用的几个配置,简单总结了下: User-Agent中间件: from fake_useragent import UserAgent class RandomUserAgen ...