-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
95 lines (80 loc) · 2.14 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Double linked list is used to store values of a binomial heap */
package binomialheap
//import "fmt"
/* Method to insert node into the forest*/
func InsertDL(head **BinomialHeapNode, node *BinomialHeapNode) {
var prev *BinomialHeapNode
var next *BinomialHeapNode
prev = nil
next = *head
for next != nil && node.order < next.order {
prev = next
next = next.rightsibling
}
if prev == nil && next == nil {
*head = node
} else if prev == nil && next != nil {
node.rightsibling = *head
*head = node
} else if prev != nil && next == nil {
prev.rightsibling = node
} else if prev != nil && next != nil {
prev.rightsibling = node
node.rightsibling = next
}
}
/* Method to remove node from forest */
func RemoveFromDL(head **BinomialHeapNode, node *BinomialHeapNode) {
leftsib := GetLeftsibling(*head, node)
if leftsib == nil {
*head = node.rightsibling
} else {
leftsib.rightsibling = node.rightsibling
}
node.rightsibling = nil
//fmt.Println("node.rightsibling value", node.rightsibling.value)
}
/* To remove node form binomial heap need to find leftsibling,So this method returns leftsibling*/
func GetLeftsibling(head *BinomialHeapNode, node *BinomialHeapNode) *BinomialHeapNode {
if head == node {
return nil
}
current := head
for current.rightsibling != node {
current = current.rightsibling
}
return current
}
/* It returns node with same order of parameter node passed,otherwise returns nil */
func GetNodeWithOrder(head *BinomialHeapNode, order int) *BinomialHeapNode {
current := head
for current != nil {
if current.order == order {
return current
}
current = current.rightsibling
}
return nil
}
/* Returns minimum node from binomial tree */
func GetMinimumNode(head *BinomialHeapNode) *BinomialHeapNode {
min := head
current := head.rightsibling
for current != nil {
if current.Value < min.Value {
min = current
}
current = current.rightsibling
}
return min
}
/* Iterates trough level wise */
func NodeIterator(head *BinomialHeapNode) []*BinomialHeapNode {
var arr []*BinomialHeapNode
trnode := head
for trnode != nil {
arr = append(arr, trnode)
trnode = trnode.rightsibling
}
return arr
}