コード例 #1
0
        public void Insert(T data)
        {
            var actualValue = CompareFunc(Data.First());
            var newValue    = CompareFunc(data);

            if (actualValue.CompareTo(newValue) == 0)
            {
                Data.Add(data);
            }

            if (newValue.CompareTo(actualValue) > 0)
            {
                if (Right == null)
                {
                    Right = new BinaryTreeList <T>(data, CompareFunc);
                }
                else
                {
                    Right.Insert(data);
                }
            }
            else
            {
                if (Left == null)
                {
                    Left = new BinaryTreeList <T>(data, CompareFunc);
                }
                else
                {
                    Left.Insert(data);
                }
            }
        }
コード例 #2
0
        public static BinaryTreeList <T> FromArray(T[] array, Func <T, IComparable> compareFunc)
        {
            BinaryTreeList <T> root = null;

            foreach (var item in array)
            {
                if (root == null)
                {
                    root = new BinaryTreeList <T>(item, compareFunc);
                }
                else
                {
                    root.Insert(item);
                }
            }
            return(root);
        }