Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9236887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:24:23+00:00 2026-06-18T07:24:23+00:00

I’m working on a binary search tree console application, specifically, a method to list

  • 0

I’m working on a binary search tree console application, specifically, a method to list the shortest path between two target nodes. My approach is to

1) Create an ArrayList of the values of each target node in the tree from the root node to the target (one ArrayList for each path)
2) Compare the two ArrayLists, removing all duplicates except the last one (which would represent where the two paths branch
3) combine the two remaining ArrayLists into a single array and print to console with a for loop

This is the method I’m working with. The problem I’m having is that I’m never entering the block that reads “if (list1[n] == list2[n])”, even though the ArrayLists are printing out with the values
Contents of _pathArrayList1: 5, 7, 9
Contents of _pathArrayList2: 5, 7, 9, 10, 12, 11

I tried typcasting, but that didn’t help.

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:24:25+00:00Added an answer on June 18, 2026 at 7:24 am

    Please clarify “I tried typcasting, but that didn’t help.”.

    ArrayList does not use generics, so it deals with everything as Objects. Even if the two objects are ints, when they’re typed as Object, == won’t compare the integer values as you expect.

    Object^ o1 = 1;
    Object^ o2 = 1;
    
    bool asObject = (o1 == o2); // false
    bool asInt = ((int)o1 == (int)o2); // true
    

    If you switch out your ArrayList for List<int>, then == will work as you expect, with the added benefits of type-safety.

    Other Notes:

    • Use Math.Min when setting forLoopCount. If other people have to stop and think about it to figure out if the code is correct, for something that simple, use the function.
    • Before checking if list2[n+1] == list1[n+1], make sure there’s an extra item in both lists. In your example data (5-7-9 and 5-7-9-10-12-11), this will give you an exception when list1[n] is the 9.
    • You can remove gcnew array<T>(forLoopCount), or even combineArray entirely. That object is getting thrown away when you assign the return value of combineArrays.

    Edit

    If the objects in the passed lists are the items themselves (not their node addresses, or something else that’s always an int), then I’d recommend adding where T : IEquatable<T> to the generic definition. That gives you an Equals(T) method, and you can use that instead of ==, which isn’t defined for all types. (If you implement this on your types, just be sure to implement all three methods Equals(T), Equals(object), and GetHashCode(), so that everything’s consistent.)

    To help with building the search tree in the first place, you may find IComparable<T> useful. Use the CompareTo method instead of the comparison operators (e.g., <).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.