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.
139 lines
2.9 KiB
139 lines
2.9 KiB
5 years ago
|
package correct
|
||
5 years ago
|
|
||
5 years ago
|
type TreeNodeL struct {
|
||
|
Left *TreeNodeL
|
||
|
Val int
|
||
|
Right *TreeNodeL
|
||
|
}
|
||
|
|
||
|
func IsSameTree(p *TreeNodeL, q *TreeNodeL) bool {
|
||
|
if p == nil && q == nil {
|
||
|
return true
|
||
|
}
|
||
|
return checkIfEq(p, q)
|
||
|
}
|
||
|
|
||
|
func checkIfEq(t1 *TreeNodeL, t2 *TreeNodeL) bool {
|
||
|
if t1 == nil && t2 == nil {
|
||
|
return true
|
||
|
}
|
||
|
if t1 == nil || t2 == nil {
|
||
|
return false
|
||
|
}
|
||
|
return t1.Val == t2.Val && checkIfEq(t1.Right, t2.Right) && checkIfEq(t1.Left, t2.Left)
|
||
|
}
|
||
5 years ago
|
|
||
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
|
lib.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++ {
|
||
5 years ago
|
value := node{lib.RandIntBetween(10, 15), lib.RandIntBetween(1, 10)}
|
||
5 years ago
|
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
|
}
|