Home > Blockchain >  C# Binary Tree, Max and Min Value
C# Binary Tree, Max and Min Value

Time:01-03

I have write a code and I have some problems but most important question is.... Why array is not working with AddNode. I know I'm close to solving this problem, but I hope for a little hint. Next question. Code for Min and Max value is good and how can i move it to Class Node? And last question. How to make Class Depth for tree?

class Node
{
    public Node LeftNode { get; set; }
    public Node MiddleNode { get; set; }
    public Node RightNode { get; set; }

    public int Value { get; set; }

    public void AddNode(int value)
    {
        if (value < this.Value)
        {
            if (LeftNode != null)
            {
                LeftNode.AddNode(value);
                return;
            }
            LeftNode = new Node(value);
            return;
        }

        if (value > Value)
        {
            if (RightNode != null)
            {
                RightNode.AddNode(value);
                return;
            }
            RightNode = new Node(value);
            return;
        }

        if (MiddleNode != null)
        {
            MiddleNode.AddNode(value);
            return;
        }
        MiddleNode = new Node(value);
    }

    public Node(int value)
    {
        this.Value = value;
    }

    public override string ToString()
    {
        return $"Value: {Value}";
    }

    public string SortedString(Array arr)
    {
       Array.Sort(arr);
       foreach (int val in arr)
       {
           Console.WriteLine(val);
       }
       return "";
    }

    public int ValueCount(int value)
    {
        if (value < Value)
        {
            if (LeftNode == null)
            {
                return -1;
            }
            return LeftNode.ValueCount(value);
        }

        if (value > Value)
        {
            if (RightNode == null)
            {
                return -1;
            }
            return RightNode.ValueCount(value);
        }

        if (MiddleNode != null)
        {
            return 1   MiddleNode.ValueCount(value);
        }
        return 1;
    }

    internal int Next(int min, int max)
    {
        Random rnd = new Random();
        return rnd.Next(min, max);
    }
}
class Program
{
    static void Main(string[] args)
    {

        var arr = new int[1000];
        var rnd = new Node(1);
        for (int i = 0; i < arr.Length; i  )
        {
            rnd.AddNode(arr[i]);
            //Console.WriteLine(arr[i]);
        }
        for(int i = 0; i < arr.Length; i  )
        {
            arr[i] = rnd.Next(1, 100);
            Console.WriteLine(arr[i]);
        }
        min = arr[0];
        max = arr[0];
        for (int i = 1; i < arr.Length; i  )
        {
            if (min > arr[i])
                min = arr[i];
            if (max < arr[i])
                max = arr[i];
        }
        Console.WriteLine("największa liczba"   " "   max);
        Console.WriteLine("najmniejsza liczba"   " "   min);

        Array.Sort(arr);
        foreach (int value in arr)
        {
            Console.WriteLine(value);
        }

        Console.WriteLine(rnd.ValueCount(6));
        Console.WriteLine(rnd.ToString());

    }
}

CodePudding user response:

To be honest - your code is a bit confusing. first of all - why do you have a pointer (ref) to a middle node ? A binary tree defines each node to have at most 2 children (left \ right). If you wish to handle duplicate values you can either choose a strategy that defines one of the children to also includes equals, or better use ref count (e.g.: each node has both a value and a counter for this value). re Min \ Max - if your tree is a BST (which it seems to be the case), you can easily implement this through simple tree traversing either to the left (for Min value) or to the right (for max value).

  • Related