-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontainer.go
More file actions
61 lines (50 loc) · 1.29 KB
/
container.go
File metadata and controls
61 lines (50 loc) · 1.29 KB
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
/*
* Implementation of a Wrapper class container which holds a RED_BLACK_TREE
* or a Slice
* 14/5/18
* Author Callan Taylor
*/
package main
import "fmt"
type container struct {
containerType string
contentsArray []string
contentsRbt *rbt
}
func newContainer(t string) *container {
c := container{containerType:"", contentsArray:nil, contentsRbt:nil}
if t == "RED_BLACK_TREE" {
c = container{containerType:t, contentsArray:nil, contentsRbt:newRbt()}
} else {
c = container{containerType:t, contentsArray:make([]string, 0), contentsRbt:nil}
}
return &c
}
func containerAdd(c *container, word string) {
if c.containerType == "RED_BLACK_TREE" {
c.contentsRbt = rootFix(insertWord(c.contentsRbt, word))
} else {
c.contentsArray = append(c.contentsArray, word)
}
}
func containerPrint(c *container) {
if c.containerType == "RED_BLACK_TREE" {
preorderTraverse(c.contentsRbt, printRbt)
} else {
for i := 0; i < len(c.contentsArray); i++ {
fmt.Print(c.contentsArray[i], " ")
}
}
}
func containerSearch(c *container, word string) bool {
if c.containerType == "RED_BLACK_TREE" {
return rbtSearch(c.contentsRbt, word)
} else {
for _, value := range c.contentsArray {
if value == word {
return true
}
}
}
return false
}