Merge Go Work
This commit is contained in:
Binary file not shown.
@@ -11,9 +11,9 @@ func main() {
|
||||
s1 := stringset.NewFromSlice([]string{"a"})
|
||||
addAndOutput(s1, "b")
|
||||
addAndOutput(s1, "d")
|
||||
addAndOutput(s1, "c")
|
||||
s1.PrettyPrint()
|
||||
return
|
||||
addAndOutput(s1, "c")
|
||||
addAndOutput(s1, "0")
|
||||
addAndOutput(s1, "aa")
|
||||
addAndOutput(s1, "aaa")
|
||||
|
@@ -27,7 +27,6 @@ package stringset
|
||||
// Format the empty set as {}.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"strconv"
|
||||
@@ -167,17 +166,13 @@ func TestEqual(t *testing.T) {
|
||||
// helper for testing Add, Delete
|
||||
func testEleOp(name string, op func(Set, string), cases []eleOpCase, t *testing.T) {
|
||||
for _, tc := range cases {
|
||||
fmt.Print("Running Test Case: ")
|
||||
fmt.Println(tc)
|
||||
s := NewFromSlice(tc.set)
|
||||
op(s, tc.ele)
|
||||
want := NewFromSlice(tc.want)
|
||||
if !Equal(s, want) {
|
||||
fmt.Println(s.String())
|
||||
t.Fatalf("%v %s %q = %v, want %v",
|
||||
NewFromSlice(tc.set), name, tc.ele, s, want)
|
||||
}
|
||||
fmt.Println("=== Done ===")
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -3,7 +3,6 @@ package stringset
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -45,20 +44,12 @@ func (s Set) Add(v string) {
|
||||
return
|
||||
}
|
||||
if !s.Has(v) {
|
||||
bef := "Current Top Node: " + s.top.value + ";L:" + s.top.getLeftValue() + ";R:" + s.top.getRightValue()
|
||||
newNode := s.top.Add(&Node{value: v})
|
||||
fmt.Println(bef)
|
||||
fmt.Println("Current New Node: " + newNode.value + ";L:" + newNode.getLeftValue() + ";R:" + newNode.getRightValue())
|
||||
fmt.Printf("newNode.left: %v (%v)\n", &newNode.left, newNode.left)
|
||||
|
||||
s.top.value = newNode.value
|
||||
s.top.left = newNode.left
|
||||
s.top.right = newNode.right
|
||||
fmt.Println("Current Top Node: " + s.top.value + ";L:" + s.top.getLeftValue() + ";R:" + s.top.getRightValue())
|
||||
fmt.Printf("s.top.left: %v (%v)\n", &s.top.left, s.top.left)
|
||||
fmt.Println("Add Done")
|
||||
|
||||
//s.top = s.top.Add(newNode)
|
||||
//fmt.Println("Current Top Node: " + s.top.value + ";L:" + s.top.getLeftValue() + ";R:" + s.top.getRightValue())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,63 +265,43 @@ func (sv *Node) findHome(v *Node) {
|
||||
}
|
||||
|
||||
func (sv *Node) Add(n *Node) *Node {
|
||||
fmt.Println("1 Adding at Node " + sv.value)
|
||||
cmp := strings.Compare(n.value, sv.value)
|
||||
if cmp < 0 {
|
||||
fmt.Println("2 checking to left")
|
||||
if sv.left == nil {
|
||||
fmt.Println("3 left is nil, add it")
|
||||
sv.left = n
|
||||
} else {
|
||||
fmt.Println("--> 4 recurse left")
|
||||
sv.left = sv.left.Add(n)
|
||||
fmt.Println("<-- 5 added to left")
|
||||
}
|
||||
} else {
|
||||
fmt.Println("6 checking to right")
|
||||
if sv.right == nil {
|
||||
fmt.Println("7 right is nil, add it")
|
||||
sv.right = n
|
||||
} else {
|
||||
fmt.Println("--> 8 recurse right")
|
||||
sv.right = sv.right.Add(n)
|
||||
fmt.Println("<-- 9 added to right")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println("10 let's balance now (sv: " + sv.value + ")")
|
||||
newVal := sv
|
||||
/* TODO: Fix the tree balancing
|
||||
balance := GetBalance(sv)
|
||||
// left left unbalance
|
||||
fmt.Println("11 balance: " + strconv.Itoa(balance))
|
||||
newVal := sv
|
||||
if balance > 1 && strings.Compare(n.value, sv.getLeftValue()) < 0 { // < sv.left.value {
|
||||
fmt.Println("12 Left Left")
|
||||
if balance > 1 && strings.Compare(n.value, sv.getLeftValue()) < 0 {
|
||||
newVal = RightRotate(sv)
|
||||
fmt.Println("13")
|
||||
}
|
||||
// right right unbalance
|
||||
if balance < -1 && strings.Compare(n.value, sv.getRightValue()) > 0 { //n.value > sv.right.value {
|
||||
fmt.Println("14 Right Right")
|
||||
if balance < -1 && strings.Compare(n.value, sv.getRightValue()) > 0 {
|
||||
newVal = LeftRotate(sv)
|
||||
fmt.Println("15")
|
||||
}
|
||||
// left right unbalance
|
||||
if balance > 1 && strings.Compare(n.value, sv.getLeftValue()) > 0 { //n.value > sv.left.value {
|
||||
fmt.Println("16 Left Right")
|
||||
if balance > 1 && strings.Compare(n.value, sv.getLeftValue()) > 0 {
|
||||
sv.left = LeftRotate(sv.left)
|
||||
fmt.Println("17")
|
||||
newVal = RightRotate(sv)
|
||||
fmt.Println("18")
|
||||
}
|
||||
// right left unbalance
|
||||
if balance < -1 && strings.Compare(n.value, sv.getRightValue()) < 0 { //n.value < sv.right.value {
|
||||
fmt.Println("19 Right Left")
|
||||
if balance < -1 && strings.Compare(n.value, sv.getRightValue()) < 0 {
|
||||
sv.right = RightRotate(sv.right)
|
||||
fmt.Println("20")
|
||||
newVal = LeftRotate(sv)
|
||||
fmt.Println("21")
|
||||
}
|
||||
fmt.Println("22 end add")
|
||||
*/
|
||||
return newVal
|
||||
}
|
||||
|
||||
@@ -476,9 +447,6 @@ func RightRotate(y *Node) *Node {
|
||||
// Perform rotation
|
||||
x.right = y
|
||||
y.left = t2
|
||||
fmt.Println("RightRotate: return=" + x.value + "; L:" + x.left.value + "; R:" + x.right.value)
|
||||
fmt.Println("RightRotate: leftNode=" + x.getLeftValue() + "; L:" + x.left.getLeftValue() + "; R:" + x.left.getRightValue())
|
||||
fmt.Println("RightRotate: rightNode=" + x.getRightValue() + "; L:" + x.right.getLeftValue() + "; R:" + x.right.getRightValue())
|
||||
// Return new root
|
||||
return x
|
||||
}
|
||||
@@ -490,9 +458,6 @@ func LeftRotate(x *Node) *Node {
|
||||
y.left = x
|
||||
x.right = t2
|
||||
// Return new root
|
||||
fmt.Println("LeftRotate: return=" + y.value + "; L:" + y.left.value + "; R:" + y.right.value)
|
||||
fmt.Println("LeftRotate: leftNode=" + y.getLeftValue() + "; L:" + y.left.getLeftValue() + "; R:" + y.left.getRightValue())
|
||||
fmt.Println("LeftRotate: rightNode=" + y.getRightValue() + "; L:" + y.right.getLeftValue() + "; R:" + y.right.getRightValue())
|
||||
return y
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user