|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
"github.com/01-edu/z01"
|
|
|
|
|
|
|
|
solutions "../../solutions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type stuNode = TNode
|
|
|
|
type solNode = solutions.TNode
|
|
|
|
|
|
|
|
func solInsert(N *solNode, newVal int) {
|
|
|
|
if N == nil {
|
|
|
|
return
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
}
|
|
|
|
if newVal <= N.Val {
|
|
|
|
if N.Left == nil {
|
|
|
|
N.Left = &solNode{Val: newVal, Left: nil, Right: nil}
|
|
|
|
} else {
|
|
|
|
solInsert(N.Left, newVal)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if N.Right == nil {
|
|
|
|
N.Right = &solNode{Val: newVal, Left: nil, Right: nil}
|
|
|
|
} else {
|
|
|
|
solInsert(N.Right, newVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func stuInsert(N *stuNode, newVal int) {
|
|
|
|
if N == nil {
|
|
|
|
return
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
}
|
|
|
|
if newVal <= N.Val {
|
|
|
|
if N.Left == nil {
|
|
|
|
N.Left = &stuNode{Val: newVal, Left: nil, Right: nil}
|
|
|
|
} else {
|
|
|
|
stuInsert(N.Left, newVal)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if N.Right == nil {
|
|
|
|
N.Right = &stuNode{Val: newVal, Left: nil, Right: nil}
|
|
|
|
} else {
|
|
|
|
stuInsert(N.Right, newVal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
func IsIdentical(root1 *solNode, root2 *stuNode) bool {
|
|
|
|
if root1 == nil && root2 == nil {
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
return true
|
|
|
|
}
|
|
|
|
if root1 == nil && root2 != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if root2 == nil && root1 != nil {
|
|
|
|
return false
|
|
|
|
}
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
return root1.Val == root2.Val && IsIdentical(root1.Left, root2.Left) && IsIdentical(root1.Right, root2.Right)
|
|
|
|
}
|
|
|
|
|
|
|
|
func stuPrint(w io.Writer, node *stuNode, ns int, ch rune) {
|
|
|
|
if node == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < ns; i++ {
|
|
|
|
fmt.Fprint(w, " ")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "%c:%v\n", ch, node.Val)
|
|
|
|
stuPrint(w, node.Left, ns+2, 'L')
|
|
|
|
stuPrint(w, node.Right, ns+2, 'R')
|
|
|
|
}
|
|
|
|
|
|
|
|
func solPrint(w io.Writer, node *solNode, ns int, ch rune) {
|
|
|
|
if node == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < ns; i++ {
|
|
|
|
fmt.Fprint(w, " ")
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "%c:%v\n", ch, node.Val)
|
|
|
|
solPrint(w, node.Left, ns+2, 'L')
|
|
|
|
solPrint(w, node.Right, ns+2, 'R')
|
|
|
|
}
|
|
|
|
|
|
|
|
func returnStuTree(root *stuNode) string {
|
|
|
|
if root == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
ans := strconv.Itoa(root.Val)
|
|
|
|
if root.Left == nil && root.Right == nil {
|
|
|
|
return ans
|
|
|
|
}
|
|
|
|
if root.Left != nil {
|
|
|
|
ans += " " + returnStuTree(root.Left)
|
|
|
|
}
|
|
|
|
if root.Right != nil {
|
|
|
|
ans += " " + returnStuTree(root.Right)
|
|
|
|
}
|
|
|
|
return ans
|
|
|
|
}
|
|
|
|
|
|
|
|
func returnSolTree(root *solNode) string {
|
|
|
|
if root == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
ans := strconv.Itoa(root.Val)
|
|
|
|
if root.Left == nil && root.Right == nil {
|
|
|
|
return ans
|
|
|
|
}
|
|
|
|
if root.Left != nil {
|
|
|
|
ans += " " + returnSolTree(root.Left)
|
|
|
|
}
|
|
|
|
if root.Right != nil {
|
|
|
|
ans += " " + returnSolTree(root.Right)
|
|
|
|
}
|
|
|
|
return ans
|
|
|
|
}
|
|
|
|
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
func main() {
|
|
|
|
root, val1, val2, val3, val4 := 0, 0, 0, 0, 0
|
|
|
|
|
|
|
|
root = rand.Intn(30)
|
|
|
|
tree := &solNode{Val: root, Left: nil, Right: nil}
|
|
|
|
TestTree := &stuNode{Val: root, Left: nil, Right: nil}
|
|
|
|
for i := 0; i < 15; i++ {
|
|
|
|
tree = &solNode{Val: root, Left: nil, Right: nil}
|
|
|
|
temp := tree
|
|
|
|
val1, val2, val3, val4 = rand.Intn(30), rand.Intn(30), rand.Intn(30), rand.Intn(30)
|
|
|
|
solInsert(tree, val1)
|
|
|
|
solInsert(tree, val2)
|
|
|
|
solInsert(tree, val3)
|
|
|
|
solInsert(tree, val4)
|
|
|
|
// solPrint(os.Stdout, tree, 0, 'M')
|
|
|
|
solutions.InvertTree(tree)
|
|
|
|
// solPrint(os.Stdout, tree, 0, 'M')
|
|
|
|
|
|
|
|
TestTree = &stuNode{Val: root, Left: nil, Right: nil}
|
|
|
|
tmp := TestTree
|
|
|
|
stuInsert(TestTree, val1)
|
|
|
|
stuInsert(TestTree, val2)
|
|
|
|
stuInsert(TestTree, val3)
|
|
|
|
stuInsert(TestTree, val4)
|
|
|
|
// stuPrint(os.Stdout, TestTree, 0, 'M')
|
|
|
|
InvertTree(TestTree)
|
|
|
|
// stuPrint(os.Stdout, TestTree, 0, 'M')
|
|
|
|
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
if !IsIdentical(tree, TestTree) {
|
|
|
|
tree1 := returnSolTree(temp)
|
|
|
|
tree2 := returnStuTree(tmp)
|
Refactor & Beautify & destruction commit
return early, remove else branches, reorder conditions and top-level functions, remove empty lines, remove unnecessary append(), fix typos, stop using testing package, remove dead code, fix mistakes in subjects, tests and solutions, remove disclaimers, reformat comments, simplify solutions, tests, add more instructions to subjects, remove obsolete files, etc.
Some of the reasons behind those modifications will be added to good-practices.en.md
Some of the exercises are now broken, they will have to be fixed, most of them have a "TODO:" comment.
5 years ago
|
|
|
z01.Fatalf("\n\"%v\" instead of \"%v\"\n\n", tree1, tree2)
|
|
|
|
// z01.Fatalf("\nError\n\n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|