mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
2.6 KiB
124 lines
2.6 KiB
5 years ago
|
package main
|
||
|
|
||
|
import (
|
||
5 years ago
|
"math/rand"
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/01-edu/z01"
|
||
5 years ago
|
|
||
5 years ago
|
correct "./correct"
|
||
5 years ago
|
)
|
||
|
|
||
|
type stuTreeNode = TreeNodeL
|
||
5 years ago
|
type solTreeNode = correct.TreeNodeL
|
||
5 years ago
|
|
||
|
func insertStu(t *stuTreeNode, v int) *stuTreeNode {
|
||
5 years ago
|
if t == nil {
|
||
|
return &stuTreeNode{Left: nil, Val: v, Right: nil}
|
||
|
}
|
||
|
if v < t.Val {
|
||
|
t.Left = insertStu(t.Left, v)
|
||
|
return t
|
||
|
}
|
||
|
t.Right = insertStu(t.Right, v)
|
||
|
return t
|
||
5 years ago
|
}
|
||
|
|
||
|
func insertSol(t *solTreeNode, v int) *solTreeNode {
|
||
5 years ago
|
if t == nil {
|
||
|
return &solTreeNode{Left: nil, Val: v, Right: nil}
|
||
|
}
|
||
|
if v < t.Val {
|
||
|
t.Left = insertSol(t.Left, v)
|
||
|
return t
|
||
|
}
|
||
|
t.Right = insertSol(t.Right, v)
|
||
|
return t
|
||
5 years ago
|
}
|
||
|
|
||
|
func New(n, k int) (*solTreeNode, *stuTreeNode, *solTreeNode) {
|
||
5 years ago
|
var cop *solTreeNode
|
||
|
var stu *stuTreeNode
|
||
|
var sol *solTreeNode
|
||
|
for _, v := range rand.Perm(n) {
|
||
|
cop = insertSol(cop, (1+v)*k)
|
||
|
stu = insertStu(stu, (1+v)*k)
|
||
|
sol = insertSol(sol, (1+v)*k)
|
||
|
}
|
||
|
return cop, stu, sol
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
func returnSolTree(root *solTreeNode) 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
|
||
5 years ago
|
}
|
||
|
|
||
|
func returnStuTree(root *stuTreeNode) string {
|
||
5 years ago
|
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
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
func compareResults(stuResult, solResult bool, solTree1, solTree2 *solTreeNode) {
|
||
5 years ago
|
if stuResult != solResult {
|
||
|
tree1 := returnSolTree(solTree1)
|
||
|
tree2 := returnSolTree(solTree2)
|
||
5 years ago
|
z01.Fatalf("\nIsSameTree(\"%v\", \"%v\") == \"%v\" instead of \"%v\"\n\n", tree1, tree2, stuResult, solResult)
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
type node struct {
|
||
|
n int
|
||
|
k int
|
||
|
}
|
||
|
|
||
|
table := []node{}
|
||
|
for i := 0; i < 15; i++ {
|
||
|
value := node{z01.RandIntBetween(10, 15), z01.RandIntBetween(1, 10)}
|
||
|
table = append(table, value)
|
||
|
}
|
||
|
|
||
|
// Check for different trees
|
||
|
for _, arg := range table {
|
||
|
cop1, stuTree1, solTree1 := New(arg.n, arg.k)
|
||
|
cop2, stuTree2, solTree2 := New(arg.n, arg.k)
|
||
|
stuResult := IsSameTree(stuTree1, stuTree2)
|
||
5 years ago
|
solResult := correct.IsSameTree(solTree1, solTree2)
|
||
5 years ago
|
|
||
5 years ago
|
compareResults(stuResult, solResult, cop1, cop2)
|
||
5 years ago
|
}
|
||
|
|
||
|
// Check for same trees
|
||
|
for _, arg := range table {
|
||
|
cop1, stuTree1, solTree1 := New(arg.n, arg.k)
|
||
|
stuResult := IsSameTree(stuTree1, stuTree1)
|
||
5 years ago
|
solResult := correct.IsSameTree(solTree1, solTree1)
|
||
5 years ago
|
|
||
5 years ago
|
compareResults(stuResult, solResult, cop1, cop1)
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|