简体   繁体   English

C#-IEnumerable <> StackOverFlowException(AVLTree)

[英]C# - IEnumerable<> StackOverFlowException (AVLTree)

I am having an issue with using IEnumerable<> 我在使用IEnumerable<>时遇到问题

I am reading in a CSV File, I have my get/set methods sorted and I have tested my code before implementing IEnumerable<> and the Autos/Locals are displayed correctly. 我正在读取CSV文件,对get / set方法进行了排序,并且在实现IEnumerable<>之前测试了代码,并且正确显示了Autos / Locals。

I'm trying to display the CSV file's content within a DataGridView 我正在尝试在DataGridView中显示CSV文件的内容

foreach (Country country in avlTree)
                    {

                      displayCountriesDataGridView.Rows.Add(country.CountryName, country.GDPGrowth, country.Inflation, country.TradeBalance, country.HDIRank, country.TradingPartners);

                    }

And using an AVLTree and the InsertItem method I have wrote to append this data to the AVLTree 并使用我编写的AVLTree和InsertItem方法将此数据附加到AVLTree

  avlTree.InsertItem(tempCountry);

This problem arises when I use: 当我使用时会出现此问题:

class AVLTree<T> : BSTree<T>, IEnumerable<Country> where T : IComparable

I have implemented the interfaces : 我已经实现了接口:

 public IEnumerator<Country> GetEnumerator()
        {

            return this.GetEnumerator();

            throw new NotImplementedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {

            return GetEnumerator();
            throw new NotImplementedException();
        }


    }

However, no luck what-so-ever. 但是,至今没有运气。

In the Locals, I get this output- 在当地人那里,我得到的输出是

$exception {"Exception of type "System.StackOverflowException' was thrown"}

I get a red-cross and the name this with the value Unable to read memory 我得到一个红色的叉叉和名字this与价值Unable to read memory

And Type Variables T has the value System.__Canon 并且Type Variables T的值为System.__Canon

I implemented IEnumerable<> in my Country class and no issues. 我在Country类中实现了IEnumerable<> ,没有任何问题。

I just can't seem to understand what is causing this issue. 我似乎无法理解是什么导致了此问题。

Could someone please offer some guidance or shed light on this matter. 有人可以提供一些指导或阐明此事。

Thank you. 谢谢。

EDIT - Implementation of my AVLTree 编辑-我的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();
        }

You need to actually implement an enumerator. 您实际上需要实现一个枚举器。 Right now your implementation loops infinitely by calling itself -- no enumerator is returned. 现在,您的实现可以通过调用自身无限循环-不返回任何枚举数。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM