- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
为了计算二叉树中的下线数量,我正在通过制作 2 个用于注册和成员下线结构的数据库来尝试以下脚本。它确实有效。
但是成员结构数据库增长非常快。导致二叉树中的n层会产生n条单用户注册记录。我只是想知道如果用户注册到 1000 级,那么它将在单个用户注册中创建 1000 条记录。
这个系统还有其他解决方案吗?
完整的长脚本是:
CREATE TABLE IF NOT EXISTS `member` (
`id` int(255) NOT NULL AUTO_INCREMENT,
`username` varchar(55) CHARACTER SET utf8 NOT NULL,
`upline` varchar(55) CHARACTER SET utf8 NOT NULL,
`position` varchar(10) NOT NULL,
`sponsor` varchar(55) CHARACTER SET utf8 NOT NULL,
`_left` varchar(55) CHARACTER SET utf8 NOT NULL,
`_right` varchar(55) CHARACTER SET utf8 NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
制作下线结构表:
CREATE TABLE IF NOT EXISTS `net_downline` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(60) NOT NULL,
`upline` varchar(60) NOT NULL,
`level` int(7) NOT NULL,
`position` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
从简单或基本的注册表中获取:
$newuser = htmlentities(trim($_POST['user']));
$sponsor = htmlentities(trim($_POST['sponsor']));
$upline = htmlentities(trim($_POST['upline']));
$position = htmlentities(trim($_POST['position']));
在注册过程中,执行树形步骤:
// 1. register new member
$users->dummyRegister($newuser, $sponsor, $upline, $position);
// 2. update upline
$users->dummyUpdateUpline($newuser, $upline, $position);
// 3. create donwline structure of binary tree
$level=0;
$memberid=$newuser;
do{
$getdata=$users->dummyGetUpline($memberid);
$uplne=$getdata[2];
$posi=$getdata[3];
$level++;
if($uplne!==''){
$users->dummyInsert_NetDownline($newuser, $uplne, $posi, $level);
}
$memberid=$uplne;
}
while($memberid!='');
该脚本的“用户”类:
<?php // start class
class Users{
private $db;
public function __construct($database) {
$this->db = $database;
}
public function dummyRegister($username, $sponsor, $upline, $position, $today){
$query = $this->db->prepare("INSERT INTO `member` (`username`, `sponsor`, `upline`, `position`, `entry_date` ) VALUES (?, ?, ?, ?, ?) ");
$query->bindValue(1, $username);
$query->bindValue(2, $sponsor);
$query->bindValue(3, $upline);
$query->bindValue(4, $position);
$query->bindValue(5, $today);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
public function dummyUpdateUpline($username, $upline, $position){
if ($position=='left') {
$query = $this->db->prepare("UPDATE `member` SET `_left`=? WHERE username=? ");
}elseif ($position=='right') {
$query = $this->db->prepare("UPDATE `member` SET `_right`=? WHERE username=? ");
}
$query->bindValue(1, $username);
$query->bindValue(2, $upline);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
public function dummyGetUpline($newuser) {// for demo
$query = $this->db->prepare("SELECT * FROM `member` WHERE `username`= ?");
$query->bindValue(1, $newuser);
try{
$query->execute();
$rows = $query->fetch();
return $rows;//['upline'];
} catch(PDOException $e){
die($e->getMessage());
}
}
public function dummyInsert_NetDownline($newuser, $upline, $posi, $level){// for demo
$query = $this->db->prepare("INSERT INTO `net_downline` (`username`, `upline`, `position`, `level` ) VALUES (?, ?, ? ,?) ");
$query->bindValue(1, $newuser);
$query->bindValue(2, $upline);
$query->bindValue(3, $posi);
$query->bindValue(4, $level);
try{
$query->execute();
}catch(PDOException $e){
die($e->getMessage());
}
}
}// endclass
创建 initial.php 并放在最上面 regitration php 脚本:
<?php
if(!isset($_SESSION)) { session_start(); }
require 'conn/database.php'; // in folder conn
require 'clas/users.php'; // in folder class
$users = new Users($db);
}
?>
处理数据库连接(clas/database.php 脚本):
<?php
$config = array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'sampledatabase'
);
$db = new PDO('mysql:host=' . $config['host'] . ';dbname=' . $config['dbname'], $config['username'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
最佳答案
这是我尝试了很多方法后的答案。解决我之前的问题。仅使用上面的单个成员表。
显示左右下线的数量。我将此脚本插入到树 A 中每个用户的 HTML 成员树页面中,一直到 B/C,一直到 D/E/F/G):
<?php echo $users->downline_number($member,'_left'); ?>
<?php echo $users->downline_number($member,'_right'); ?>
在用户类中添加该函数;
function downline_number($member,$position) {
$query = $this->db->prepare("SELECT * FROM `member` WHERE `upline`='$member' AND `position`='$position'");
$query->bindValue(1, $member);
$query->bindValue(2, $position);
try{
$query->execute();
$rows = $query->fetch();
if($this->count_downline($member,$position) >0 ){
$total=$this->total_members_down($rows['username']);
}else{
$total=0;
}
return $total;
}catch(PDOException $e){
die($e->getMessage());
}
}
function count_downline($member,$position) {
$query = $this->db->prepare("SELECT * FROM `member` WHERE `upline`=? AND `position`=? ");
$query->bindValue(1, $member);
$query->bindValue(2, $position);
try{
$query->execute();
return $rows = $query->rowCount();
}catch(PDOException $e){
die($e->getMessage());
}
}
function total_members_down($upline,$reset=0) {
global $num;
if ($reset==0) { $num=1; }
$query = $this->db->prepare("SELECT * FROM `member` where `upline`='$upline' order by id asc");
$query->bindValue(1, $upline);
try{
$query->execute();
if ($upline !='') {
if ($this->total_down($upline) > 0 ) {
while ($rows = $query->fetch() ) {
$num++;
$this->total_members_down($rows['username'],$num);
}
return $num;
} else {
return $num;
}
} else { $num=0; return $num; }
}catch(PDOException $e){
die($e->getMessage());
}
}
function total_down($upline) {
$query = $this->db->prepare("SELECT * FROM `member` where `upline`='$upline' order by id asc ");
$query->bindValue(1, $upline);
try{
$query->execute();
return $rows = $query->rowCount();
}catch(PDOException $e){
die($e->getMessage());
}
}
它可以显示二进制成员树结构。显示 memberID 没有附加在这里,导致它的简单方法。只是左右下线号码。
希望这篇文章能帮助到有需要的人。有什么更好的建议吗?
关于PHP计算二叉树中的下线数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25323039/
我想在我的 Tree 类中创建一个函数来遍历 n-ary Tree[T] 以取回具有 (level, T) 的元组,以便该 Tree 的用户可以执行类似 tree.traverse.foreach{
给定一个层次格式的数组,它们的直接子级存储在一个连续的数组中,返回一个 n 叉树 给定输入格式: [{'name':'a', 'level': -1}, {'name':'b', 'level
我要求教授给我一份另一个学期的旧作业。它是关于构建一个家谱,然后找到给定的两个节点之间的亲属关系。家谱是关于那美克星人(龙珠z)的,所以每个那美克星人都有一个父亲。 问题是输入是这样的: First
我正在尝试创建一个包含子 vector 的 n 叉树。 这就是我到目前为止所得到的。 在 node.h 文件中我有这个: #include #include using namespa
我正在尝试了解 n 叉树的预序遍历。我一直在阅读,我发现的所有示例都使用左子树和右子树,但是在 n 叉树中,什么是左子树,什么是右子树?有人可以给出一个很好的解释或伪代码吗? 最佳答案 而不是考虑 l
我应该反序列化一个 n 叉树。 这段代码创建了我的树: foodtree.addChildren("Food", { "Plant", "Animal" } ); foodtree.a
我正在尝试创建叉 TreeMap ,但仍然没有成功。这是我的代码: #include #include #include void procStatus(int level) { prin
我有一个二叉树,代表一个解析后的逻辑公式。例如,f = a & b & -c | d 由前缀表示法的列表列表表示,其中第一个元素是运算符(一元或二元),接下来的元素是它们的参数: f = [ |, [
我正在尝试根据给定的输入创建一棵树。那里将有一个根,包括子节点和子子节点。我可以实现树,在其中我可以将子节点添加到特定的主节点(我已经知道根)。但是,我试图弄清楚实现树的推荐方法是什么,我们必须首先从
我在 n 个节点上有一个完整的 19 元树。我标记所有具有以下属性的节点,即它们的所有非根祖先都是最年长或最小的 child (包括根)。我必须为标记节点的数量给出一个渐近界限。 我注意到 第一层有一
如何在不使用递归的情况下遍历 n 叉树? 递归方式: traverse(Node node) { if(node == null) return; for(Node c
我的树/节点类: import java.util.ArrayList; import java.util.List; public class Node { private T data;
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
我在我的 Java 应用程序中有一个非 UI 使用的所谓的“k-ary”树,我想知道 javax.swing.tree 包是否是完成这项工作的正确工具,即使它与 Swing 打包在一起. 我有一类 W
我正在用 Java 实现 N 叉树;每个节点可以有尽可能多的节点。当我尝试 build 一棵树时,问题就来了。我有一个函数可以递归地创建一个特定高度的树,并根据节点列表分配子节点。当我调用该函数时,根
嗨,我有这段代码来搜索 n 叉树,但它不能正常工作,我不知道这有什么问题当搜索 n4 和 n5 时,它返回 n3怎么了? public FamilyNode findNodeByName(Family
哪个是 C 语言中 N 叉树的简洁实现? 特别是,我想实现一个 n 元树,而不是自平衡的,每个节点中的子节点数量不受限制,其中每个节点都包含一个已经定义的结构,例如: struct task {
#include #include #include typedef struct _Tree { struct _Tree *child; struct _Tree *
我正在编写文件系统层次结构的 N 叉树表示形式,其中每个节点都包含有关它所表示的文件/文件夹的一些信息。 public class TreeNode { private FileSystemE
如何在 R 中为给定数量的分支和深度构建 N 叉树,例如深度为 3 的二叉树? 编辑:将源问题与问答分开。 最佳答案 我想提出解决方案,我用它来构建树数据结构 叶安姆 分支因子。要将数据存储在树中,字
我是一名优秀的程序员,十分优秀!