Untitled

 avatar
unknown
plain_text
a year ago
3.5 kB
5
Indexable
using System;

public class BinaryTree
{
    private int Value;
    private BinaryTree Left;
    private BinaryTree Right;

    public BinaryTree()
    {
        Left = null;
        Right = null;
    }

    public void CreateFromString(string data)
    {
        int currentIndex = 0;
        Value = CharToInt(data[0]);

        BinaryTree current = this;

        for (int i = 1; i < data.Length; i++)
        {
            if (CharToInt(data[i]) >= 0 && CharToInt(data[i]) <= 9)
            {
                if (CharToInt(data[i - 1]) >= 0 && CharToInt(data[i - 1]) <= 9)
                {
                    current.Left = new BinaryTree();
                    current = current.Left;
                }
                current.Value = CharToInt(data[i]);
                continue;
            }

            if (data[i] == 'L')
            {
                current.Left = new BinaryTree();
                current = current.Left;
                continue;
            }

            if (data[i] == 'P')
            {
                current.Right = new BinaryTree();
                current = current.Right;
                continue;
            }

            if (data[i] == '*')
            {
                if (data[i + 1] == '*')
                {
                    continue;
                }
                if (CharToInt(data[i - 1]) >= 0 && CharToInt(data[i - 1]) <= 9)
                {
                    current.Right = new BinaryTree();
                    current = current.Right;
                    continue;
                }
                continue;
            }
        }
    }

    private int CharToInt(char x)
    {
        return (int)x - 48;
    }

    public void PreOrder()
    {
        Console.Write($"{Value} ");

        if (Left != null)
        {
            Console.Write("L");
            Left.PreOrder();
        }
        else
        {
            Console.Write("*L");
        }

        if (Right != null)
        {
            Console.Write("P");
            Right.PreOrder();
        }
        else
        {
            Console.Write("*P");
        }
    }

    public void InOrder()
    {
        if (Left != null) Left.InOrder();

        Console.Write($"{Value} ");

        if (Right != null) Right.InOrder();
    }

    public void PostOrder()
    {
        if (Left != null) Left.PostOrder();
        if (Right != null) Right.PostOrder();

        Console.Write($"{Value} ");
    }

    public void Delete()
    {
        if (Left != null) Left.Delete();
        if (Right != null) Right.Delete();

        Left = null;
        Right = null;
    }

    static void Main(string[] args)
    {
        BinaryTree test = new BinaryTree();

        Console.WriteLine("Podaj ciąg tekstowy reprezentujący drzewo:");
        string input = Console.ReadLine();

        test.CreateFromString(input);

        Console.WriteLine("Metoda PreOrder:");
        test.PreOrder();
        Console.WriteLine();

        Console.WriteLine("Metoda InOrder:");
        test.InOrder();
        Console.WriteLine();

        Console.WriteLine("Metoda PostOrder:");
        test.PostOrder();
        Console.WriteLine();

        Console.WriteLine("Usuwanie drzewa:");
        test.Delete();
        Console.WriteLine("Drzewo usunięte.");

        Console.ReadKey();
    }
}
Editor is loading...
Leave a Comment