在C#中实现SplayTrees,添加到树结构时出错 [英] Implementing SplayTrees in C#, error in adding to tree sturcture

查看:82
本文介绍了在C#中实现SplayTrees,添加到树结构时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在尝试使用我发现的此类在此链接下在线实现splaytree:
https://raw.github.com/gist/940104/01bb918f4190b8e8016ab968724a3f0953c14416/SplayTree%602.cs [ ^ ]

香港专业教育学院做出了以下代码,以将用户的一组值添加到树中,但它给了我一个参数异常,即键已存在? 我做错了什么?


Hello,
I''m trying to use this class i found for implementing a splaytree online under this link:
https://raw.github.com/gist/940104/01bb918f4190b8e8016ab968724a3f0953c14416/SplayTree%602.cs[^]

ive made the following code to add a set of values from the user to the tree but it gives me an argument exception that the key already exists??
What am I doing wrong?


private void makeTreeBtn_Click(object sender, EventArgs e)
       {
SplayTree<int, int> tree = new SplayTree<int, int>();           

           values = values_textBox.Text;
           string [] split = values.Split(new Char [] {' ', ',' });

           foreach(string s in split)
           {
               int i = 1;
               try
               {
                   tree.Add(i, Convert.ToInt32(s));
               }
               catch (ArgumentException error)
               {
                   MessageBox.Show("Element with key " + i + " and value " + tree[i] + " already exists.\n"+error);
               }
               i++;
           }

           foreach (KeyValuePair<int, int> kvp in tree)
           {
              MessageBox.Show("\n" +kvp.Key+" "+kvp.Value);
           }

       }

推荐答案

我并不感到惊讶!取出不会导致问题的代码:
I''m not surprised! Taking out the code that doesn''t contribute to the problem:
foreach(string s in split)
{
    int i = 1;
        tree.Add(i, Convert.ToInt32(s));
    i++;
}

尝试将"i"的声明移出循环...

Try moving the declaration of "i" outside the loop...


这篇关于在C#中实现SplayTrees,添加到树结构时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆