示例#1
0
 /*
  * Add to the end, set its value to smallest possible, and then call increase key on it with
  * the actual value.
  */
 public void Insert(Algorithms.Item newItem)
 {
     size++;
     pq.Add(new Algorithms.Item());
     pq[size - 1] = new Algorithms.Item()
     {
         number = int.MinValue, order = -1
     };
     IncreaseKey(size - 1, newItem.number);
 }
示例#2
0
        public Algorithms.Item ExtractMax()
        {
            if (size < 1)
            {
                throw new Exception("Heap Underflow");
            }

            Algorithms.Item max = Max;
            Max = pq[size - 1];
            size--;
            MaxHeapify(0);

            return(max);
        }
示例#3
0
 public void IncreaseKey(int i, int key)
 {
     if (pq[i].number > key)
     {
         throw new Exception("new value must be greater");
     }
     pq[i] = new Algorithms.Item()
     {
         number = key, order = pq[i].order
     };
     while (i > 0 && pq[GetParentIndex(i)].number < pq[i].number)
     {
         Algorithms.Item temp = pq[i];
         pq[i] = pq[GetParentIndex(i)];
         pq[GetParentIndex(i)] = temp;
         i = GetParentIndex(i);
     }
 }
示例#4
0
        private void MaxHeapify(int i)
        {
            int l   = GetLeftChildIndex(i);
            int r   = GetRightChildIndex(i);
            int max = i;

            if (l < size && pq[l].number > pq[max].number)
            {
                max = l;
            }
            if (r < size && pq[r].number > pq[max].number)
            {
                max = r;
            }

            if (i != max)
            {
                Algorithms.Item temp = pq[i];
                pq[i]   = pq[max];
                pq[max] = temp;
                MaxHeapify(max);
            }
        }
示例#5
0
        static void Main(string[] args)
        {
            //HashTable ht = new HashTable(20);
            //ht.Insert(10, "10");
            //ht.Insert(30, "10 - 2");
            //ht.Search(12);
            //string s = "Hello";

            //int[] l = Enumerable.Range(1, 100).ToArray<int>().Reverse<int>().ToArray();
            Algorithms.Item[] items = new Algorithms.Item[] {
                new Algorithms.Item {
                    number = 4, order = 1
                },
                new Algorithms.Item {
                    number = 1, order = 2
                },
                new Algorithms.Item {
                    number = 6, order = 3
                },
                new Algorithms.Item {
                    number = 4, order = 4
                },
                new Algorithms.Item {
                    number = 2, order = 5
                },
                new Algorithms.Item {
                    number = 11, order = 6
                },
                new Algorithms.Item {
                    number = 6, order = 7
                },
                new Algorithms.Item {
                    number = 7, order = 8
                },
                new Algorithms.Item {
                    number = 5, order = 9
                },
                new Algorithms.Item {
                    number = 3, order = 10
                }
            };

            //Sorts.QuickSort(items, 0, items.Length - 1);

            //Sorts.MergeSort(items, 0, items.Length - 1);

            //Sorts.HeapSort(items);

            /*
             * PriorityQueue pq = new PriorityQueue();
             * foreach (var i in items)
             * {
             *  pq.Insert(i);
             * }
             *
             * pq.IncreaseKey(pq.size - 1, 23);
             * pq.ExtractMax();
             * pq.Delete(3);
             *
             * foreach (Algorithms.Item i in items)
             * {
             *  Console.WriteLine(i.number + " - " + i.order);
             * }
             */
            /*
             * BinarySearchTree tree = new BinarySearchTree();
             * tree.AddIterative(6);
             * tree.AddIterative(5);
             * tree.AddIterative(7);
             * tree.AddIterative(2);
             * tree.AddIterative(5);
             * tree.AddIterative(8);
             * tree.AddIterative(1);
             * tree.InorderTraversal();
             * Console.WriteLine("minimum is " + tree.Minimum.data);
             * Console.WriteLine("maximum is " + tree.Maximum.data);
             * while (1 == 1)
             * {
             *  int keyToSearch = int.Parse(Console.ReadLine());
             *  Console.WriteLine(tree.IterativeSearch(keyToSearch) == null ? "False" : "True");
             * }
             */
            BinarySearchTree tree = new BinarySearchTree();

            tree.ReconstructFromPreorderTraversal(new int[] { 50, 39, 28, 18, 38, 47, 40, 48, 90, 80, 100 });
            //tree.root = tree.construct(new int[] { 50, 39, 28, 18, 38, 47, 40, 48, 90, 80, 100 },0,null);
            //tree.root = tree.construct(new int[] { 50, 40,30,45,80,70,90 }, 0, null);
            tree.IterativePreorderTraversal(tree.root);
            Console.WriteLine();
            tree.printTree(tree.root);
            //tree.InorderTraversal();
            //Node lca = tree.GetLowestCommonAncestor(8, 48, tree.root, null);

            //LinkedList ll = new LinkedList();
            //ll.Flatten(ll.root, null);
            //ll.Print();
        }