jeudi 2 juillet 2015

Recursive Insertion in a Binary Tree C#

Why reason the member variables LEFT and RIGHT never change when i make the recursive call? Here's the Source Code:

public class C_Nodo

{
    int dato;
    C_Nodo left;
    C_Nodo right;

    public int DATO
    {
        get { return dato; }
        set { dato = value; }
    }

    public C_Nodo LEFT
    {
        get { return this.left; }
        set { this.left= value; }
    }

    public C_Nodo RIGHT
    {
        get { return this.right; }
        set { this.right = value; }
    }

    public C_Nodo(int inf)
    {
        this.dato = inf;
        this.left = null;
        this.right = null;
    }
}

public class C_Arbol_Bin

{   
    C_Nodo root;

    public C_Arbol_Bin()
    {
        root = null;
    }

Simple insertion in the root or make the recursive call

    public void inserta(int dat)
    {
        if (root == null)
        {
            root = new C_Nodo(dat);
        }
        else
        {
            insert_Order(this.root, dat);
        }
    }

Here i make the recursive insertion in ordered way depending of the value that contains the father node but RIGH and LEFT never change.

    public void insert_Order(C_Nodo tree, int inf)
    {
        if (tree == null)
        {
            tree = new C_Nodo(inf);
        }
        else
        {
            if (tree.DATO > inf)
            {
                insert_Order(tree.LEFT, inf);
            }
            else
            {
                insert_Order(tree.RIGHT, inf);
            }
        }
    }
}

Thanks for any help.

Aucun commentaire:

Enregistrer un commentaire