给定一个节点表(它们的ID和它们的父ID),我如何获得所有的根,内部和叶子节点?
这是我到目前为止所拥有的:
根节点
SELECT Id, "Root" FROM NodeTable
WHERE ParentId IS NULL;
内部节点
???
叶子节点
SELECT N1.Id, "Leaf" FROM NodeTable N1
LEFT JOIN NodeTable N2 ON N1.Id = N2.ParentId
WHERE N2.ParentId IS NULL;
这是对的吗?有没有办法只用一个查询就可以做到这一点?
答案 0 :(得分:5)
select case
when p is null then concat(n, ' Root')
when n in (select distinct p from bst) then concat(n, ' Inner')
else concat(n, ' Leaf')
end
from bst
order by n asc
答案 1 :(得分:3)
您的“内部节点”查询可以与“叶子节点”查询类似地派生。
func hideTabBar() {
var frame = self.tabBarController?.tabBar.frame
frame?.origin.y = self.view.frame.size.height + (frame?.size.height)!
UIView.animate(withDuration: 0.5, animations: {
self.tabBarController?.tabBar.frame = frame!
})
}
func showTabBar() {
var frame = self.tabBarController?.tabBar.frame
frame?.origin.y = self.view.frame.size.height - (frame?.size.height)!
UIView.animate(withDuration: 0.5, animations: {
self.tabBarController?.tabBar.frame = frame!
})
}
这将获得所有不是根节点的节点(N1)(因为它们具有非null的ParentId)并且具有子节点(N2)。
是的,您可以使用SELECT DISTINCT N1.Id, "Inner"
FROM NodeTable N1
JOIN NodeTable N2
ON N2.ParentId = N1.ID
WHERE N1.ParentId IS NOT NULL;
关键字在一个查询中完成所有这些操作。
UNION
答案 2 :(得分:2)
一种更简洁的方法:
SELECT
N,
IF(
P IS NULL,
'Root',
IF(
(
SELECT COUNT(*)
FROM BST
WHERE P=B.N
) > 0,
'Inner',
'Leaf'
)
)
FROM BST AS B
ORDER BY N;
OR
SELECT
N,
IF(
P IS NULL,
'Root',
IF(
B.N IN (SELECT P FROM BST),
'Inner',
'Leaf'
)
)
FROM BST AS B
ORDER BY N;
答案 3 :(得分:1)
select n, (case when p is null then "Root" when n in (select p from bst) then "Inner" else "Leaf" end) from BST order by n;
答案 4 :(得分:1)
您的“根和叶逻辑”似乎正确。内心通常会是其他一切
如果要在1个SQL代码中执行此操作,则可以尝试
const createTradsObjFrom = async (languages, tradsPaths) => {
try {
const obj = {};
for (const [index, lang] of languages.entries()) {
const path = tradsPaths[index];
obj[lang] = {
json: await $.getJSON(path)
};
}
return obj;
}
catch (err) {
throw new Error("some error");
}
};
const getTrads = currentLanguage => {
return new Promise((resolve, reject) => {
const tradsDir = "/assets/trads/";
const languages = [
"de",
"en",
"es",
"fr",
"it",
"nl",
"ru"
];
const tradsPaths = languages.map(e => tradsDir + e + '.json');
createTradsObjFrom(languages, tradsPaths)
.then(trads => resolve(trads[currentLanguage].json))
.catch(err => reject(err));
});
};
export { getTrads };
答案 5 :(得分:0)
SELECT DISTINCT(N), 'Root' FROM BST WHERE P IS NULL UNION
SELECT DISTINCT(N), 'Leaf' FROM BST WHERE N NOT IN (SELECT
DISTINCT(P) FROM BST WHERE P IS NOT NULL) UNION
SELECT DISTINCT(N), 'Inner' FROM BST WHERE N IN (SELECT
DISTINCT(P) FROM BST) AND P IS NOT NULL;
WHERE
Root= Parent is null,
leaf= when a Particular node is not a parent for any node,
Inner= when a Particular node is a parent for any node
答案 6 :(得分:0)
SELECT N,CASE WHEN ISNULL(P,0) = 0 THEN 'Root'
WHEN N IN (SELECT P FROM BST ) THEN 'Inner'
ELSE 'Leaf'
END NODETYPE
FROM BST
ORDER BY N
答案 7 :(得分:0)
选择 n , 当 p 为 null 时,则为 'Root' 当 n in (从 bst 中选择不同的 p )然后“内部” 否则 'Leaf' 以 t 结尾
从 bst order by 1;
答案 8 :(得分:0)
您可以使用存在和不存在:
选择 N, 'Root'
来自 BST,其中 p 为空
联盟
选择 N,'叶子'
来自 BST
如果不存在(从 BST b 中选择 * 其中 b.P =a.N )
工会
选择 N,'内部'
来自 BST
whereexists(select * from BST b where b.P =a.N and a.P is not null);
答案 9 :(得分:0)
您也可以尝试。.
选择N, B.p为NULL的情况下,则'Root' 当B.N IN(从BST中选择p),然后选择“内部” 否则“叶” 结束案例 从BST B到N;
答案 10 :(得分:0)
select N, 'Root' from N1 where P is null
UNION
select N, 'Leaf' from N1 where N not in (select P from N1 where p is not
null)
UNION
select N , 'Inner' from N1 where N in
(select P from N1 where p NOT IN (select N from N1 where P is null))
order by n;
答案 11 :(得分:0)
简短高效
SELECT N , CASE WHEN P is null THEN 'Root' ELSE
(IF (N IN (SELECT P FROM BST ), 'Inner','Leaf' ) ) END FROM BST ORDER BY N
答案 12 :(得分:0)
Select distinct(N),x
from(
select N,P,
case when P is null and N is not null then 'Root'
when N in (Select distinct(P)from BST where P is not null) then 'Inner'
else 'Leaf' end as x
from BST)a
order by N;
答案 13 :(得分:0)
选择n,大小写 当p为null时,则'Root' 当n进入(从bst选择p)时,则选择“内部” 否则“叶” 结束 从bst开始 按n排序;
答案 14 :(得分:0)
以下是我的解决方案:
select N,case when level=1 then 'Root'
when connect_by_isleaf=1 then 'Leaf'
when level>1 and connect_by_isleaf=0 then 'Inner'
end
from BST
start with p is null
connect by p=prior N
order by N;
答案 15 :(得分:0)
这是另一种选择:
SELECT distinct N1.N,
Case
When N1.P is null then 'Root'
When N2.P is null then 'Leaf'
Else 'Inner'
End
FROM BST N1
LEFT JOIN BST N2
ON N1.N = N2.P
ORDER BY N1.n
答案 16 :(得分:0)
我相信它来自以下链接:https://www.hackerrank.com/challenges/binary-search-tree-1/problem
这就是您可以做的!
SELECT N, IF(P IS NULL, 'Root', IF((SELECT COUNT(*) FROM BST WHERE P=B.N)>0,'Inner','Leaf'))
FROM BST AS B
ORDER BY N;
答案 17 :(得分:0)
选择n,如果输入p in(从bst选择n),然后选择n输入(从bst选择p),然后“ Inner”,否则“ Leaf”结束,否则“ null”从bst到n结束;
答案 18 :(得分:0)
select t.N,
case
when t.P is null then 'Root'
when t.N in (select t1.P from BST t1 where t1.P = t.N) then 'Inner'
else 'Leaf'
end
from BST t
order by t.N