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.
127 lines
2.3 KiB
127 lines
2.3 KiB
5 years ago
|
package main
|
||
5 years ago
|
|
||
|
import (
|
||
5 years ago
|
student "student"
|
||
|
|
||
5 years ago
|
"./correct"
|
||
5 years ago
|
"github.com/01-edu/public/go/lib"
|
||
5 years ago
|
)
|
||
|
|
||
|
func parentListInsert(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 += parentListInsert(root.Left) + parentListInsert(root.Right)
|
||
|
return r
|
||
|
}
|
||
|
|
||
|
func FormatTree_insert(root *student.TreeNode) string {
|
||
|
if root == nil {
|
||
|
return ""
|
||
|
}
|
||
|
res := root.Data + "\n"
|
||
|
res += formatSubTree_insert(root, "")
|
||
|
return res
|
||
|
}
|
||
|
|
||
|
func formatSubTree_insert(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_insert(root.Right, newPrefix)
|
||
|
}
|
||
|
|
||
|
if hasLeft {
|
||
|
if hasRight {
|
||
|
res += prefix
|
||
|
}
|
||
|
res += "└── " + root.Left.Data + "\n"
|
||
|
res += formatSubTree_insert(root.Left, prefix+" ")
|
||
|
}
|
||
|
return res
|
||
|
}
|
||
|
|
||
5 years ago
|
func errorMessage_insert(fn interface{}, inserted string, root *correct.TreeNode, rootS *student.TreeNode) {
|
||
5 years ago
|
lib.Fatalf("%s(\n%s, %s\n) ==\n%s instead of\n%s\n",
|
||
5 years ago
|
"BTreeInsertData",
|
||
5 years ago
|
correct.FormatTree(root),
|
||
5 years ago
|
inserted,
|
||
|
FormatTree_insert(rootS),
|
||
5 years ago
|
correct.FormatTree(root),
|
||
5 years ago
|
)
|
||
|
}
|
||
|
|
||
5 years ago
|
func CompareTrees_insert(fn interface{}, inserted string, root *correct.TreeNode, rootS *student.TreeNode) {
|
||
|
solTree := correct.FormatTree(root)
|
||
5 years ago
|
stuTree := FormatTree_insert(rootS)
|
||
|
|
||
|
if solTree != stuTree {
|
||
5 years ago
|
errorMessage_insert(fn, inserted, root, rootS)
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
func main() {
|
||
5 years ago
|
root := &correct.TreeNode{Data: "08"}
|
||
5 years ago
|
rootS := &student.TreeNode{Data: "08"}
|
||
|
|
||
|
var pos []string
|
||
|
|
||
|
pos = append(pos,
|
||
|
"x",
|
||
|
"z",
|
||
|
"y",
|
||
|
"t",
|
||
|
"r",
|
||
|
"q",
|
||
|
"01",
|
||
|
"b",
|
||
|
"c",
|
||
|
"a",
|
||
|
"d",
|
||
|
)
|
||
5 years ago
|
fn := interface{}(correct.BTreeInsertData)
|
||
5 years ago
|
for _, arg := range pos {
|
||
5 years ago
|
root = correct.BTreeInsertData(root, arg)
|
||
5 years ago
|
rootS = student.BTreeInsertData(rootS, arg)
|
||
5 years ago
|
CompareTrees_insert(fn, arg, root, rootS)
|
||
5 years ago
|
}
|
||
|
}
|