Exemple #1
0
        public void Main()
        {
            int  value, value2;
            Heap theHeap = new Heap(31);
            bool success;

            theHeap.Insert(70);
            theHeap.Insert(40);
            theHeap.Insert(50);
            theHeap.Insert(20);
            theHeap.Insert(60);
            theHeap.Insert(100);
            theHeap.Insert(80);
            theHeap.Insert(30);
            theHeap.Insert(10);
            theHeap.Insert(90);

            var choise = "s";  // s-enter, i-insert, r-remove, c-change priority

            switch (choise)
            {
            case "s":
                theHeap.Display();
                break;

            case "i":
                var newValue = (new Random()).Next(1, 100);
                success = theHeap.Insert(newValue);
                if (!success)
                {
                    Console.WriteLine("Can't insert: the heap is full");
                    break;
                }
                break;

            case "r":
                if (!theHeap.IsEmpty())
                {
                    theHeap.Remove();
                }
                else
                {
                    Console.WriteLine("Can't remove: the heap full");
                }
                break;

            case "c":
                value   = (new Random()).Next(1, 5);
                value2  = (new Random()).Next(2, 3);
                success = theHeap.Change(value, value2);
                if (!success)
                {
                    Console.WriteLine("Invalid index");
                }
                break;

            default:
                break;
            }
        }
Exemple #2
0
        static void Main(string[] args)
        {
            try
            {
                Heap h   = new Heap();
                int  ans = 0;
                do
                {
                    Console.Clear();
                    h.Display();
                    Console.WriteLine();
                    Console.WriteLine("1. Add to heap");
                    Console.WriteLine("2. Pop from heap");
                    Console.WriteLine("3. Check Top Element");
                    if (!int.TryParse(Console.ReadLine(), out ans))
                    {
                        throw new Exception("Only Int Allowed");
                    }
                    int element;
                    switch (ans)
                    {
                    case 1:
                        Console.WriteLine("Enter Element");
                        if (!int.TryParse(Console.ReadLine(), out element))
                        {
                            throw new Exception("Only Int Allowed");
                        }
                        h.AddToHeap(element);
                        break;

                    case 2:
                        h.DeleteMinElement();
                        break;

                    case 3:
                        Console.WriteLine(h.Top());
                        Console.ReadKey();
                        break;

                    default: break;
                    }
                } while (ans != -1);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
            }
        }
Exemple #3
0
        static void Main(string[] args)
        {
            var h = new Heap(20);

            int choice, value;

            while (true)
            {
                Console.WriteLine("1. Insert");
                Console.WriteLine("2. Delete");
                Console.WriteLine("3. Display");
                Console.WriteLine("4. Exit");
                Console.Write("Enter your choice : ");
                choice = Convert.ToInt32(Console.ReadLine());

                if (choice == 4)
                {
                    break;
                }

                switch (choice)
                {
                case 1:
                    Console.Write("Enter the value to be inserted : ");
                    value = Convert.ToInt32(Console.ReadLine());
                    h.Insert(value);
                    break;

                case 2:
                    Console.WriteLine($"Maximum value is {h.DeleteRoot()}");
                    break;

                case 3:
                    h.Display();
                    break;

                default:
                    Console.WriteLine("You have entered an invalid choice!");
                    break;
                }
            }
        }
        static void Main(string[] args)
        {
            Heap heap = new Heap();

            heap.Insert(5);
            heap.Insert(45);
            heap.Insert(1);
            heap.Insert(90);
            heap.Display();

            int disp = heap.Remove();

            Console.WriteLine($"Removed:{disp}");

            heap.Display();

            disp = heap.Remove();
            Console.WriteLine($"Removed:{disp}");

            heap.Display();

            disp = heap.Remove();
            Console.WriteLine($"Removed:{disp}");

            heap.Display();

            disp = heap.Remove();
            Console.WriteLine($"Removed:{disp}");

            heap.Display();

            disp = heap.Remove();
            Console.WriteLine($"Removed:{disp}");

            heap.Display();

            disp = heap.Remove();
            Console.WriteLine($"Removed:{disp}");

            heap.Display();
        }