如何在二叉树中查找子级的级别和数量 [英] How to find the level and number of childs in binary tree

查看:106
本文介绍了如何在二叉树中查找子级的级别和数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我必须开发MLM项目,我想计算

1.来自每个节点的孩子数量。

2.找到节点的等级。



1

/ \

2 3

/ \ / \

4 5 6 7



例如。 TOT。没有。 '1'的孩子是6

Tot。没有。 '2'的孩子是2

Tot。没有。 '4'的孩子是0

...........



我试过的:



我有这样的数据库表:



id parent_id Leg

--------------------

1 0

2 1 L

3 1 R

4 2 L

5 2 R

6 3 L

7 3 R $ / $


我试过这个函数它会出错:

CREATE FUNCTION get_counts(value INT)RETURNS INT

NOT DETERMINISTIC

读取SQL数据

BEGIN

DECLARE _id INT;

DECLARE _parent INT;

DECLARE _next INT;

DECLARE CONTINUE HANDLER for NOT FOUND SET @id = NULL;



SET _parent = @id;

SET _id = -1;



如果@id IS为NULL

RETURN NULL;

END IF;



LOOP

SELECT MIN(id)

INTO @id

来自my_registration

WHERE sponser_id = _parent

AND id> _id;

如果@id IS NOT NULL或_parent = @start_with那么

SET @level = @level + 1;

RETURN @id ;

结束IF;

SET @level:= @level - 1;

SELECT id,sponser_id

INTO _id,_parent

来自my_registration

WHERE id = _parent;

END LOOP;

结束



错误是:MySQL说:文档



#1064 - 您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第5行附近使用正确的语法

Dear Friends,

I have to develop the MLM project, I want to calculate
1. Number of children from each node.
2. find the level of node.

1
/ \
2 3
/ \ / \
4 5 6 7

For Eg. Tot. no. of child for '1' is 6
Tot. no. of child for '2' is 2
Tot. no. of child for '4' is 0
...........

What I have tried:

I have db table like this:

id parent_id Leg
--------------------
1 0
2 1 L
3 1 R
4 2 L
5 2 R
6 3 L
7 3 R

I tried this function it makes error:
CREATE FUNCTION get_counts(value INT) RETURNS INT
NOT DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE _id INT;
DECLARE _parent INT;
DECLARE _next INT;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET @id = NULL;

SET _parent = @id;
SET _id = -1;

IF @id IS NULL THEN
RETURN NULL;
END IF;

LOOP
SELECT MIN(id)
INTO @id
FROM my_registration
WHERE sponser_id = _parent
AND id > _id;
IF @id IS NOT NULL OR _parent = @start_with THEN
SET @level = @level + 1;
RETURN @id;
END IF;
SET @level := @level - 1;
SELECT id, sponser_id
INTO _id, _parent
FROM my_registration
WHERE id = _parent;
END LOOP;
END

ERROR is: MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5

推荐答案

我将在代码中重新构建树,使用从表中检索的数据,然后遍历它以计算所需的值。

请参阅:例如: PHP Master | PHP Devs的数据结构:树 [ ^ ]。
I would re-build the tree in code, using the data retrieved from the table, and then traverse it in order to compute the required values.
See, for instance: PHP Master | Data Structures for PHP Devs: Trees[^].


拿一张纸,注意表格,添加2列,1列表示水平,1表示孩子。

不要使用你的神能够为您提供即时答案并尝试手动解决问题的力量。

请注意,您唯一的信息是每个节点的父节点。

等级:你如何获得表中节点的级别?获取随机节点的级别需要了解什么?

Childs:如何获得表中节点的子节点数?你需要知道什么来获得随机节点的孩子数量?



你应该拿出一个程序来获得答案。



Nota;该表包含您需要的所有内容,无需构建树。
Take a sheet of paper, note the table, add 2 columns, 1 for level and 1 for childs.
Don't use your god power that gives you instant answer and try to solve the problem by hand.
Note that your only information is the parent node for every node.
Level: How do you get the level of a node in the table? What do you need to know to get the level of a random node?
Childs: How do you get the number of childs of a node in the table? What do you need to know to get the number of childs of a random node?

You should come out with a procedure to get the answers.

Nota; the table contain everything you need, no need to build the tree.


这篇关于如何在二叉树中查找子级的级别和数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆