使用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 ...
随机推荐
- MFC 程序 手写创建顺序
MFC 程序 手写创建顺序 1.继承CWinApp类 覆盖 class CMyApp : public CWinApp { virtual BOOL InitInstance(); } BOOL CM ...
- Java对象序列化为什么要使用SerialversionUID
1.首先谈谈为什么要序列化对象 把对象转换为字节序列的过程称为对象的序列化. 把字节序列恢复为对象的过程称为对象的反序列化. 对象的序列化主要有两种用途: 1) 把对象的字节序列永久地保存到硬盘上,通 ...
- centos7在grub界面下更改root密码
想要更改root的密码或者忘记了root的密码的时候可以在grub界面下更改root的密码. 百度了很多内容,更多方法都是适用于centos6及以前版本的,终于找到一个可以的. 1.开机后,在下图界面 ...
- 【Codeforces 522B】Photo to Remember
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 模拟题.用set模拟下就好 [代码] import java.io.*; import java.util.*; public class M ...
- RabbitMQ整合spring----https://www.cnblogs.com/woms/p/7040902.html
https://www.cnblogs.com/woms/p/7040902.html
- ModelMap org.springframework.ui.ModelMap
ModelMap实现了map接口,可以在其中存放属性,作用域同request,同时可与@SessionAttributes联合使用,把数据放入到session中去, 下面这个示例,我们可以在Model ...
- [luoguP1029] 最大公约数和最小公倍数问题(数论)
传送门 一.暴力枚举(加了点优化) #include <cstdio> int x, y, ans; inline int gcd(int x, int y) { return !y ? ...
- CodeForces 362C
分析:首先我们要知道调用swap()函数的次数跟什么有关.可以观察发现在Insertion Sort里,当且仅当a[j](j∈[0,i)) > a[i]时会调用一次swap(),也就是说有多少个 ...
- php处理管道文件流
<?php #!/usr/local/bin/php -q function read(){ $fp = fopen("php://stdin", "r" ...
- Ubuntu桌面卡死时的处理
1.这种方式可以尝试,但是不成功 sudo skill x sudo stop lightdm sudo start lightdm 2.这种方式比较可靠 ps -t tty7 kill 27342 ...