C# 宽度优先遍历
我试图解决一个面试问题,但为此我必须逐级遍历二叉树。我用下面的变量设计了BinaryNodeC# 宽度优先遍历,c#,.net,algorithm,data-structures,C#,.net,Algorithm,Data Structures,我试图解决一个面试问题,但为此我必须逐级遍历二叉树。我用下面的变量设计了BinaryNode private object data; private BinaryNode left; private BinaryNode right; 有人能帮我在BinarySearchTree类中编写BroadthFirstSearch方法吗 更新:感谢大家的投入。这就是面试的问题。 “给定一个二叉搜索树,设计一个算法,在每个深度创建一个包含所有节点的链表(即,如果你有一个深度为D的树,你将有D个链表)”
private object data;
private BinaryNode left;
private BinaryNode right;
有人能帮我在BinarySearchTree类中编写BroadthFirstSearch方法吗
更新:感谢大家的投入。这就是面试的问题。
“给定一个二叉搜索树,设计一个算法,在每个深度创建一个包含所有节点的链表(即,如果你有一个深度为D的树,你将有D个链表)”
这是我的方法,让我知道你的专家意见
public List<LinkedList<BNode>> FindLevelLinkList(BNode root)
{
Queue<BNode> q = new Queue<BNode>();
// List of all nodes starting from root.
List<BNode> list = new List<BNode>();
q.Enqueue(root);
while (q.Count > 0)
{
BNode current = q.Dequeue();
if (current == null)
continue;
q.Enqueue(current.Left);
q.Enqueue(current.Right);
list.Add(current);
}
// Add tree nodes of same depth into individual LinkedList. Then add all LinkedList into a List
LinkedList<BNode> LL = new LinkedList<BNode>();
List<LinkedList<BNode>> result = new List<LinkedList<BNode>>();
LL.AddLast(root);
int currentDepth = 0;
foreach (BNode node in list)
{
if (node != root)
{
if (node.Depth == currentDepth)
{
LL.AddLast(node);
}
else
{
result.Add(LL);
LL = new LinkedList<BNode>();
LL.AddLast(node);
currentDepth++;
}
}
}
// Add the last linkedlist
result.Add(LL);
return result;
}
public List FindLevelLinkList(BNode root)
{
队列q=新队列();
//从根开始的所有节点的列表。
列表=新列表();
q、 排队(根);
而(q.Count>0)
{
B节点电流=q.Dequeue();
如果(当前==null)
继续;
q、 排队(当前。左侧);
q、 排队(当前。右侧);
列表。添加(当前);
}
//将相同深度的树节点添加到单个LinkedList中。然后将所有LinkedList添加到列表中
LinkedList LL=新建LinkedList();
列表结果=新列表();
LL.AddLast(根);
int currentDepth=0;
foreach(列表中的BNode节点)
{
if(节点!=根)
{
if(node.Depth==currentDepth)
{
LL.AddLast(节点);
}
其他的
{
结果:添加(LL);
LL=新的LinkedList();
LL.AddLast(节点);
currentDepth++;
}
}
}
//添加最后一个linkedlist
结果:添加(LL);
返回结果;
}
宽度优先搜索通常通过队列实现,深度优先搜索使用堆栈
可与节点上的子属性
一起使用:
IEnumerable<Node> Children { get { return new []{ Left, Right }.Where(x => x != null); } }
var queue=new queue();
排队(rootNode);
while(queue.Any())
{
var currentNode=queue.Dequeue();
如果(currentNode.data==searchedData)
{
打破
}
if(currentNode.Left!=null)
queue.Enqueue(currentNode.Left);
if(currentNode.Right!=null)
排队(currentNode.Right);
}
使用DFS方法:树遍历为O(n)
到目前为止你试过什么?你能用简单的英语解释一下算法应该做什么(即给出伪代码)?维基百科的尽职调查怎么样@这并不奇怪。队列是实现广度优先搜索的明显选择,就像使用堆栈进行深度优先一样。@CodeInChaos感谢您的帮助。虽然这是一个老帖子,但我想我会留下一个反馈,以防它对某人有所帮助。1) 我无法编译您的“更高级的解决方案”。2) 您最初的解决方案运行良好。再次感谢。DFS和BFS的比较很好,我理解DFS,但我就是不能得到BFS,所以这个问题的关键是使用队列而不是堆栈。谢谢。如果你能为否决投票添加评论,那会很有帮助。很好的猜测是,OP要求广度优先,而你的开场白说你的答案是深度优先。
public static IEnumerable<T> BreadthFirstTopDownTraversal<T>(T root, Func<T, IEnumerable<T>> children)
{
var q = new Queue<T>();
q.Enqueue(root);
while (q.Count > 0)
{
T current = q.Dequeue();
yield return current;
foreach (var child in children(current))
q.Enqueue(child);
}
}
IEnumerable<Node> Children { get { return new []{ Left, Right }.Where(x => x != null); } }
foreach(var node in BreadthFirstTopDownTraversal(root, node => node.Children))
{
...
}
var queue = new Queue<BinaryNode>();
queue.Enqueue(rootNode);
while(queue.Any())
{
var currentNode = queue.Dequeue();
if(currentNode.data == searchedData)
{
break;
}
if(currentNode.Left != null)
queue.Enqueue(currentNode.Left);
if(currentNode.Right != null)
queue.Enqueue(currentNode.Right);
}
public class NodeLevel
{
public TreeNode Node { get; set;}
public int Level { get; set;}
}
public class NodeLevelList
{
private Dictionary<int,List<TreeNode>> finalLists = new Dictionary<int,List<TreeNode>>();
public void AddToDictionary(NodeLevel ndlvl)
{
if(finalLists.ContainsKey(ndlvl.Level))
{
finalLists[ndlvl.Level].Add(ndlvl.Node);
}
else
{
finalLists.Add(ndlvl.Level,new List<TreeNode>(){ndlvl.Node});
}
}
public Dictionary<int,List<TreeNode>> GetFinalList()
{
return finalLists;
}
}
public static void DFSLevel(TreeNode root, int level, NodeLevelList nodeLevelList)
{
if(root == null)
return;
nodeLevelList.AddToDictionary(new NodeLevel{Node = root, Level = level});
level++;
DFSLevel(root.Left,level,nodeLevelList);
DFSLevel(root.Right,level,nodeLevelList);
}