go的二叉树,二叉搜索树实现

二叉树

  1 package main
  2 
  3 import "fmt"
  4 
  5 type node struct {
  6     val   int
  7     left  *node
  8     right *node
  9 }
 10 
 11 type btree struct {
 12     root *node
 13 }
 14 
 15 func max(a, b int) int {
 16     if a > b {
 17         return a
 18     }
 19     return b
 20 }
 21 
 22 func newNode(val int) *node {
 23     n := &node{val, nil, nil}
 24     return n
 25 }
 26 
 27 func inorder(n *node) {
 28     if n == nil {
 29         return
 30     }
 31     inorder(n.left)
 32     fmt.Print(n.val, " ")
 33     inorder(n.right)
 34 }
 35 
 36 func preorder(n *node) {
 37     if n == nil {
 38         return
 39     }
 40     fmt.Print(n.val, " ")
 41     inorder(n.left)
 42     inorder(n.right)
 43 }
 44 
 45 func postorder(n *node) {
 46     if n == nil {
 47         return
 48     }
 49     inorder(n.left)
 50     inorder(n.right)
 51     fmt.Print(n.val, " ")
 52 }
 53 
 54 func levelorder(root *node) {
 55     var q []*node
 56     var n *node
 57     q = append(q, root)
 58 
 59     for len(q) != 0 {
 60         n, q = q[0], q[1:]
 61         fmt.Print(n.val, " ")
 62 
 63         if n.left != nil {
 64             q = append(q, n.left)
 65         }
 66         if n.right != nil {
 67             q = append(q, n.right)
 68         }
 69     }
 70 }
 71 
 72 func _calculate_depth(n *node, depth int) int {
 73     if n == nil {
 74         return depth
 75     }
 76     return max(_calculate_depth(n.left, depth+1),
 77         _calculate_depth(n.right, depth+1))
 78 }
 79 
 80 func (t *btree) depth() int {
 81     return _calculate_depth(t.root, 0)
 82 }
 83 
 84 func main() {
 85     t := btree{nil}
 86     t.root = newNode(0)
 87     t.root.left = newNode(1)
 88     t.root.right = newNode(2)
 89     t.root.left.left = newNode(3)
 90     t.root.left.right = newNode(4)
 91     t.root.right.left = newNode(5)
 92     t.root.right.right = newNode(6)
 93     t.root.right.right.right = newNode(10)
 94 
 95     inorder(t.root)
 96     fmt.Print(" ---inorder\n")
 97     preorder(t.root)
 98     fmt.Print(" ---preorder\n")
 99     postorder(t.root)
100     fmt.Print(" ---postorder\n")
101     levelorder(t.root)
102     fmt.Print(" ---levelorder\n")
103     fmt.Print(t.depth(), " ---depth\n")
104 }

二叉搜索树

  1 package main
  2 
  3 import "fmt"
  4 
  5 type node struct {
  6     data  int
  7     left  *node
  8     right *node
  9 }
 10 
 11 type btree struct {
 12     root *node
 13 }
 14 
 15 func max(a, b int) int {
 16     if a > b {
 17         return a
 18     }
 19     return b
 20 }
 21 
 22 func newNode(data int) *node {
 23     return &node{data, nil, nil}
 24 }
 25 
 26 func inorder(n *node) {
 27     if n != nil {
 28         inorder(n.left)
 29         fmt.Print(n.data, " ")
 30         inorder(n.right)
 31     }
 32 }
 33 
 34 func insert(root *node, data int) *node {
 35     if root == nil {
 36         return newNode(data)
 37     }
 38     if data < root.data {
 39         root.left = insert(root.left, data)
 40     } else {
 41         root.right = insert(root.right, data)
 42     }
 43     return root
 44 }
 45 
 46 func inorderSuccessor(root *node) *node {
 47     cur := root
 48     for cur.left != nil {
 49         cur = cur.left
 50     }
 51     return cur
 52 }
 53 
 54 func bstDelete(root *node, data int) *node {
 55     if root == nil {
 56         return nil
 57     }
 58     if data < root.data {
 59         root.left = bstDelete(root.left, data)
 60     } else if data > root.data {
 61         root.right = bstDelete(root.right, data)
 62     } else {
 63         // this is the node to delete
 64         // node with one child
 65         if root.left == nil {
 66             root = root.right
 67         } else if root.right == nil {
 68             root = root.left
 69         } else {
 70             // node with two children
 71             d := inorderSuccessor(root)
 72             root.data = d.data
 73             root.right = bstDelete(root.right, d.data)
 74         }
 75     }
 76     return root
 77 }
 78 
 79 func calculateDepth(n *node, depth int) int {
 80     if n == nil {
 81         return depth
 82     }
 83     return max(calculateDepth(n.left, depth+1),
 84         calculateDepth(n.right, depth+1))
 85 }
 86 
 87 func (t *btree) depth() int {
 88     return calculateDepth(t.root, 0)
 89 }
 90 
 91 func main() {
 92     t := &btree{nil}
 93     inorder(t.root)
 94     t.root = insert(t.root, 10)
 95     t.root = insert(t.root, 20)
 96     t.root = insert(t.root, 15)
 97     t.root = insert(t.root, 30)
 98     fmt.Print("t.depth: ", t.depth(), "\n")
 99 
100     inorder(t.root)
101     fmt.Print(" ---inorder\n")
102 
103     t.root = bstDelete(t.root, 10)
104     inorder(t.root)
105     fmt.Print(" ----after delete 10\n")
106 
107     t.root = bstDelete(t.root, 30)
108     inorder(t.root)
109     fmt.Print("  ---after delete 30\n")
110 
111     t.root = bstDelete(t.root, 15)
112     inorder(t.root)
113     fmt.Print("  ---after delete 15\n")
114 
115     t.root = bstDelete(t.root, 20)
116     inorder(t.root)
117     fmt.Print(" ---after delete 20\n")
118 
119     fmt.Print(t.depth(), " ---depth\n")
120 }

 

posted @ 2019-12-03 15:35  尘归风  阅读(427)  评论(0)    收藏  举报