我想创建一个应用程序来查找网络上的特定 IP 地址是否在线。我已经知道IP了。我对 C# 很陌生,但想知道是否有人可以给我一个简单的解决方案。谢谢。
在构建了一个BST<Tkey,TValue>
由BSTNode<Tkey,TValue>
节点组成的二叉搜索树之后,我试图为它实现IEnumerable接口.
这就是我构建的方式BSTNodeEnumrator<Tkey,TValue>
:
public class BSTNodeEnumerator<TKey, TValue> : IEnumerator<BSTNode<TKey, TValue>> where TKey : IComparable<TKey>
{
private Stack<BSTNode<TKey, TValue>> _stack;
public BSTNodeEnumerator(BSTNode<TKey, TValue> root)
{
_stack = new Stack<BSTNode<TKey, TValue>>();
_current = null;
_root = root;
}
// ... rest of the implementation
}
Run Code Online (Sandbox Code Playgroud)
我传入root
节点并且_current
是枚举的结果.我也试图使用堆栈,因为我不像AVL BST那样跟踪父节点.
现在我希望枚举器以非递归的方式按顺序遍历树.这应该导致排序的枚举,因为bst的属性,这是伟大的,因为这正是我想要实现的.
用于在伪代码中遍历的非递归算法,如本维基百科文章中所述
iterativeInorder(node)
s ? empty stack
while (not s.isEmpty() or node ? null)
if (node ? null)
s.push(node)
node ? node.left
else
node ? …
Run Code Online (Sandbox Code Playgroud)