I was trying to create a recursive function to delete a node of a Binary Search Tree using Kotlin language. During the same I encountered this compile time error saying "Val cannot be reassigned.". I see from similar questions here that parameters of Kotlin are final, that is, they are 'val' and not 'var' in the language of Kotlin. However I want to know if there is still a way to achieve this and be able to assign values to it. I am sharing the code and commenting the ahead of the line where this error is happening.
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KMutableProperty0
data class Node(var value:Int?){
    var data:Int? = value
    var left:Node? = null
    var right:Node? = null
}
fun insert(root:Node?, value:Int?):Node{
    var root = root
    if(root==null){
        root = Node(value)
        return root
    }
    value?.let {
        if (root.data!! > value) {
            root.left = insert(root.left, value)
        } else {
            root.right = insert(root.right, value)
        }
    }
    return root
}
fun inorder(root: Node?){
    if(root == null ) return
    root.data?.let {
        inorder(root.left)
        print("${root.data} ")
        inorder(root.right)
    }
}
fun srchDelete(root: Node?, key: Int){
    if(root == null){
        return
    }
    else if (root.data == key){
        if(root.left == null && root.right == null){
            root.data = null
            return
        }
        if(root.left != null && root.right == null){
            root = root.left //Error Here **"Val cannot be reassigned"**
        }
    }
    else {
        srchDelete(root.left, key)
        srchDelete(root.right, key)
    }
}
fun main(){
    var values = arrayOf(5,1,3,4,2,7)
    var root:Node? = null
    for(i in values){
        root = insert(root, i)
    }
    srchDelete(root, 2)
    inorder(root)
}
How can I be able to assign values in the root of type 'Node?' ?
If I put a simple code to explain my problem further. I am adding Java code that allows me to assign in function parameter and similar Kotlin code that doesn't allow me the same.
import java.util.stream.IntStream;
public class DeleteNode {
    static int[] fun (int[] arr){
        arr[0] = 1;
        arr[1] = 9;
        arr[7] = 1;
        arr = new int[9]; //Java Allows Me
        arr[0] = 9;
        return arr;
    }
    public static void main(String[] args) {
        int[] arr = new int[8];
        int[] array = fun(arr);
        IntStream.range(0, arr.length).forEach(h -> System.out.print(" " + arr[h]));
        System.out.println();
        IntStream.range(0, array.length).forEach(h -> System.out.print(" " + array[h]));
    }
}
This Java code allowed me to assign using 'new' keyword in 'arr', now similar Kotlin code.
fun list(arr:IntArray):IntArray{
    arr[0] = 1
    arr[8] = 1
    arr = IntArray(8){0} //Kotlin Doesn't Allow Me
    arr[0] = 1
    return arr
}
fun main(){
    var arr = IntArray(9){0}
    var array = list(arr)
    for(value in arr) print("$value ")
    for(value in array) print("$value ")
}
Is there a way to make Kotlin allow me to do the same too?
