-2
//given array in main class
 
class Program
    
{

        static void Main(string[] args)
        {
            int[] nums = new int[] { 1, 5, 7, 10, 20, 12};
            BinaryTree bt = new BinaryTree();
            
            //what I want to do
            bt.insertElement(45);

        }

   }

// don't mind that the class says binarytree I just want to know how to insert a number in an array

public class BinaryTree

{
   
    public void insertElement(int num)
     {

        //not sure how to go about it

     }

}
2
  • You cant insert element to an array. Why dont you use just List<T>? If you have to or want to use array, you should create a list from existing array then add new element to List then convert it back to array. But why do you even need this? Commented Mar 5, 2021 at 23:01
  • it's part of my assignment where my teacher wants it where a user can insert a new value into an array then use the numbers of that array to insert that into a binary tree then print out the values using in-order traversal Commented Mar 5, 2021 at 23:29

1 Answer 1

0

try this:

class Program

{

    static void Main(string[] args)
    {
        int[] nums = new int[] { 1, 5, 7, 10, 20, 12 };

        insertElement(ref nums, 45);

    }

    public static void insertElement(ref  int[] nums, int num)
    {
        var temp = nums.ToList();
        temp.Add(45);
        nums = temp.ToArray();
        }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.