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.
142 lines
3.3 KiB
142 lines
3.3 KiB
5 years ago
|
package main
|
||
5 years ago
|
|
||
|
import (
|
||
|
"github.com/01-edu/z01"
|
||
|
|
||
5 years ago
|
"fmt"
|
||
|
|
||
5 years ago
|
solutions "./solutions"
|
||
|
student "./student"
|
||
|
)
|
||
|
|
||
|
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 *solutions.TreeNode, rootA *solutions.TreeNode, rootAS *student.TreeNode) {
|
||
|
z01.Fatalf("%s(\nRoot:\n %s, Selected:\n%s, Replacement:\n%s\n) ==\n%s instead of\n%s\n",
|
||
|
"BTreeTransplant",
|
||
5 years ago
|
solutions.FormatTree(root),
|
||
|
solutions.FormatTree(sel),
|
||
|
solutions.FormatTree(repl),
|
||
|
FormatTree_transp(rootAS),
|
||
|
solutions.FormatTree(rootA),
|
||
|
)
|
||
|
}
|
||
|
|
||
5 years ago
|
func CompareTrees_transp(fn interface{}, root, sel, repl *solutions.TreeNode, rootA *solutions.TreeNode, rootAS *student.TreeNode) {
|
||
5 years ago
|
solTree := solutions.FormatTree(rootA)
|
||
|
stuTree := FormatTree_transp(rootAS)
|
||
|
|
||
|
if solTree != stuTree {
|
||
5 years ago
|
errorMessage_transp(fn, root, sel, repl, rootA, rootAS)
|
||
5 years ago
|
}
|
||
|
parentSol := solutions.ParentList(rootA)
|
||
|
parentStu := parentListTransp(rootAS)
|
||
|
|
||
|
if parentSol != parentStu {
|
||
|
fmt.Println("Tree:\n", solTree)
|
||
5 years ago
|
z01.Fatalf("Expected\n%s instead of\n%s\n", parentSol, parentStu)
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
root := &solutions.TreeNode{Data: "04"}
|
||
|
rootS := &student.TreeNode{Data: "04"}
|
||
|
rootOr := &solutions.TreeNode{Data: "04"}
|
||
|
|
||
|
replacement := &solutions.TreeNode{Data: "55"}
|
||
|
replacementS := &student.TreeNode{Data: "55"}
|
||
|
|
||
|
ins := []string{"01", "07", "05", "12", "02", "03", "10"}
|
||
|
|
||
|
rep := []string{"33", "12", "15", "60"}
|
||
|
|
||
|
for _, v := range ins {
|
||
|
root = solutions.BTreeInsertData(root, v)
|
||
|
rootS = student.BTreeInsertData(rootS, v)
|
||
|
rootOr = solutions.BTreeInsertData(rootOr, v)
|
||
|
}
|
||
|
|
||
|
for _, v := range rep {
|
||
|
replacement = solutions.BTreeInsertData(replacement, v)
|
||
|
replacementS = student.BTreeInsertData(replacementS, v)
|
||
|
}
|
||
|
|
||
|
selected := solutions.BTreeSearchItem(root, "07")
|
||
|
selectedS := student.BTreeSearchItem(rootS, "07")
|
||
|
root = solutions.BTreeTransplant(root, selected, replacement)
|
||
|
rootS = student.BTreeTransplant(rootS, selectedS, replacementS)
|
||
|
fn := interface{}(solutions.BTreeTransplant)
|
||
|
|
||
5 years ago
|
CompareTrees_transp(fn, rootOr, selected, replacement, root, rootS)
|
||
5 years ago
|
}
|