Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Creating a Binary Tree Heap in Java where we are inserting value at object Heap[0] and then calling Heapify_Down() to move the value down the

Creating a Binary Tree Heap in Java where we are inserting value at object Heap[0] and then calling Heapify_Down() to move the value down the tree where it belongs so the smallest value is at position Heap[1].

I can't figure out what I need to do for the Heapify_Down procedure. My procedure just flips the values when I add the second number instead of moving on and putting the value in the leaf node. I am so complete lost.

import java.util.LinkedHashMap; public class StartHeap { private int[] Heap; private int size; private int maxsize; static LinkedHashMap dictionary = new LinkedHashMap(); private static final int FRONT = 1; // not required public StartHeap(int maxsize) { this.maxsize = maxsize; this.size = 0; Heap = new int[this.maxsize + 1]; Heap[0] = Integer.MIN_VALUE; } public int FindMin() { return Heap[1]; } private void swap(int fpos, int spos) { int tmp; tmp = Heap[fpos]; Heap[fpos] = Heap[spos]; Heap[spos] = tmp; } private void Heapify_Down2(int i) { int n = size; int j = 1; if ((2 * i) > n) { System.out.println("Bottom of Heap."); } else if ((2 * i) < n) { int left = 2 * i; int right = 2 * i + 1; if (Heap[left] < Heap[i]) { j = left; System.out.println("J Left: " + j); } else if (Heap[right] < Heap[i]) { j = right; System.out.println("J Right: " + j); } }else if ((2 * i) == n) { j = 2 * i; System.out.println("J 2i = n: " + j); } System.out.println("Heap[" + j +"*] " + Heap[j]); System.out.println("Heap[" + i +"*] " + Heap[i]); if (Heap[j] > Heap[i] || Heap[j] == 0) { System.out.println("Heapj[" + j +"]- " + Heap[j]); System.out.println("Heapi[" + i +"]- " + Heap[i]); swap(i,j); System.out.println("AfterSwapHeap j[" + j +"]- " + Heap[j]); System.out.println("AfterSwapHeap i[" + i +"]- " + Heap[i]); Heapify_Down2(i); }else if (Heap[i] == 0 ) { System.out.println("i is zero"); } } private void Heapify_Up(int i) { } private void Insert(int key, String value) { if(size >= maxsize) { return; } Heap[0] = key; ++size; dictionary.put(key,value); System.out.println("Heap[" + 0 +" Insert] " + Heap[0]); Heapify_Down2(0); } public void print() { for (int i = 1; i <= size / 2; i++) { System.out.print(" PARENT : " + Heap[i] + " LEFT CHILD : " + Heap[2 * i + 1] + " RIGHT CHILD :" + Heap[2 * i + 2]); System.out.println(); } } public static void main(String[] arg) { StartHeap priorityQueue = new StartHeap(8); priorityQueue.Insert(2,"4"); priorityQueue.Insert(1,"5"); //minHeap.Insert(5,0); //minHeap.Insert(3,0); //minHeap.Insert(1,0); System.out.println("The PriorityQueue is " ); priorityQueue.print(); System.out.println(dictionary.get(1)); System.out.println("FindMin: " + priorityQueue.FindMin()); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Second International Conference Acids Hue City Vietnam March 2010 Proceedings Part 1 Lnai 5990

Authors: Manh Thanh Le ,Jerzy Swiatek ,Ngoc Thanh Nguyen

2010th Edition

3642121446, 978-3642121449

More Books

Students also viewed these Databases questions