C# - IEnumerable<> StackOverFlowException (AVLTree)
C# - IEnumerable<> StackOverFlowException (AVLTree)
我在使用 IEnumerable<>
时遇到问题
我正在读取 CSV 文件,我对 get/set 方法进行了排序,并且在实施 IEnumerable<>
之前测试了我的代码,并且 Autos/Locals 显示正确。
我正在尝试在 DataGridView 中显示 CSV 文件的内容
foreach (Country country in avlTree)
{
displayCountriesDataGridView.Rows.Add(country.CountryName, country.GDPGrowth, country.Inflation, country.TradeBalance, country.HDIRank, country.TradingPartners);
}
并使用 AVLTree 和我编写的 InsertItem
方法将此数据附加到 AVLTree
avlTree.InsertItem(tempCountry);
这个问题是在我使用的时候出现的:
class AVLTree<T> : BSTree<T>, IEnumerable<Country> where T : IComparable
我已经实现了接口:
public IEnumerator<Country> GetEnumerator()
{
return this.GetEnumerator();
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
throw new NotImplementedException();
}
}
然而,无论如何都没有运气。
在 Locals 中,我得到了这个输出-
$exception {"Exception of type "System.WhosebugException' was thrown"}
我得到一个红叉和名称 this
的值 Unable to read memory
而 Type Variables T
的值为 System.__Canon
我在我的国家 class 实施了 IEnumerable<>
,没有任何问题。
我似乎无法理解是什么导致了这个问题。
有人可以提供一些指导或阐明这个问题。
谢谢。
编辑 - 我的 AVLTree 的实现
class AVLTree<T> : BSTree<T>, IEnumerable<Country> where T : IComparable
{
Node<T> newRoot;
public new void InsertItem(T item)
{
insertItem(item, ref root);
}
private void insertItem(T item, ref Node<T> tree)
{
if (tree == null)
{
tree = new Node<T>(item);
}
else if (item.CompareTo(tree.Data) < 0)
{
insertItem(item, ref tree.Left);
}
else if (item.CompareTo(tree.Data) > 0)
{
insertItem(item, ref tree.Right);
}
tree.BalanceFactor = Height(ref tree.Left) - Height(ref tree.Right);
if (tree.BalanceFactor <= -2)
{
rotateLeft(ref tree);
}
if (tree.BalanceFactor >= 2)
{
rotateRight(ref tree);
}
}
private void rotateRight(ref Node<T> tree)
{
if (tree.Left.BalanceFactor < 0)
{
rotateLeft(ref tree.Left);
}
newRoot = tree.Left;
tree.Left = newRoot.Right;
newRoot.Right = tree;
tree = newRoot;
}
private void rotateLeft(ref Node<T> tree)
{
if (tree.Right.BalanceFactor > 0)
{
rotateRight(ref tree.Right);
}
newRoot = tree.Right;
tree.Right = newRoot.Left;
newRoot.Left = tree;
tree = newRoot;
}
public IEnumerator<Country> GetEnumerator()
{
// Some iterator/loop which uses "yield return" for each item
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
您需要实际实现一个枚举器。现在,您的实现通过调用自身无限循环——没有返回枚举器。
public IEnumerator<Country> GetEnumerator()
{
// Some iterator/loop which uses "yield return" for each item
foreach (var item in items)
yield return item;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
我在使用 IEnumerable<>
我正在读取 CSV 文件,我对 get/set 方法进行了排序,并且在实施 IEnumerable<>
之前测试了我的代码,并且 Autos/Locals 显示正确。
我正在尝试在 DataGridView 中显示 CSV 文件的内容
foreach (Country country in avlTree)
{
displayCountriesDataGridView.Rows.Add(country.CountryName, country.GDPGrowth, country.Inflation, country.TradeBalance, country.HDIRank, country.TradingPartners);
}
并使用 AVLTree 和我编写的 InsertItem
方法将此数据附加到 AVLTree
avlTree.InsertItem(tempCountry);
这个问题是在我使用的时候出现的:
class AVLTree<T> : BSTree<T>, IEnumerable<Country> where T : IComparable
我已经实现了接口:
public IEnumerator<Country> GetEnumerator()
{
return this.GetEnumerator();
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
throw new NotImplementedException();
}
}
然而,无论如何都没有运气。
在 Locals 中,我得到了这个输出-
$exception {"Exception of type "System.WhosebugException' was thrown"}
我得到一个红叉和名称 this
的值 Unable to read memory
而 Type Variables T
的值为 System.__Canon
我在我的国家 class 实施了 IEnumerable<>
,没有任何问题。
我似乎无法理解是什么导致了这个问题。
有人可以提供一些指导或阐明这个问题。
谢谢。
编辑 - 我的 AVLTree 的实现
class AVLTree<T> : BSTree<T>, IEnumerable<Country> where T : IComparable
{
Node<T> newRoot;
public new void InsertItem(T item)
{
insertItem(item, ref root);
}
private void insertItem(T item, ref Node<T> tree)
{
if (tree == null)
{
tree = new Node<T>(item);
}
else if (item.CompareTo(tree.Data) < 0)
{
insertItem(item, ref tree.Left);
}
else if (item.CompareTo(tree.Data) > 0)
{
insertItem(item, ref tree.Right);
}
tree.BalanceFactor = Height(ref tree.Left) - Height(ref tree.Right);
if (tree.BalanceFactor <= -2)
{
rotateLeft(ref tree);
}
if (tree.BalanceFactor >= 2)
{
rotateRight(ref tree);
}
}
private void rotateRight(ref Node<T> tree)
{
if (tree.Left.BalanceFactor < 0)
{
rotateLeft(ref tree.Left);
}
newRoot = tree.Left;
tree.Left = newRoot.Right;
newRoot.Right = tree;
tree = newRoot;
}
private void rotateLeft(ref Node<T> tree)
{
if (tree.Right.BalanceFactor > 0)
{
rotateRight(ref tree.Right);
}
newRoot = tree.Right;
tree.Right = newRoot.Left;
newRoot.Left = tree;
tree = newRoot;
}
public IEnumerator<Country> GetEnumerator()
{
// Some iterator/loop which uses "yield return" for each item
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
您需要实际实现一个枚举器。现在,您的实现通过调用自身无限循环——没有返回枚举器。
public IEnumerator<Country> GetEnumerator()
{
// Some iterator/loop which uses "yield return" for each item
foreach (var item in items)
yield return item;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}