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.
141 lines
3.2 KiB
141 lines
3.2 KiB
5 years ago
|
package main
|
||
5 years ago
|
|
||
|
import (
|
||
5 years ago
|
"fmt"
|
||
|
|
||
5 years ago
|
"../lib"
|
||
|
"./correct"
|
||
|
"./student"
|
||
5 years ago
|
)
|
||
|
|
||
|
func parentListTransp(root *student.TreeNode) string {
|
||
|
if root == nil {
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
var parent string
|
||
|
|
||
|
if root.Parent == nil {
|
||
|
parent = "nil"
|
||
|
} else {
|
||
|
parent = root.Parent.Data
|
||
|
}
|
||
|
|
||
|
r := "Node: " + root.Data + " Parent: " + parent + "\n"
|
||
|
r += parentListTransp(root.Left) + parentListTransp(root.Right)
|
||
|
return r
|
||
|
}
|
||
|
|
||
|
func FormatTree_transp(root *student.TreeNode) string {
|
||
|
if root == nil {
|
||
|
return ""
|
||
|
}
|
||
|
res := root.Data + "\n"
|
||
|
res += formatSubTree_transp(root, "")
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
func formatSubTree_transp(root *student.TreeNode, prefix string) string {
|
||
|
if root == nil {
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
var res string
|
||
|
|
||
|
hasLeft := root.Left != nil
|
||
|
hasRight := root.Right != nil
|
||
|
|
||
|
if !hasLeft && !hasRight {
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
res += prefix
|
||
|
if hasLeft && hasRight {
|
||
|
res += "├── "
|
||
|
}
|
||
|
|
||
|
if !hasLeft && hasRight {
|
||
|
res += "└── "
|
||
|
}
|
||
|
|
||
|
if hasRight {
|
||
|
printStrand := (hasLeft && hasRight && (root.Right.Right != nil || root.Right.Left != nil))
|
||
|
newPrefix := prefix
|
||
|
if printStrand {
|
||
|
newPrefix += "│ "
|
||
|
} else {
|
||
|
newPrefix += " "
|
||
|
}
|
||
|
res += root.Right.Data + "\n"
|
||
|
res += formatSubTree_transp(root.Right, newPrefix)
|
||
|
}
|
||
|
|
||
|
if hasLeft {
|
||
|
if hasRight {
|
||
|
res += prefix
|
||
|
}
|
||
|
res += "└── " + root.Left.Data + "\n"
|
||
|
res += formatSubTree_transp(root.Left, prefix+" ")
|
||
|
}
|
||
|
return res
|
||
|
}
|
||
|
|
||
5 years ago
|
func errorMessage_transp(fn interface{}, root, sel, repl *correct.TreeNode, rootA *correct.TreeNode, rootAS *student.TreeNode) {
|
||
5 years ago
|
lib.Fatalf("%s(\nRoot:\n %s, Selected:\n%s, Replacement:\n%s\n) ==\n%s instead of\n%s\n",
|
||
5 years ago
|
"BTreeTransplant",
|
||
5 years ago
|
correct.FormatTree(root),
|
||
|
correct.FormatTree(sel),
|
||
|
correct.FormatTree(repl),
|
||
5 years ago
|
FormatTree_transp(rootAS),
|
||
5 years ago
|
correct.FormatTree(rootA),
|
||
5 years ago
|
)
|
||
|
}
|
||
|
|
||
5 years ago
|
func CompareTrees_transp(fn interface{}, root, sel, repl *correct.TreeNode, rootA *correct.TreeNode, rootAS *student.TreeNode) {
|
||
|
solTree := correct.FormatTree(rootA)
|
||
5 years ago
|
stuTree := FormatTree_transp(rootAS)
|
||
|
|
||
|
if solTree != stuTree {
|
||
5 years ago
|
errorMessage_transp(fn, root, sel, repl, rootA, rootAS)
|
||
5 years ago
|
}
|
||
5 years ago
|
parentSol := correct.ParentList(rootA)
|
||
5 years ago
|
parentStu := parentListTransp(rootAS)
|
||
|
|
||
|
if parentSol != parentStu {
|
||
|
fmt.Println("Tree:\n", solTree)
|
||
5 years ago
|
lib.Fatalf("Expected\n%s instead of\n%s\n", parentSol, parentStu)
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
root := &correct.TreeNode{Data: "04"}
|
||
5 years ago
|
rootS := &student.TreeNode{Data: "04"}
|
||
5 years ago
|
rootOr := &correct.TreeNode{Data: "04"}
|
||
5 years ago
|
|
||
5 years ago
|
replacement := &correct.TreeNode{Data: "55"}
|
||
5 years ago
|
replacementS := &student.TreeNode{Data: "55"}
|
||
|
|
||
|
ins := []string{"01", "07", "05", "12", "02", "03", "10"}
|
||
|
|
||
|
rep := []string{"33", "12", "15", "60"}
|
||
|
|
||
|
for _, v := range ins {
|
||
5 years ago
|
root = correct.BTreeInsertData(root, v)
|
||
5 years ago
|
rootS = student.BTreeInsertData(rootS, v)
|
||
5 years ago
|
rootOr = correct.BTreeInsertData(rootOr, v)
|
||
5 years ago
|
}
|
||
|
|
||
|
for _, v := range rep {
|
||
5 years ago
|
replacement = correct.BTreeInsertData(replacement, v)
|
||
5 years ago
|
replacementS = student.BTreeInsertData(replacementS, v)
|
||
|
}
|
||
|
|
||
5 years ago
|
selected := correct.BTreeSearchItem(root, "07")
|
||
5 years ago
|
selectedS := student.BTreeSearchItem(rootS, "07")
|
||
5 years ago
|
root = correct.BTreeTransplant(root, selected, replacement)
|
||
5 years ago
|
rootS = student.BTreeTransplant(rootS, selectedS, replacementS)
|
||
5 years ago
|
fn := interface{}(correct.BTreeTransplant)
|
||
5 years ago
|
|
||
5 years ago
|
CompareTrees_transp(fn, rootOr, selected, replacement, root, rootS)
|
||
5 years ago
|
}
|