Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create minHeap.go #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Create minHeap.go #18

wants to merge 1 commit into from

Conversation

aramzes
Copy link
Collaborator

@aramzes aramzes commented Jun 16, 2019

No description provided.

h := &minHeap
for _, value := range values {
h.insert(value)
fmt.Println(h.store)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove debugging

if len(values) == 0 {
return nil, errors.New("Array is empty")
}
minHeap := heap{capacity: (len(values) * 2), size: 0}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why capacity default to twice size only ?
what if i wanna build a heap size of 1 right now and keep adding new vaues as we go?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking of table-doubling based on the need for capacity to make efficient use of the memory

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea

}

// checks if the array is not full
func (h *heap) checkCapacity() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the way to double the size?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the capacity is full, a new array is created with double the size of the original array.

h.capacity = h.capacity * 2
newStore := make([]int, h.capacity)
for _, value := range h.store {
newStore = append(newStore, value)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to keep references to old store members , to destroy it once its copied.
to decrease the risk of memory leaks.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think golang has automatic garbage collection and once h.store is assigned to the new array, the old one is automatically deleted, am I right?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it has a good gc, it will need some tweaking on compiling.

@aramzes
Copy link
Collaborator Author

aramzes commented Jul 28, 2019

package heap

import (
	"fmt"
	"testing"
)

var minHeap *heap

func TestBuildMinHeap(t *testing.T) {
	values := []int{20, 0, 15, 6, 28, 17, 3, 1, 9}
	minHeap, err := buildMinHeap(values)

	expected := []int{0, 1, 3, 6, 28, 17, 15, 20, 9}
	if err != nil {
		fmt.Println(err)
	} else {
		if minHeap.size == 0 {
			t.Error("Heap was not created successfully")
		} else {
			for i := range expected {
				if minHeap.store[i] != expected[i] {
					t.Error("Expected", expected, "got", minHeap.store)
				}
			}
		}
	}
	fmt.Println(minHeap.store)
}

func TestCheckCapacity(t *testing.T) {
	values := []int{20, 0, 15, 6}
	minHeap, _ := buildMinHeap(values)
	//h := &minHeap

	values2 := []int{28, 17, 3, 1, 9}
	for _, value := range values2 {
		minHeap.insert(value)
	}

	if minHeap.capacity != 16 {
		t.Error("Capacity was not doubled")
	}
}

func TestExtractMin(t *testing.T) {
	values := []int{20, 0, 15, 6, 28, 17, 3, 1, 9}
	expected := 0
	minHeap, err := buildMinHeap(values)
	if err != nil {
		fmt.Println(err)
	} else {
		if minHeap.size == 0 {
			t.Error("Heap was not created successfully")
		}
	}
	min, err := minHeap.extractMin()
	if err != nil {
		fmt.Println(err)
	} else {
		if min != expected {
			fmt.Println("Expected", expected, "got", min)
		} else {
			fmt.Println("The minimum value is", min)
		}
	}
}

func TestInsert(t *testing.T) {
	values := []int{20, 0, 15, 6, 28, 17, 3, 1}
	minHeap, _ := buildMinHeap(values)
	minHeap.insert(9)
	expected := []int{0, 1, 3, 6, 28, 17, 15, 20, 9}

	for i := range expected {
		if expected[i] != minHeap.store[i] {
			t.Error("Expected", expected, "got", minHeap.store)
		}
	}
}

func TestHeapifyUp(t *testing.T) {
	values := []int{20, 0, 15, 6, 28, 17, 3, 1, 9}
	minHeap := heap{store: values, capacity: 16, size: 9}
	minHeap.heapifyUp(7)

	expected := []int{20, 0, 15, 1, 28, 17, 3, 6, 9} // 6 is replaced by 1

	for i := range expected {
		if minHeap.store[i] != expected[i] {
			t.Error("Expected", expected, "got", minHeap.store)
		}
	}
}

func TestParent(t *testing.T) {
	values := []int{20, 0, 15, 6, 28, 17, 3, 1, 9}
	minHeap, _ := buildMinHeap(values)

	expectedRoot := 0
	expectedLeftChild := 2  //child index 5
	expectedRightChild := 3 //child index 8

	resultRoot, isRoot1 := minHeap.parent(0)
	resultLeftChild, isRoot2 := minHeap.parent(5)
	resultRightChild, isRoot3 := minHeap.parent(8)

	if isRoot1 == false || isRoot2 == true || isRoot3 == true {
		t.Error("isRoot of root is", isRoot1)
		t.Error("isRoot of left child is", isRoot2)
		t.Error("isRoot of right child is", isRoot3)
	} else if resultRoot != expectedRoot || resultLeftChild != expectedLeftChild || resultRightChild != expectedRightChild {
		t.Error("Expected parent of root", expectedRoot, "got", resultRoot)
		t.Error("Expected parent of root", expectedLeftChild, "got", resultLeftChild)
		t.Error("Expected parent of root", expectedRightChild, "got", resultRightChild)
	}
}

func TestHeapifyDown(t *testing.T) {
	values := []int{20, 0, 15, 6, 28, 17, 3, 1, 9}
	minHeap := heap{store: values, capacity: 16, size: 9}
	minHeap.heapifyDown(0)
	expected := []int{0, 6, 15, 1, 28, 17, 3, 20, 9}
	for i := range expected {
		if minHeap.store[i] != expected[i] {
			t.Error("Expected", expected, "got", minHeap.store)
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants