-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinomnode.go
94 lines (71 loc) · 2.04 KB
/
binomnode.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
package BinomialHeap
import "fmt"
const (
PRINT_LEVEL_INCR = 4
)
type BinomialHeapNode struct {
value int
parent *BinomialHeapNode
children_head *BinomialHeapNode
rightsibling *BinomialHeapNode
order int
}
func newBinomialHeapNode(value int) *BinomialHeapNode {
return &BinomialHeapNode {
value: value,
parent: nil,
children_head: nil,
rightsibling: nil,
order: 0,
}
}
/*
A node adopts another node, becoming its parent.
*/
func (bn* BinomialHeapNode) adopt(other *BinomialHeapNode) {
// Assumes "other" has no parent and is not present within a linked list.
// Sibling relations
insertIntoLinkedList(&bn.children_head, other)
// Parent relations
other.parent = bn
}
/*
A node goes rogue, severing its ties with its parent and siblings.
*/
func (bn* BinomialHeapNode) rogue() {
// Assumes this node has a parent already.
// Note: Topmost nodes of forests should not be isolated using this function.
// Sibling relations
removeFromLinkedList(&bn.parent.children_head, bn)
// Parent relations
bn.parent = nil
}
/*
Link two given nodes; which is simply done with the principle:
"The node with smaller value becomes the parent of the other one"
*/
func linkNodes(n1 *BinomialHeapNode, n2 *BinomialHeapNode) *BinomialHeapNode {
if n1.value < n2.value {
n1.order += 1
n1.adopt(n2)
return n1
} else {
n2.order += 1
n2.adopt(n1)
return n2
}
}
// === Printing Utility ===
func (bn *BinomialHeapNode) print_single() {
/*fmt.Printf("Value: %d Order: %d addr: %p ch_head: %p right: %p parent: %p\n",
bn.value, bn.order, bn, bn.children_head, bn.rightsibling, bn.parent)
*/
fmt.Printf("Value: %d Order: %d\n", bn.value, bn.order)
}
func (bn *BinomialHeapNode) print_recursive(level int) {
printSpaces(level)
bn.print_single()
for _, child := range nodeIterator(bn.children_head) {
child.print_recursive(level + PRINT_LEVEL_INCR)
}
}