Generics 通用二叉搜索树

Generics 通用二叉搜索树,generics,c++-cli,binary-search-tree,Generics,C++ Cli,Binary Search Tree,我正在开发一个二进制搜索树控制台应用程序,特别是一个列出两个目标节点之间最短路径的方法。我的方法是 1) 创建树中从根节点到目标的每个目标节点的值的ArrayList(每个路径一个ArrayList) 2) 比较两个ArrayList,删除除最后一个(表示两条路径分支的位置)之外的所有重复项 3) 将剩下的两个ArrayList组合成一个数组,并使用for循环打印到控制台 这就是我正在使用的方法。我遇到的问题是,我从未输入读取“if(list1[n]==list2[n])”的块,即使ArrayL

我正在开发一个二进制搜索树控制台应用程序,特别是一个列出两个目标节点之间最短路径的方法。我的方法是

1) 创建树中从根节点到目标的每个目标节点的值的ArrayList(每个路径一个ArrayList) 2) 比较两个ArrayList,删除除最后一个(表示两条路径分支的位置)之外的所有重复项 3) 将剩下的两个ArrayList组合成一个数组,并使用for循环打印到控制台

这就是我正在使用的方法。我遇到的问题是,我从未输入读取“if(list1[n]==list2[n])”的块,即使ArrayList正在打印值 _pathArrayList1的内容:5、7、9 _pathArrayList2的内容:5,7,9,10,12,11

我试过了,但没用

array<T>^ removeDuplicates(ArrayList^ list1, ArrayList^ list2)
{
    int forLoopCount;
    int i; // for loop iterator for this method
    Console::WriteLine(L"Contents of _pathArrayList1: ");
    for (i = 0; i < list1->Count; i++)
        Console::WriteLine(list1[i]);

    Console::WriteLine(L"Contents of _pathArrayList2"); 
    for (i = 0; i < list2->Count; i++)
        Console::WriteLine(list2[i]);

    // find out which array is the shortest; we need to use the shorter of the two
    if (list1->Count - list2->Count < 0)
        forLoopCount = list1->Count;
    else
        forLoopCount = list2->Count;
    Console::WriteLine("Value of forLoopCopunt is " + forLoopCount);
    array<T>^ combineArray = gcnew array<T>(forLoopCount);

    for (int n = 0; n < forLoopCount; n++)
    {
        Console::WriteLine(L"List1[n] = " + list1[n]);
        Console::WriteLine(L"list2[n] = " + list2[n]);

        if (list1[n] == list2[n])  // never entering this block of code
        {
            if (list2[n+1] == list1[n+1])
            {
                Console::WriteLine(L"Removing " + list1[n] + " and " + list2[n]);
                list1->RemoveAt(n);
                list2->RemoveAt(n);
                n --;
            }
            else
            {
                Console::WriteLine(L"Deleting " + list1[n]);
                list1->RemoveAt(n);
                //_pathArrayList1->Reverse();
                return combineArray = combineArrays(_pathArrayList1, _pathArrayList2);
            }
        }
    }
    return combineArray = combineArrays(_pathArrayList1, _pathArrayList2);
}
array^removeDuplicates(ArrayList^list1,ArrayList^list2)
{
整数表示循环计数;
int i;//用于此方法的循环迭代器
控制台::WriteLine(L“路径列表1的内容”);
对于(i=0;iCount;i++)
控制台::WriteLine(列表1[i]);
控制台::WriteLine(L“路径列表的内容2”);
对于(i=0;iCount;i++)
控制台::WriteLine(列表2[i]);
//找出哪个数组最短;我们需要使用两个数组中较短的一个
如果(列表1->计数-列表2->计数<0)
forLoopCount=list1->Count;
其他的
forLoopCount=list2->Count;
控制台::WriteLine(“forloopcopount的值为”+forLoopCount);
数组^Combinarray=gcnew数组(forLoopCount);
for(int n=0;nRemoveAt(n);
列表2->RemoveAt(n);
n--;
}
其他的
{
控制台::WriteLine(L“Deleting”+list1[n]);
列表1->RemoveAt(n);
//_pathArrayList1->Reverse();
返回combineArray=combineArray(_-pathArrayList1,_-pathArrayList2);
}
}
}
返回combineArray=combineArray(_-pathArrayList1,_-pathArrayList2);
}
请澄清“我尝试了typcasting,但没有帮助。”

ArrayList
不使用泛型,因此它将所有内容都作为对象处理。即使这两个对象是int,当它们被键入为Object时,
==
也不会像您期望的那样比较整数值

Object^ o1 = 1;
Object^ o2 = 1;

bool asObject = (o1 == o2); // false
bool asInt = ((int)o1 == (int)o2); // true
如果您为
列表
关闭
阵列列表
,则
==
将按预期工作,并具有额外的类型安全性好处

其他说明:

  • 设置
    for loopcount
    时使用。如果其他人不得不停下来想一想代码是否正确,那么对于这么简单的事情,使用函数
  • 在检查列表2[n+1]==list1[n+1]之前,请确保两个列表中都有额外的项。在您的示例数据(5-7-9和5-7-9-10-12-11)中,当
    list1[n]
    是9时,这将为您提供一个异常
  • 您可以删除
    gcnewarray(forLoopCount)
    ,甚至可以完全删除
    combineArray
    。当您指定combineArrays的返回值时,该对象将被丢弃

编辑 如果传递的列表中的对象是项本身(不是它们的节点地址,或者总是
int
),那么我建议将
其中T:IEquatable
添加到通用定义中。这为您提供了一个方法,您可以使用它来代替
==
,它不是为所有类型定义的。(如果在类型上实现此功能,请确保实现所有三个方法Equals(T)、Equals(object)和GetHashCode(),以便所有方法都是一致的。)


若要首先帮助构建搜索树,您可能会发现有用的。使用CompareTo方法而不是比较运算符(例如,
谢谢David,这确实解决了我的问题。通过“类型转换”,我的意思是我尝试了“if((T)list1[n]=(T)list2[n]{}”,但我不确定如何在通用环境中使用此方法,在该环境中可以使用其他对象(如字符串、记录等).我错过了一个更大的画面吗?