using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PL
{
class Program
{
static void Main()
{
var tree = new BinaryTree<int>();
Console.WriteLine("Сколько элементов будет в дереве?");
int NumberelElements=Convert.ToInt32(Console.ReadLine());
List<int> elements = new List<int>();
for (int i = 0; i < NumberelElements; i++)
{
Console.WriteLine("Введите элемент ");
elements.Add(Convert.ToInt32(Console.ReadLine()));
}
elements.Sort();
int n = elements.Count / 2;
tree.Add(elements[n]);
for (int i = 0; i < elements.Count; i++)
{
if (i == n)
{
continue;
}
tree.Add(elements[i]);
}
Console.Clear();
tree.Print();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PL
{
internal class Node<T> :IComparable
where T : IComparable
{
public T Data { get; set; }
public Node<T> Left { get; set; }
public Node<T> Right { get; set; }
public Node(T Data)
{
this.Data = Data;
}
public Node(T Data, Node<T> Left, Node<T> Right)
{
this.Data = Data;
this.Left = Left;
this.Right = Right;
}
public Node()
{
}
public int CompareTo(object? obj)
{
if(obj is Node<T> item)
{
return Data.CompareTo(item);
}
else
{
throw new ArgumentException("Типы не совпадают");
}
}
public void Add(T data)
{
var node=new Node<T>(data);
if (node.Data.CompareTo(Data) == -1)
{
if (Left == null)
{
Left = node;
}
else
{
Left.Add(data);
}
}
else
{
if(Right == null)
{
Right = node;
}
else
{
Right.Add(data);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PL
{
internal class BinaryTree<T> : Node<T>
where T : IComparable
{
public Node<T> Root { get; set; }
public int Count { get; set; }
public void Add(T data)
{
if (Root == null)
{
Root = new Node<T>(data);
Count = 1;
return;
}
Root.Add(data);
Count++;
}
public void Print(int left = 50, int top = 0)
{
Console.SetCursorPosition(left, top);
Console.WriteLine(this.Data);
top += 1;
Console.SetCursorPosition(left - 1, top);
Console.Write("/");
Console.SetCursorPosition(left + 1, top);
Console.Write("\\");
if (this.Left != null)
{
Print(left - 3, top + 1);
}
if (this.Right != null)
{
Print(left + 3, top + 1);
}
Console.WriteLine();
}
}
}
public void Add(T data)
{
var node=new Node<T>(data);
if (node.Data.CompareTo(Data) == -1)
// other code…
}
public int CompareTo(object? obj)
{
if(obj is Node<T> item)
{
return Data.CompareTo(item);
}
else
{
throw new ArgumentException("Типы не совпадают");
}