-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtreeset.go
61 lines (48 loc) · 1.27 KB
/
treeset.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
///main package has examples shown
// in Go Data Structures and algorithms book
package main
// TreeSet type
type TreeSet struct {
bst *BinarySearchTree
}
// InsertTreeNode method
func (treeset *TreeSet) InsertTreeNode(treeNodes ...TreeNode) {
var treeNode TreeNode
for _, treeNode = range treeNodes {
treeset.bst.InsertElement(treeNode.key, treeNode.value)
}
}
// Delete method
func (treeset *TreeSet) Delete(treeNodes ...TreeNode) {
var treeNode TreeNode
for _, treeNode = range treeNodes {
treeset.bst.RemoveNode(treeNode.key)
}
}
// Search method
func (treeset *TreeSet) Search(treeNodes ...TreeNode) bool {
var treeNode TreeNode
var exists bool
for _, treeNode = range treeNodes {
if exists = treeset.bst.SearchNode(treeNode.key); !exists {
return false
}
}
return true
}
// String method
func (treeset *TreeSet) String() {
treeset.bst.String()
}
// main method
func main() {
var treeset *TreeSet = &TreeSet{}
treeset.bst = &BinarySearchTree{}
var node1 TreeNode = TreeNode{8, 8, nil, nil}
var node2 TreeNode = TreeNode{3, 3, nil, nil}
var node3 TreeNode = TreeNode{10, 10, nil, nil}
var node4 TreeNode = TreeNode{1, 1, nil, nil}
var node5 TreeNode = TreeNode{6, 6, nil, nil}
treeset.InsertTreeNode(node1, node2, node3, node4, node5)
treeset.String()
}