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.
164 lines
3.3 KiB
164 lines
3.3 KiB
5 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
5 years ago
|
"io"
|
||
5 years ago
|
"math/rand"
|
||
|
"strconv"
|
||
5 years ago
|
|
||
5 years ago
|
"github.com/01-edu/z01"
|
||
|
|
||
5 years ago
|
correct "./correct"
|
||
5 years ago
|
)
|
||
|
|
||
|
type stuNode = TNode
|
||
5 years ago
|
type solNode = correct.TNode
|
||
5 years ago
|
|
||
|
func solInsert(N *solNode, newVal int) {
|
||
5 years ago
|
if N == nil {
|
||
|
return
|
||
5 years ago
|
}
|
||
|
if newVal <= N.Val {
|
||
5 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
|
func stuInsert(N *stuNode, newVal int) {
|
||
5 years ago
|
if N == nil {
|
||
|
return
|
||
5 years ago
|
}
|
||
|
if newVal <= N.Val {
|
||
5 years ago
|
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)
|
||
|
}
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
func IsIdentical(root1 *solNode, root2 *stuNode) bool {
|
||
5 years ago
|
if root1 == nil && root2 == nil {
|
||
5 years ago
|
return true
|
||
|
}
|
||
|
if root1 == nil && root2 != nil {
|
||
|
return false
|
||
|
}
|
||
|
if root2 == nil && root1 != nil {
|
||
|
return false
|
||
5 years ago
|
}
|
||
5 years ago
|
return root1.Val == root2.Val && IsIdentical(root1.Left, root2.Left) && IsIdentical(root1.Right, root2.Right)
|
||
5 years ago
|
}
|
||
|
|
||
|
func stuPrint(w io.Writer, node *stuNode, ns int, ch rune) {
|
||
5 years ago
|
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')
|
||
5 years ago
|
}
|
||
|
|
||
|
func solPrint(w io.Writer, node *solNode, ns int, ch rune) {
|
||
5 years ago
|
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')
|
||
5 years ago
|
}
|
||
|
|
||
|
func returnStuTree(root *stuNode) 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 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
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
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}
|
||
5 years ago
|
temp := tree
|
||
5 years ago
|
val1, val2, val3, val4 = rand.Intn(30), rand.Intn(30), rand.Intn(30), rand.Intn(30)
|
||
5 years ago
|
solInsert(tree, val1)
|
||
|
solInsert(tree, val2)
|
||
|
solInsert(tree, val3)
|
||
|
solInsert(tree, val4)
|
||
|
// solPrint(os.Stdout, tree, 0, 'M')
|
||
5 years ago
|
correct.InvertTree(tree)
|
||
5 years ago
|
// 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')
|
||
|
|
||
5 years ago
|
if !IsIdentical(tree, TestTree) {
|
||
5 years ago
|
tree1 := returnSolTree(temp)
|
||
|
tree2 := returnStuTree(tmp)
|
||
5 years ago
|
z01.Fatalf("\n\"%v\" instead of \"%v\"\n\n", tree1, tree2)
|
||
|
// z01.Fatalf("\nError\n\n")
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
|
}
|