Пример #1
0
 public void ArrayHelperBinarySearch()
 {
     int[] numbers = { -7, -2, 1, 4, 7, 8, 100, 120 };
     Assert.IsTrue(numbers.BinarySearch(-2), "number -2 IS contained in the sorted list of integers");
     Assert.IsFalse(numbers.BinarySearch(2), "number 2 IS NOT contained in the sorted list of integers");
     Assert.IsFalse(ArrayHelper.BinarySearch(null, 2), "number 2 IS NOT contained in null (or is it?)");
 }
Пример #2
0
        public void BinarySearchTest(string value, int exp)
        {
            string[] arrIn = { "a", "ab", "bc", "bc", "cd" };

            var actual = ArrayHelper.BinarySearch <string>(arrIn, value);

            Assert.AreEqual(exp, actual);
        }
Пример #3
0
        public override bool AddChild(BaseNode <V> node)
        {
            var add = false;

            if (child == null)
            {
                child = new BaseNode <V> [0];
            }

            int index = ArrayHelper.BinarySearch(child, node);

            if (index >= 0)                // 如果已经存在,则分情况做修改
            {
                var target = child[index]; // 已经存在的目标对象
                switch (node.Status)
                {
                case Status.UNDEFINED:          // 这其实是一种软删除方式
                    if (target.Status != Status.NOT_WORD)
                    {
                        target.Status = Status.NOT_WORD;
                        target.Value  = default(V);
                        add           = true;
                    }
                    break;

                case Status.NOT_WORD:       // 要添加的节点不是词语结尾,那么已存在的节点如果是词语结尾,则修改状态为词语中间,并且还可以继续
                    if (target.Status == Status.WORD_END)
                    {
                        target.Status = Status.WORD_MIDDLE;
                    }
                    break;

                case Status.WORD_END:
                    if (target.Status != Status.WORD_END)
                    {
                        target.Status = Status.WORD_MIDDLE;
                    }

                    if (target.Value == null)
                    {
                        add = true;
                    }
                    target.Value = node.Value;
                    break;
                }
            }
            else
            {
                var newChild  = new BaseNode <V> [child.Length + 1];
                var insertIdx = -index - 1;
                Array.Copy(child, 0, newChild, 0, insertIdx);
                newChild[insertIdx] = node;
                Array.Copy(child, insertIdx, newChild, insertIdx + 1, child.Length - insertIdx);
                child = newChild;
                add   = true;
            }
            return(add);
        }
Пример #4
0
        static void Main(string[] args)
        {
            Console.Write("1-binary search, 2-Fibonacci numbers, 3-stack, 0-exit:");
            if (!int.TryParse(Console.ReadLine(), out int choice))
            {
                Console.WriteLine("This is not an integer");
                Console.ReadLine();
                return;
            }

            if (choice == 1)
            {
                Console.WriteLine("Enter the string elements of the array in sorted form for binary search. The space is a separator:");
                var input    = Console.ReadLine();
                var inputArr = input.Split(" ");
                Console.WriteLine("Enter an item to search for: ");
                var value = Console.ReadLine();

                var actual = ArrayHelper.BinarySearch <string>(inputArr, value);
                Console.WriteLine($"The index of the element: {actual}");
            }
            if (choice == 2)
            {
                int       i   = 0;
                Fibonacci fib = new Fibonacci();
                Console.WriteLine("Enter the output number of Fibonacci numbers: ");
                int.TryParse(Console.ReadLine(), out int valueInt);

                foreach (var item in fib)
                {
                    if (i == valueInt)
                    {
                        break;
                    }
                    Console.WriteLine($"{item} ");
                    i++;
                }
            }

            if (choice == 3)
            {
                Console.WriteLine($"Demonstration of stack operation:");
                MyStack <string> numbers = new MyStack <string>();
                numbers.Push("one");
                numbers.Push("two");
                numbers.Push("three");
                numbers.Push("four");
                numbers.Push("five");

                foreach (string number in numbers)
                {
                    Console.WriteLine(number);
                }
                Console.WriteLine($"{Environment.NewLine}Executing the Pop method '{numbers.Pop()}'");
                Console.WriteLine($"Executing the Peek method: {numbers.Peek()}");
                Console.WriteLine($"Executing the Pop method '{numbers.Pop()}'");
                Console.WriteLine($"The status of stack after execution of methods: ");
                foreach (string number in numbers)
                {
                    Console.WriteLine(number);
                }
            }
        }