Go语言实现二叉搜索树

来源:互联网 发布:福建广播网络电视台 编辑:程序博客网 时间:2024/05/29 15:56
package mainimport ("fmt""math/rand""time")const ( //常量min, max = 0, 100)var (A    []intn    introot *BTNode = &BTNode{})type BTNode struct {m_value intm_left  *BTNodem_right *BTNodeparent  *BTNode}func Init() bool { //初始化切片if n > 0 {rand := rand.New(rand.NewSource(time.Now().UnixNano())) //时间种子for i := 0; i < n; i++ {A = append(A, (rand.Intn(max-min) + min)) //生成随机数}return true} else {return false}}//插入一个节点到二叉树中func TREE_INSERT(v int) { //递归y := &BTNode{}z := &BTNode{}z.m_value = vx := rootif x != nil {for {y = x //y始终是x的父节点if v < x.m_value {x = x.m_left} else {x = x.m_right}if x == nil {break}}}z.parent = yif y.m_left == nil && y.m_right == nil && y.parent == nil { //树为空root = z//fmt.Println(root.m_value)} else {if v < y.m_value {y.m_left = z} else {y.m_right = z}}}func Create_Tree() { //插入一棵树length := len(A)for i := 0; i < length; i++ {TREE_INSERT(A[i])}}func TREE_SEARCH(root *BTNode, x int) *BTNode { //查询if root == nil || x == root.m_value {return root} else {if x < root.m_value {return TREE_SEARCH(root.m_left, x)} else {return TREE_SEARCH(root.m_right, x)}}}func INORDER_TREE_WALK(root *BTNode) { //中序打印一棵二叉树if root != nil {INORDER_TREE_WALK(root.m_left)fmt.Print(root.m_value, " ")INORDER_TREE_WALK(root.m_right)}}func TRANSPLANT(root, u, v *BTNode) {if u.parent == nil {root = v} else {if u == u.parent.m_left {u.parent.m_left = v} else {u.parent.m_right = v}}if v != nil {v.parent = u.parent}}func TREE_MINIMUM(root *BTNode) *BTNode {if root.m_left != nil {for { //由于Go中没有while语句root = root.m_leftif root.m_left == nil {break}}}return root}func TREE_DELETE(z *BTNode) {y := &BTNode{}if z.m_left == nil {TRANSPLANT(root, z, z.m_right)} else {if z.m_right == nil {TRANSPLANT(root, z, z.m_left)} else { //z有左右两个子树y = TREE_MINIMUM(root.m_right)if y.parent != z {TRANSPLANT(root, y, y.m_right)y.m_right = z.m_righty.m_right.parent = y}TRANSPLANT(root, z, y)y.m_left = z.m_lefty.m_left.parent = y}}}func main() {fmt.Println("请输入元素的个数:")fmt.Scanln(&n)if Init() {fmt.Println(A)t1 := time.Now()Create_Tree()INORDER_TREE_WALK(root)fmt.Println()//删除操作s := TREE_SEARCH(root, A[2])TREE_DELETE(s)INORDER_TREE_WALK(root)elapsed := time.Since(t1)fmt.Println("运行时间: ", elapsed)} else {fmt.Println("你输入的数据存在错误\a")}}

原创粉丝点击