很多站长朋友们都不太清楚phpnoed树结构,今天小编就来给大家整理phpnoed树结构,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 Zookeeper(一)可以用来干什么? 2、 PHP+Dtree实现动态读取三级树 3、 请教高手:php实现n叉树遍历 4、 麻烦各位高手大虾们 给小弟一个 php 简单的树形菜单 代码! 要注释。。非常感谢!!!!!! 5、 PHP版本二叉树按层 从上到下左到右完全二叉树 Zookeeper(一)可以用来干什么?当我们在学习一项新技术的时候,首先应了解的就是它是用来干什么的。下面一段话是来自Zookeeper官网的一段解释:
ZooKeeper用于为分布式应用程序提供分布式并且开源的协调服务。它公开了一组简单的原语,分布式应用程序可以基于这些原语来实现用于同步,配置维护以及组和命名的更高级别的服务。
对于不了解Zookeeper的初学者来说,看完是不是很懵逼?所以本文将从程序员的角色出发,引导大家快速的去了解Zookeeper能够用来干什么。以此作为学习Zookeeper的入门,希望能够帮助到大家。
操作基于node组成的树形模型的方法很简单,无非就是创建node,删除node,监听node等简单的操作。
在介绍Zookeeper的主要用途前,我们先按不同维度将node对象划分为几种类型。
综上,node可分为四大类。持久无序、持久有序、临时无序、临时有序。
到此,我们不妨思考一下,基于node的树形结构以及四种不同的分类,可以实现什么用途呢?
思考倒计时 10......
思考倒计时 9........
思考倒计时 8........
思考倒计时 0........
有没有跟笔者一样,一个都没想出来的同学?没关系,接下来就简明说说Zookeeper到底可以用来干什么?
获得锁时,无非就是多个客户端,争着抢着去创建同一个node节点,谁第一个创建成功,谁就获得锁。
释放时,抢到锁的客户端就去删除node,然后通知其他需要创建锁的客户端,去再次争抢创建node节点。
正如我们之前介绍的一共有四种类型的node,那么创建时我们该选择哪种类型呢?
如果选择持久node,那当持有该node锁的客户端出现网络故障后,会产生什么问题呢?客户端是不是就无法主动删除node,导致锁无法正常释放了!所以我们应选用临时node,这样即时故障后,node锁也会随着会话结束而删除。
至于有序还是无序的node,则需要根据不同类型的锁来选择。例如:实现非公平独占锁,我们就选择无序;实现公平锁或共享锁,我们就选择有序。实现原理很简单,小伙伴们可以自己稍作思考,本文不再细说。
Master的选举过程其实类似node锁的创建,删除与监听。
临时node由客户端A创建,所以客户端A所在服务器当选为master。
前面介绍过,node组成了树形结构。类似操作系统中的文件路径,树形分支上的node名称拼接成的全路径,它必定是唯一的。因此我们可以使用node的全路径作为命名方式。
本文作为Zookeeper的入门篇,主要从Node对象的角度讲解了Zookeeper可以用来干么。当然Node的设计还有更多可具体深入的地方。Zookeeper也不仅只有Node,还有更多值得我们学习的知识,例如:Watcher机制,如何保障事务的顺序一致性、集群间数据的一致性,以及必须要知道的ZAB协议等等。
最后祝大家看完此文后能够有所收获!
PHP+Dtree实现动态读取三级树代码乱七八糟的!
给你一个我写的递归,虽然效率有点低,但是效果还是不错可以查询所以的子集
<?php
/**
* 使用递归查询 生成树结构
*
* @author Li
* @version
*/
include_once("ConnectDB.php");//导入数据库连接类,[我自己的数据库连接类]
function getTreeInfo($pid)
{
$db = new ConnectDB();
if ($pid=="")
{
$sql = "SELECT nodeid,pid,nodename FROM mytree where pid is null";//nodeid 节点ID pid 父节点ID nodename 节点名称
}
else
{
$sql = ' SELECT nodeid,pid,nodename FROM mytree where pid = \''.$pid.'\'';
}
return $db->findAll($sql);
}
function createTree($pid)
{
$uls = "<ul>";
$arr = getTreeInfo($pid);
if (is_array($arr))
{
for ($i = 0 ; $i < count($arr) ; $i++ )
{
$uls.= "<li>".$arr[$i]['nodename'];
$uls.= createTree($arr[$i]['nodeid']);
$uls.= "</li>";
}
}
$uls .= "</ul>";
return $uls;
}
echo createTree("");
?>
请教高手:php实现n叉树遍历要构建的无限分类的模型. 电子产品是最大的分类.家用电器 ,数码产品是其子分类.可以看到子分类是被父分类包含起来的.每个分类都有左右 两个节点编号分别是1、2、3.....
根据上面的图mysql中建立表和插入数据
CREATE TABLE `product_categories` (
`id` MEDIUMINT( 8 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,`name` VARCHAR( 20 ) NOT NULL ,
`left_node` MEDIUMINT( 8 ) NOT NULL ,
`right_node` MEDIUMINT( 8 ) NOT NULL
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;INSERT INTO `product_categories` (`id`, `name`, `left_node`, `right_node`) VALUES(1, '电子产品', 1, 20),
(2, '家用电器', 2, 9),
(3, '电视机', 3, 4),
(4, '电冰箱', 5, 6),
(5, '空调', 7, 8),
(6, '数码产品', 10, 19),
(7, '电脑', 11, 18),
(8, '台式电脑', 12, 13),
(9, '笔记本电脑', 14, 15),
(10, '平板电脑', 16, 17);
表结构如下:
下面是PHP的实例代码:
1、获取所有节点
<?php
$pdo = new PDO(
'mysql:host=localhost;dbname=test',
'root',
''
);
$pdo->exec("SET NAMES UTF8");
$stmt = $pdo->prepare("SELECT c.name FROM product_categories as c, product_categories as pWHERE c.left_node BETWEEN p.left_node AND p.right_nodeAND p.name='电子产品' ORDER BY c.left_node");$stmt->execute();
$rs=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rs as $v){
echo $v['name'].'<br />';
}
输出:
电子产品
家用电器
电视机
电冰箱
空调
数码产品
电脑
台式电脑
笔记本电脑
平板电脑
2、 获取某个父节点以及其所有子节点
<?php
$pdo = new PDO(
'mysql:host=localhost;dbname=test',
'root',
''
);
$pdo->exec("SET NAMES UTF8");
$stmt = $pdo->prepare("SELECT c.name FROM product_categories as c, product_categories as pWHERE c.left_node BETWEEN p.left_node AND p.right_nodeAND p.name='数码产品' ORDER BY c.left_node");$stmt->execute();
$rs=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rs as $v){
echo $v['name'].'<br />';
}
输出:
数码产品
电脑
台式电脑
笔记本电脑
平板电脑
3、获取所有的叶子节点
<?php
$pdo = new PDO(
'mysql:host=localhost;dbname=test',
'root',
''
);
$pdo->exec("SET NAMES UTF8");
$stmt = $pdo->prepare("SELECT name FROM product_categories where right_node-left_node=1");$stmt->execute();
$rs=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rs as $v){
echo $v['name'].'<br />';
}
输出:
电视机
电冰箱
空调
台式电脑
笔记本电脑
平板电脑
4、获取某个子节点及其所有父节点
<?php
$pdo = new PDO(
'mysql:host=localhost;dbname=test',
'root',
''
);
$pdo->exec("SET NAMES UTF8");
$stmt = $pdo->prepare("SELECT p.name FROM product_categories AS c, product_categories AS p WHERE c.left_node BETWEEN p.left_node AND p.right_node AND c.name = '平板电脑' ORDER BY p.left_node");$stmt->execute();
$rs=$stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rs as $v){
echo $v['name'].'<br />';
}
输出:
电子产品
数码产品
电脑
平板电脑
5、获取所有节点极其所处的层级
<?php
$pdo = new PDO(
'mysql:host=localhost;dbname=test',
'root',
''
);
$pdo->exec("SET NAMES UTF8");
$stmt = $pdo->prepare("SELECT c.name, (COUNT(p.name) - 1) AS level FROM product_categories AS c, product_categories AS p WHERE c.left_node BETWEEN p.left_node AND p.right_node GROUP BY c.name ORDER BY c.left_node");$stmt->execute();
$rs=$stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($rs);
echo '<br />';
foreach($rs as $v){
echo $v['name'].' level:'.$v['level'].'<br />';}
输出:
电子产品 level:0
家用电器 level:1
电视机 level:2
电冰箱 level:2
空调 level:2
数码产品 level:2
电脑 level:2
台式电脑 level:3
笔记本电脑 level:3
平板电脑 level:3
6、获取某个节点的层级
<?php
$pdo = new PDO(
'mysql:host=localhost;dbname=test',
'root',
''
);
$pdo->exec("SET NAMES UTF8");
$stmt = $pdo->prepare("SELECT c.name, (COUNT(p.name) - 1) AS level FROM product_categories AS c, product_categories AS p WHERE c.left_node BETWEEN p.left_node AND p.right_node and c.name='平板电脑' GROUP BY c.name ORDER BY c.left_node");$stmt->execute();
$rs=$stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($rs);
echo '<br />';
foreach($rs as $v){
echo $v['name'].' level:'.$v['level'].'<br />';}
输出:
平板电脑 level:3
7、在某个节点后平行的插入一个节点
<?php
$pdo = new PDO(
'mysql:host=localhost;dbname=test',
'root',
''
);
$pdo->exec("SET NAMES UTF8");
function addNode($left_node,$new_node){
global $pdo;
$stmt = $pdo->prepare("SELECT right_node FROM product_categories WHERE name = '$left_node'");$stmt->execute();
$rs=$stmt->fetch(PDO::FETCH_ASSOC);
$right_node=$rs['right_node'];
$pdo->exec("UPDATE product_categories SET right_node = right_node + 2 WHERE right_node > $right_node");$pdo->exec("UPDATE product_categories SET left_node = left_node + 2 WHERE left_node > $right_node");$pdo->exec("INSERT INTO product_categories(name, left_node, right_node) VALUES('$new_node', $right_node + 1, $right_node + 2)");}
addNode('家用电器','办公用品');
完成之后表结构如下:
8、删除某个节点及其所有子节点
<?php
$pdo = new PDO(
'mysql:host=localhost;dbname=test',
'root',
''
);
$pdo->exec("SET NAMES UTF8");
function deleteNode($node_name){
global $pdo;
$stmt = $pdo->prepare("SELECT left_node,right_node, right_node - left_node + 1 as width FROM product_categories WHERE name ='$node_name'");$stmt->execute();
$rs=$stmt->fetch(PDO::FETCH_ASSOC);
$left_node=$rs['left_node'];
$right_node=$rs['right_node'];
$width=$rs['width'];
$pdo->exec("DELETE FROM product_categories WHERE left_node BETWEEN $left_node AND $right_node");$pdo->exec("UPDATE product_categories SET right_node = right_node - $width WHERE right_node > $right_node");$pdo->exec("UPDATE product_categories SET left_node = left_node - $width WHERE left_node > $right_node");}
deleteNode('数码产品');
完成之后表结构如下:
可以看到用多叉树的方式构建无限分类,查询的时候是非常简便的.但是在插入新的节点和删除节点时就比较麻烦了.
麻烦各位高手大虾们 给小弟一个 php 简单的树形菜单 代码! 要注释。。非常感谢!!!!!!要看效果,你加Q:573780643
1.jquery支持
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
$(document).ready(function() {
$('#tree').lightTreeview({
collapse: true,
line: true,
nodeEvent: true,
unique: true,
style: 'black',
animate: 400
});
});
</script>
2.树菜单函数
<?
function getCategoryTree($TID, $iTable,$url,$urlPar="1=1") {
if ($TID == 0) {
$treeStr = $treeStr."<ul id=\"tree\" class=\"lightTreeview treeview-black\">";
}
$sql = "select * from `$iTable` where TID1 = ".$TID." order by orderid";
$rs = mysql_query($sql);
while ($ary = mysql_fetch_array($rs)) {
$treeStr = $treeStr."<li>";
$rs2 = mysql_query("select count(*) from `$iTable` where TID1 = ".$ary["id"]);
$Vcount = @mysql_result($rs2, 0);
if ($Vcount != 0) {
$treeStr = $treeStr."<div class=\"treeview-folder\">".$ary["title"]."</div><ul style=\"display:none\">";
$treeStr = $treeStr.getCategoryTree($ary["id"], $iTable,$url,$urlPar);
$treeStr = $treeStr."</ul>";
} else {
$treeStr = $treeStr."<div class=\"treeview-file\"><a href=\"".$url."?T=".$ary["id"]."".$urlPar."\" target=\"pro\">".$ary["title"]."</a></div>";
}
$treeStr = $treeStr."</li>";
}@mysql_free_result($rs);
if ($TID == 0) {
$treeStr = $treeStr."</ul>";
}
return $treeStr;
}
?>
3.页面上调用
<div>
<?=getCategoryTree(0,"N_type","edit.php","1=1")?>
</div>
4.数据结构
id title TID1(上一级ID)
PHP版本二叉树按层 从上到下左到右完全二叉树<?php
/** * 二叉树的定义 */
class BinaryTree {
protected $key = NULL; // 当前节点的值
protected $left = NULL; // 左子树
protected $right = NULL; // 右子树
/** * 以指定的值构造二叉树,并指定左右子树 *
* @param mixed $key 节点的值.
* @param mixed $left 左子树节点.
* @param mixed $right 右子树节点.
*/
public function __construct( $key = NULL, $left = NULL, $right = NULL) {
$this->key = $key;
if ($key === NULL) {
$this->left = NULL;
$this->right = NULL;
}
elseif ($left === NULL) {
$this->left = new BinaryTree();
$this->right = new BinaryTree();
}
else {
$this->left = $left;
$this->right = $right;
}
}
/**
* 析构方法.
*/
public function __destruct() {
$this->key = NULL;
$this->left = NULL;
$this->right = NULL;
}
/**
* 清空二叉树.
**/
public function purge () {
$this->key = NULL;
$this->left = NULL;
$this->right = NULL;
}
/**
* 测试当前节点是否是叶节点.
*
* @return boolean 如果节点非空并且有两个空的子树时为真,否则为假.
*/
public function isLeaf() {
return !$this->isEmpty()
$this->left->isEmpty()
$this->right->isEmpty();
}
/**
* 测试节点是否为空
*
* @return boolean 如果节点为空返回真,否则为假.
*/
public function isEmpty() {
return $this->key === NULL;
}
/**
* Key getter.
*
* @return mixed 节点的值.
*/
public function getKey() {
if ($this->isEmpty()) {
return false;
}
return $this->key;
}
/**
* 给节点指定Key值,节点必须为空
*
* @param mixed $object 添加的Key值.
*/
public function attachKey($obj) {
if (!$this->isEmpty())
return false;
$this->key = $obj;
$this->left = new BinaryTree();
$this->right = new BinaryTree();
}
/**
* 删除key值,使得节点为空.
*/
public function detachKey() {
if (!$this->isLeaf())
return false;
$result = $this->key;
$this->key = NULL;
$this->left = NULL;
$this->right = NULL;
return $result;
}
/**
* 返回左子树
*
* @return object BinaryTree 当前节点的左子树.
*/
public function getLeft() {
if ($this->isEmpty())
return false;
return $this->left;
}
/**
* 给当前结点添加左子树
*
* @param object BinaryTree $t 给当前节点添加的子树.
*/
public function attachLeft(BinaryTree $t) {
if ($this->isEmpty() || !$this->left->isEmpty())
return false;
$this->left = $t;
}
/**
* 删除左子树
*
* @return object BinaryTree 返回删除的左子树.
*/
public function detachLeft() {
if ($this->isEmpty())
return false;
$result = $this->left;
$this->left = new BinaryTree();
return $result;
}
/**
* 返回当前节点的右子树
*
* @return object BinaryTree 当前节点的右子树.
*/
public function getRight() {
if ($this->isEmpty())
return false;
return $this->right;
}
/**
* 给当前节点添加右子树
*
* @param object BinaryTree $t 需要添加的右子树.
*/
public function attachRight(BinaryTree $t) {
if ($this->isEmpty() || !$this->right->isEmpty())
return false;
$this->right = $t;
}
/**
* 删除右子树,并返回此右子树
* @return object BinaryTree 删除的右子树.
*/
public function detachRight() {
if ($this->isEmpty ())
return false;
$result = $this->right;
$this->right = new BinaryTree();
return $result;
}
/**
* 先序遍历
*/
public function preorderTraversal() {
if ($this->isEmpty()) {
return ;
}
echo ' ', $this->getKey();
$this->getLeft()->preorderTraversal();
$this->getRight()->preorderTraversal();
}
/**
* 中序遍历
*/
public function inorderTraversal() {
if ($this->isEmpty()) {
return ;
}
$this->getLeft()->preorderTraversal();
echo ' ', $this->getKey();
$this->getRight()->preorderTraversal();
}
/**
* 后序遍历
*/
public function postorderTraversal() {
if ($this->isEmpty()) {
return ;
}
$this->getLeft()->preorderTraversal();
$this->getRight()->preorderTraversal();
echo ' ', $this->getKey();
}
}
/**
* 二叉排序树的PHP实现
*/
class BST extends BinaryTree {
/**
* 构造空的二叉排序树
*/
public function __construct() {
parent::__construct(NULL, NULL, NULL);
}
/**
* 析构
*/
public function __destruct() {
parent::__destruct();
}
/**
* 测试二叉排序树中是否包含参数所指定的值
*
* @param mixed $obj 查找的值.
* @return boolean True 如果存在于二叉排序树中则返回真,否则为假期
*/
public function contains($obj) {
if ($this->isEmpty())
return false;
$diff = $this->compare($obj);
if ($diff == 0) {
return true;
}elseif ($diff < 0) return $this->getLeft()->contains($obj);
else
return $this->getRight()->contains($obj);
}
/**
* 查找二叉排序树中参数所指定的值的位置
*
* @param mixed $obj 查找的值.
* @return boolean True 如果存在则返回包含此值的对象,否则为NULL
*/
public function find($obj) {
if ($this->isEmpty())
return NULL;
$diff = $this->compare($obj);
if ($diff == 0)
return $this->getKey();
elseif ($diff < 0) return $this->getLeft()->find($obj);
else
return $this->getRight()->find($obj);
}
/**
* 返回二叉排序树中的最小值
* @return mixed 如果存在则返回最小值,否则返回NULL
*/
public function findMin() {
if ($this->isEmpty ())
return NULL;
elseif ($this->getLeft()->isEmpty())
return $this->getKey();
else
return $this->getLeft()->findMin();
}
/**
* 返回二叉排序树中的最大值
* @return mixed 如果存在则返回最大值,否则返回NULL
*/
public function findMax() {
if ($this->isEmpty ())
return NULL;
elseif ($this->getRight()->isEmpty())
return $this->getKey();
else
return $this->getRight()->findMax();
}
/**
* 给二叉排序树插入指定值
*
* @param mixed $obj 需要插入的值.
* 如果指定的值在树中存在,则返回错误
*/
public function insert($obj) {
if ($this->isEmpty()) {
$this->attachKey($obj);
} else {
$diff = $this->compare($obj);
if ($diff == 0)
die('argu error');
if ($diff < 0) $this->getLeft()->insert($obj);
else
$this->getRight()->insert($obj);
}
$this->balance();
}
/**
* 从二叉排序树中删除指定的值
*
* @param mixed $obj 需要删除的值.
*/
public function delete($obj) {
if ($this->isEmpty ())
die();
$diff = $this->compare($obj);
if ($diff == 0) {
if (!$this->getLeft()->isEmpty()) {
$max = $this->getLeft()->findMax();
$this->key = $max;
$this->getLeft()->delete($max);
}
elseif (!$this->getRight()->isEmpty()) {
$min = $this->getRight()->findMin();
$this->key = $min;
$this->getRight()->delete($min);
} else
$this->detachKey();
} else if ($diff < 0) $this->getLeft()->delete($obj);
else
$this->getRight()->delete($obj);
$this->balance();
}
public function compare($obj) {
return $obj - $this->getKey();
}
/**
* Attaches the specified object as the key of this node.
* The node must be initially empty.
*
* @param object IObject $obj The key to attach.
* @exception IllegalOperationException If this node is not empty.
*/
public function attachKey($obj) {
if (!$this->isEmpty())
return false;
$this->key = $obj;
$this->left = new BST();
$this->right = new BST();
}
/**
* Balances this node.
* Does nothing in this class.
*/
protected function balance () {}
/**
* Main program.
*
* @param array $args Command-line arguments.
* @return integer Zero on success; non-zero on failure.
*/
public static function main($args) {
printf("BinarySearchTree main program.\n");
$root = new BST();
foreach ($args as $row) {
$root->insert($row);
}
return $root;
}
}
$root = BST::main(array(50, 3, 10, 5, 100, 56, 78));
echo $root->findMax();
$root->delete(100);
echo $root->findMax();
关于phpnoed树结构的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于phpnoed树结构 php tree的详细内容...