Пример #1
0
 public Tree2D(Tree2D parent, Vector2 value) : base(value)
 {
     _parent   = parent;
     _children = new Node <Vector2> [2];
     _level    = _parent._level + 1;
     _id       = Instancecounter++;
 }
Пример #2
0
        // Filtering on predicate
        public static List <Vector2> FilterTree(Tree2D tree, Predicate <Vector2> p)
        {
            var result = new List <Vector2>();

            fill_with_filtered(ref result, tree, p);
            return(result);
        }
Пример #3
0
 private static void fill_with_filtered(ref List <Vector2> fillme, Tree2D tree, Predicate <Vector2> p)
 {
     if (p.Invoke(tree.Value))
     {
         fillme.Add(tree.Value);
     }
     foreach (var child in tree.Children.Where(child => child != null))
     {
         fill_with_filtered(ref fillme, (Tree2D)child, p);
     }
 }
Пример #4
0
 private bool Insert(Tree2D tree, bool horizontal)
 {
     tree._parent = this;
     tree._level  = _level + 1;
     if (horizontal)
     {
         if (tree.Value.X < Value.X)
         {
             if (_children[0] == null)
             {
                 tree.RelativeToParent = Relativity.LEFT;
                 _children[0]          = tree;
                 return(true);
             }
             ((Tree2D)_children[0]).Insert(tree, !horizontal);
         }
         else
         {
             if (_children[1] == null)
             {
                 tree.RelativeToParent = Relativity.RIGHT;
                 _children[1]          = tree;
                 return(true);
             }
             ((Tree2D)_children[1]).Insert(tree, !horizontal);
         }
     }
     else
     {
         if (tree.Value.Y < Value.Y)
         {
             if (_children[0] == null)
             {
                 tree.RelativeToParent = Relativity.LEFT;
                 _children[0]          = tree;
                 return(true);
             }
             ((Tree2D)_children[0]).Insert(tree, !horizontal);
         }
         else
         {
             if (_children[1] == null)
             {
                 tree.RelativeToParent = Relativity.RIGHT;
                 _children[1]          = tree;
                 return(true);
             }
             ((Tree2D)_children[1]).Insert(tree, !horizontal);
         }
     }
     return(false);
 }
Пример #5
0
        // For 'casting' the IEnumerable in ex2
        public static Tree2D FromEnumerable(IEnumerable <Vector2> enumerable)
        {
            var tree = new Tree2D(Vector2.Zero);

            for (int i = 0; i < enumerable.Count(); i++)
            {
                var vector = enumerable.ElementAt(i);
                if (i > 0)
                {
                    tree.Insert(new Tree2D(vector));
                }
                else
                {
                    tree = new Tree2D(vector);
                }
            }
            return(tree);
        }
Пример #6
0
 public bool Insert(Tree2D tree)
 {
     return(Insert(tree, true));
 }