Exemplo n.º 1
0
        private static void Test()
        {
            Bst bst = new Bst();

            bst.Insert(2);
            bst.Insert(5);
            bst.Insert(3);
            Console.WriteLine(bst.Exists(2) ? "true" : "false");
            Console.WriteLine(bst.Exists(4) ? "true" : "false");
            Console.WriteLine(bst.Next(4)?.Value.ToString() ?? "none");
            Console.WriteLine(bst.Prev(4)?.Value.ToString() ?? "none");
            bst.Delete(5);
            Console.WriteLine(bst.Next(4)?.Value.ToString() ?? "none");
            Console.WriteLine(bst.Prev(4)?.Value.ToString() ?? "none");
        }
Exemplo n.º 2
0
        public static void Solve()
        {
            using (var input = new StreamReader("bstsimple.in"))
            {
                using (var output = new StreamWriter("bstsimple.out"))
                {
                    Bst bst = new Bst();
                    while (true)
                    {
                        string[] line = input.ReadLine()?.Trim().Split();
                        if (line == null)
                        {
                            break;
                        }
                        switch (line[0])
                        {
                        case "insert":
                            bst.Insert(long.Parse(line[1]));
                            break;

                        case "delete":
                            bst.Delete(long.Parse(line[1]));
                            break;

                        case "exists":
                            output.WriteLine(bst.Exists(long.Parse(line[1])) ? "true" : "false");
                            break;

                        case "next":
                            output.WriteLine(bst.Next(long.Parse(line[1]))?.Value.ToString() ?? "none");
                            break;

                        case "prev":
                            output.WriteLine(bst.Prev(long.Parse(line[1]))?.Value.ToString() ?? "none");
                            break;
                        }
                    }
                }
            }
        }