如何获得二叉树的极左节点或极右节点?

时间:2017-01-19 08:24:50

标签: php mysql binary-tree

我的数据库结构是enter image description here

我已经写下了处理二进制树的所有查询,只剩下大多数节点查找待处理。我也在stackoverflow上得到了解决方案,但这是针对SQL的,但我希望在Mysql中这里是链接SQL Link solution。任何人都可以在MYSQL中为我改变它。

这是我的MYSQL存储过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `getLeftMost`(IN `iNode` INT)
BEGIN

DECLARE left_id int;
DECLARE prev_id int;
SET prev_id=iNode;


SELECT lnode into left_id from binTree where nodeid=iNode;

CREATE TEMPORARY table IF NOT EXISTS temp_table as (select * from binTree where 1=0);
TRUNCATE table temp_table;

WHILE  left_id <> 0 DO
INSERT INTO temp_table SELECT * FROM binTree where nodeid=prev_id;
SET prev_id=left_id;
SELECT lnode into left_id from binTree where nodeid=prev_id;
END WHILE;

SELECT lnode as `leaf_node` FROM temp_table order by nodeid desc limit 0,1;

END

由于

0 个答案:

没有答案