C++ 节点小于或大于特定节点的节点数
给定一棵树,计算每个顶点在其子树中大于顶点的节点数和小于顶点的节点数 我可以通过深度优先搜索(DFS)找到单个顶点的答案,但是对每个顶点进行深度优先搜索肯定会花费很多时间C++ 节点小于或大于特定节点的节点数,c++,algorithm,graph,tree,graph-algorithm,C++,Algorithm,Graph,Tree,Graph Algorithm,给定一棵树,计算每个顶点在其子树中大于顶点的节点数和小于顶点的节点数 我可以通过深度优先搜索(DFS)找到单个顶点的答案,但是对每个顶点进行深度优先搜索肯定会花费很多时间 我们能更快地解决这个问题吗?更有效一点的方法: 一种更有效的方法是执行后序和使用二进制搜索树类型映射(可能是在C++中,排序映射实现应该已经使用某种红黑树)来存储密钥和它们的计数。 对树进行后序遍历。 在遍历后的每个节点上: a) 对于更大的节点: 获取BST类型映射中当前节点的下一个最高值(这应在O(log n)中完成,并计
我们能更快地解决这个问题吗?更有效一点的方法:
一种更有效的方法是执行后序和使用二进制搜索树类型映射(可能是在C++中,排序映射实现应该已经使用某种红黑树)来存储密钥和它们的计数。 对树进行后序遍历。 在遍历后的每个节点上:
a) 对于更大的节点: 获取BST类型映射中当前节点的下一个最高值(这应在O(log n)
中完成,并计算其下的所有值。(这将节省大量时间,尤其是在树处于平衡状态时)。但如果树处于不平衡状态,则成本会很高
b) 对于较小的节点:
获取BST类型映射中当前节点的下一个最小值(这应该在O(log n)
中完成,并计算其上的所有值
//map - sorted map with keys and their count
//greater_map - unordered map that contains node as key and count of nodes greater than key node.
//lesser_map - unordered map that contains node as key and count of nodes lesser than key node.
post_order( Node root )
if ( root == null ) return;
for each child in root.children:
post_order( child )
int next_higher_key = find_next_higher_key( map, root_key )
greater_map.insert(root, count_all_values_greater_than_equal_to(next_higher_key, map) )
int next_smaller_key = find_next_smaller_key( map, root_key )
lesser_map.insert(root, count_all_values_lesser_than_equal_to(next_smaller_key, map) )
if ( !map[root_key] )
map.insert( root_key, 1 )
else
map.insert( root_key, map[root_key] + 1 )
效率较低的方法:
对树执行一次深度优先搜索遍历。遍历完成后,在每个节点上,将较大节点数和较小节点数存储在两个映射中。键将是刚刚完成遍历的节点,值将是大于当前节点数和小于当前节点数的节点数从地图上可以很容易地得到任意给定节点的大小节点数
伪代码:
list_of_nodes dfs( Node root ) {
list_of_nodes thisList = new list_of_nodes;
if ( root == null ) return thisList;
thisList.push( root );
for each child in root.children:
list_of_nodes childList = dfs( child )
thisList.addAll( childList )
int greater = 0, lesser = 0
for each node in thisList:
if node_value > root_value
greater++
else if node_value < root_value
lesser++
greatermap.insert( root, greater )
lessermap.insert( root, lesser )
return thisList
更有效的方法:
一种更有效的方法是执行后序和使用二进制搜索树类型映射(可能是在C++中,排序映射实现应该已经使用某种红黑树)来存储密钥和它们的计数。 对树进行后序遍历。 在遍历后的每个节点上:
a) 对于更大的节点: 获取BST类型映射中当前节点的下一个最高值(这应在O(log n)
中完成,并计算其下的所有值。(这将节省大量时间,尤其是在树处于平衡状态时)。但如果树处于不平衡状态,则成本会很高
b) 对于较小的节点:
获取BST类型映射中当前节点的下一个最小值(这应该在O(log n)
中完成,并计算其上的所有值
//map - sorted map with keys and their count
//greater_map - unordered map that contains node as key and count of nodes greater than key node.
//lesser_map - unordered map that contains node as key and count of nodes lesser than key node.
post_order( Node root )
if ( root == null ) return;
for each child in root.children:
post_order( child )
int next_higher_key = find_next_higher_key( map, root_key )
greater_map.insert(root, count_all_values_greater_than_equal_to(next_higher_key, map) )
int next_smaller_key = find_next_smaller_key( map, root_key )
lesser_map.insert(root, count_all_values_lesser_than_equal_to(next_smaller_key, map) )
if ( !map[root_key] )
map.insert( root_key, 1 )
else
map.insert( root_key, map[root_key] + 1 )
效率较低的方法:
对树执行一次深度优先搜索遍历。遍历完成后,在每个节点上,将较大节点数和较小节点数存储在两个映射中。键将是刚刚完成遍历的节点,值将是大于当前节点数和小于当前节点数的节点数从地图上可以很容易地得到任意给定节点的大小节点数
伪代码:
list_of_nodes dfs( Node root ) {
list_of_nodes thisList = new list_of_nodes;
if ( root == null ) return thisList;
thisList.push( root );
for each child in root.children:
list_of_nodes childList = dfs( child )
thisList.addAll( childList )
int greater = 0, lesser = 0
for each node in thisList:
if node_value > root_value
greater++
else if node_value < root_value
lesser++
greatermap.insert( root, greater )
lessermap.insert( root, lesser )
return thisList
一种简单的方法:执行DFS并将“当前处理的顶点”列表保留为堆栈。首次访问节点时,将其添加到堆栈中,最后一次访问后将其删除。给定DFS中的遍历顺序,这些操作听起来很合理。堆栈元素是元组
(值,计数)
,用(值,0)初始化
。在第一次访问每个顶点时,迭代堆栈并适当增加计数。移除堆栈元素后,将其计数
存储在当前节点中。此操作在O(n^2)
中运行(O(d*n)
使用d
树的深度)。嗯,我需要一个更优化的解决方案,这个解决方案非常直观。一个简单的方法:执行DFS并将“当前处理的顶点”列表作为堆栈保留。首次访问节点时,将其添加到堆栈中,最后一次访问后将其删除。给定DFS中的遍历顺序,操作听起来不错。堆栈元素是元组(value,count)
初始化为(value,0)
。第一次访问每个顶点时,迭代堆栈并适当增加计数。移除堆栈元素后,将其计数存储到当前节点。此操作在O(n^2)
中运行(O(d*n)
具有d
树的深度)。我需要一个更优化的解决方案,这个解决方案非常直观。对于根目录中的每个子节点。子节点:节点列表childList=dfs(child)thisList.addAll(childList)
这里您将子树列表添加到根列表中,这可能导致二次复杂性@SomeDude@AlexxVladisov是的,这会增加复杂性。更有效的方法是使用排序映射来存储键并进行后期排序。添加到我的答案中。对于root中的每个子级。子级:节点列表\u childList=dfs(child)thisList.addAll(childList)
在这里,您将子树列表添加到根列表中,这可能导致二次复杂性@SomeDude@AlexxVladisov是的,这会增加复杂性。一个更有效的方法是使用排序的映射来存储键并进行后期排序。添加到我的答案中。