Question

I have written a code in C# for the implementation of AVL_trees. I am having some problems with nodes that's why I am unable to insert data in the nodes. Below is my code.

public class avl_node
{
    public int Data;
    public avl_node Left;
    public avl_node Right;
    public int height;

    public void DisplayNode()
    {
        Console.Write("{0}", Data);
    }
}



public class avl_tree
{
    public avl_node root;

    public avl_tree()
    {
        root = null;
    }

    public void Insert(int i)
    {
        avl_node newNode = new avl_node();
        newNode.Data = i;
        newNode.height = newNode.height + 1;
        if (root == null)
        {
            root = newNode;
        }
        else
        {
            avl_node current = root;
            avl_node parent;
            while (true)
            {
                parent = current;
                if (i < current.Data)
                {
                    current = current.Left;
                    if (current == null)
                    {
                        parent.Left = newNode;
                        break;
                    }
                    else
                    {
                        current = current.Right;
                        if (current == null)
                        {
                            parent.Right = newNode;
                            break;
                        }
                    }
                }
            }
        }
    }


    public void InOrder(avl_node node)
    {
        if (!(node == null))
        {
            InOrder(node.Left);
            node.DisplayNode();
            InOrder(node.Right);
        }
    }
 }



class Program
{
    static void Main(string[] args)
    {
        avl_tree nums = new avl_tree();
        nums.Insert(23);
        nums.Insert(45);
        nums.Insert(16);
        nums.Insert(37);
        nums.Insert(3);
        nums.Insert(99);
        nums.Insert(22);
        avl_node nd = new avl_node();
        nd = nums.Search(37);

        Console.WriteLine("Inorder traversal: ");
        nums.InOrder(nums.root);
    }
}

All I get is a black console screen. I am very confused.

Hoping for a better response.

Regards Umer

Was it helpful?

Solution

"All I get is a black console screen"

After 23 is inserted, your Insert() method gets stuck in the while loop because 45 is never less than 23:

        while (true)
        {
            parent = current;
            if (i < current.Data)
                // you never get in here, so we just loop around in "while (true)"  

OTHER TIPS

Take a look at that loop in the insert method, you will get stuck in the loop everytime you try to insert any value greater than existing values already in the tree.

It will happen because of this conditional: if(i < current.data). Where is the else statement? You put it inside the scope of the mentioned conditional. So it will never be reached, thus the program will run the infinite loop.

You should put an "}" before the else statement, so that else will be outside the scope of the first if statement. And Remove one of the "}" at the end of the method.

That way it should run just fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top