定义一个长度为1000的结构体数组,用随机数生成1000个结构体的值,以第0个元素为根,依次按顺序插入,组成一个完全二叉树,然后遍历,并统计出各种节点的数量。
程序已经编译通过,且成功运行。
package main
import (
"fmt"
"math"
"math/rand"
)
type student struct {
Name string `json:"student_name"`
Age int `json:"age"`
Score float32 `json:"score"`
left *student
right *student
parent *student
}
func newStu(name string, age int, score float32) *student {
return &student{
Name: name,
Age: age,
Score: score,
}
}
var stu [1000]student
var root *student
func init() {
var name string
var age int
var score float32
for i := 0; i < 1000; i++ {
name = fmt.Sprintf("stu%d", i)
age = rand.Intn(100)
score = rand.Float32() * 100
stu[i] = *newStu(name, age, score)
}
root = &stu[0]
root.left = &stu[1]
root.right = &stu[2]
stu[1].parent = root
stu[2].parent = root
}
func tierArr(stu []student) int {
return int(math.Log2(float64