package binarytree
import "fmt"
type BTree struct {
Root *Node
}
func calculateDepth(n *Node, depth int) int {
if n == nil {
return depth
}
return Max(calculateDepth(n.left, depth+1), calculateDepth(n.right, depth+1))
}
func Insert(root *Node, val int) *Node {
if root == nil {
return NewNode(val)
}
if val < root.val {
root.left = Insert(root.left, val)
} else {
root.right = Insert(root.right, val)
}
return root
}
func (t *BTree) Depth() int {
return calculateDepth(t.Root, 0)
}
func InOrder(n *Node) {
if n != nil {
InOrder(n.left)
fmt.Print(n.val, " ")
InOrder(n.right)
}
}
func InOrderSuccessor(root *Node) *Node {
cur := root
for cur.left != nil {
cur = cur.left
}
return cur
}
func BstDelete(root *Node, val int) *Node {
if root == nil {
return nil
}
if val < root.val {
root.left = BstDelete(root.left, val)
} else if val > root.val {
root.right = BstDelete(root.right, val)
} else {
if root.left == nil {
return root.right
} else if root.right == nil {
return root.left
} else {
n := root.right
d := InOrderSuccessor(n)
d.left = root.left
return root.right
}
}
return root
}
func PreOrder(n *Node) {
if n == nil {
return
}
fmt.Print(n.val, " ")
PreOrder(n.left)
PreOrder(n.right)
}
func PostOrder(n *Node) {
if n == nil {
return
}
PostOrder(n.left)
PostOrder(n.right)
fmt.Print(n.val, " ")
}
func LevelOrder(root *Node) {
var q []*Node
var n *Node
q = append(q, root)
for len(q) != 0 {
n, q = q[0], q[1:]
fmt.Print(n.val, " ")
if n.left != nil {
q = append(q, n.left)
}
if n.right != nil {
q = append(q, n.right)
}
}
}