示例#1
0
        private static Heap.Heap GetLevelWiseNodes(TreeNode root, int level)
        {
            if (root == null)
            {
                return(null);
            }

            Heap.Heap heapResult = new Heap.Heap(Convert.ToInt32(Math.Pow(2, level - 1)), Heap.Heap.Type.Min);
            if (level == 1)
            {
                heapResult.Insert(root.Data);
            }
            else if (level > 1)
            {
                Heap.Heap subResult1 = GetLevelWiseNodes(root.Left, level - 1);
                Heap.Heap subResult2 = GetLevelWiseNodes(root.Right, level - 1);

                while (subResult1.count > 0)
                {
                    heapResult.Insert(subResult1.Delete());
                }
                while (subResult2.count > 0)
                {
                    heapResult.Insert(subResult2.Delete());
                }
            }

            return(heapResult);
        }
示例#2
0
        public static void PrintLevelsInSortedOrder(TreeNode root)
        {
            int height = GetHeight(root);

            for (int i = 1; i <= height; i++)
            {
                Heap.Heap minHeap = GetLevelWiseNodes(root, i);
                while (minHeap.count > 0)
                {
                    Console.Write(minHeap.Delete() + " ");
                }
            }
        }
示例#3
0
文件: AStar.cs 项目: Malakahh/A-star
        public static Node[] FindPath(Node start, Node goal, Graphs.DirectedGraph <NodeData, EData> graph, Func <Node, Node, double> heuristic)
        {
            List <Node>       visited  = new List <Node>();
            dataStructureHeap frontier = new dataStructureHeap(new List <HeapNode>(), dataStructureHeap.HeapProperty.MinHeap);

            start.IntermediateCost = 0;
            start.EstimatedCost    = 0;
            start.Parent           = null;
            frontier.HeapInsert(start);

            //Continue as long as there are nodes to explore in the frontier
            while (frontier.Count > 0)
            {
                Node current = (Node)frontier.HeapExtractRoot();

                if (current == goal)
                {
                    return(ReconstructPath(current));
                }

                visited.Add(current);
                foreach (var edgeTo in graph.GetEdges(current)) //Expand frontier
                {
                    Node to = (Node)edgeTo.Key;

                    if (visited.Contains(to))
                    {
                        continue;
                    }

                    double tentativeScore = current.IntermediateCost + ((EdgeData)edgeTo.Value).cost;
                    if (tentativeScore < to.IntermediateCost) //Best path to this node so far
                    {
                        to.IntermediateCost = tentativeScore;
                        to.EstimatedCost    = tentativeScore + heuristic(to, goal);
                        to.Parent           = current;
                    }

                    if (!frontier.Contains(to))
                    {
                        frontier.HeapInsert(to);
                    }
                }
            }

            return(null);
        }
示例#4
0
        static void Main(string[] args)
        {
            var heap = new Heap.Heap(4, HeapType.MinHeap);

            while (true)
            {
                Console.WriteLine("");
                Console.WriteLine("Choose an Options");
                Console.WriteLine("1. Insert Element");
                Console.WriteLine("2. Extract Top");
                Console.WriteLine("3. Peek");
                Console.WriteLine("4. HeapSize");
                Console.WriteLine("5. Print");
                Console.WriteLine("6. Exit");
                var option = Console.ReadLine();
                Console.WriteLine("");
                switch (option)
                {
                case "1":
                {
                    Console.Write("Enter Value : ");
                    var value = Console.ReadLine();
                    heap.Insert(Convert.ToInt32(value));
                    break;
                }

                case "2":
                {
                    Console.WriteLine("Extracted Top : " + heap.ExtractTop());
                    break;
                }

                case "3":
                {
                    Console.WriteLine("Peek : " + heap.Peek);
                    break;
                }

                case "4":
                {
                    Console.WriteLine("HeapSize : " + heap.HeapSize);
                    break;
                }

                case "5":
                {
                    Console.Write("Heap Data : ");
                    foreach (var data in heap.HeapData)
                    {
                        Console.Write(data);
                        Console.Write(" ");
                    }
                    break;
                }

                case "6":
                {
                    return;
                }
                }
            }
        }
示例#5
0
        static void Main(string[] args)
        {
            // myHeap1 for Sort test
            Heap myHeap1 = new Heap();
            myHeap1.Insert(20);
            myHeap1.Insert(32);
            myHeap1.Insert(2);
            myHeap1.Insert(25);
            myHeap1.Insert(35);
            myHeap1.Insert(814);
            myHeap1.Insert(-5);
            myHeap1.Insert(0);
            myHeap1.Insert(16);
            myHeap1.Insert(77);

            myHeap1.Sort();
            for (int i = 0; i < myHeap1.heapIndex; i++)
                Debug.Assert(myHeap1[i] < myHeap1[i + 1]);

            // myHeap2 Insert and RemoveMax tests
            Heap myHeap2 = new Heap();
            myHeap2.Insert(65);
            myHeap2.Insert(35);
            myHeap2.Insert(9);
            myHeap2.Insert(272);
            myHeap2.Insert(6245);
            myHeap2.Insert(30);
            myHeap2.Insert(918);
            myHeap2.Insert(4);
            myHeap2.Insert(-6);
            myHeap2.Insert(1);

            int currRemoval = myHeap2.RemoveMax();
            while (myHeap2.heapIndex > 1)
            {
                int lastRemoval = currRemoval;
                currRemoval = myHeap2.RemoveMax();
                Debug.Assert(lastRemoval > currRemoval);
            }
        }