Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Generics
- {
- public class BinaryTree<T> : IEnumerable<T>
- {
- public T Value { get; private set; }//значение в ячейках
- private bool isFool = false;//добавлен ли
- public BinaryTree<T> Left { get; private set; }
- public BinaryTree<T> Right { get; private set; }
- public void Add(T value)
- {
- if (!isFool)
- {
- Value = value;
- isFool = true;
- }
- else
- {
- AddNextTree(value);
- }
- }
- private void AddNextTree(T value)
- {
- if (value.GetHashCode() <= Value.GetHashCode())
- {
- if (Left == null)
- Left = new BinaryTree<T>();
- Left.Add(value);
- }
- if (value.GetHashCode() > Value.GetHashCode())
- {
- if (Right == null)
- Right = new BinaryTree<T>();
- Right.Add(value);
- }
- }
- public IEnumerator<T> GetEnumerator()
- {
- if (Left != null)
- foreach (var left in Left)
- {
- if (Left.isFool)
- yield return left;
- }
- if (isFool)
- yield return Value;
- if (Right != null)
- foreach (var right in Right)
- {
- if (Right.isFool)
- yield return right;
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment