定义
每个节点最多有2个子节点(子节点个数就是度)的树。
特点/性质
1.每个节点最多有2个子树
2.二叉树的第i层上最多有2i-1个节点
3.如果二叉树深度为k,那么最多有2^k-1个节点
4.n个结点的二叉树的高度至少为log2 (n+1)
完全二叉树
所有的非叶节点的子节点个数都是2,所有的叶节点都在最后一层,且都集中在最左边。
具有n个节点的完全二叉树的深度为[log2n]+1
节点i的左右孩子为2i以及2i + 1(编号从左至右,从上到下)
满二叉树
所有叶节点都集中在最后一层,且所有非叶节点都有两个子节点
平衡二叉树(AVL树)
所有节点的左右子树的高度差不超过1
遍历方法(go语言实现)
前序遍历
每次到达一个节点则先输出该节点的信息
func (node *Node) PreRecursionOrder() {
//递归前序遍历
if node == nil {
return
}
node.Print()
node.Left.PreRecursionOrder()
node.Right.PreRecursionOrder()
}
func (node *Node) PreNoRecursionOrder() {
//非递归前序遍历
if node == nil {
return
}
stack := list.New()
stack.PushBack(node)
for stack.Len() != 0 {
last := stack.Back()
last_node := last.Value.(*Node)
last_node.Print()
stack.Remove(last)
if last_node.Right != nil {
stack.PushBack(last_node.Right)
}
if last_node.Left != nil {
stack.PushBack(last_node.Left)
}
}
}
中序遍历
当某个节点的所有左节点都已经输出则输出本节点
func (node *Node) InRecursionOrder() {
//递归中序遍历
if node == nil {
return
}
node.Left.PreRecursionOrder()
node.Print()
node.Right.PreRecursionOrder()
}
func (node *Node) InNoRecursionOrder() {
//非递归中序遍历
if node == nil {
return
}
stack := list.New()
temp := node
for temp != nil || stack.Len() != 0 {
for temp != nil {
stack.PushBack(temp)
temp = temp.Left
}
if stack.Len() != 0 {
last := stack.Back()
last_node := last.Value.(*Node)
last_node.Print()
stack.Remove(last)
temp = last_node.Right
}
}
}
后序遍历
当某个节点的所有左右节点都已经输出则输出本节点
func (node *Node) PostRecursionOrder() {
//递归后序遍历
if node == nil {
return
}
node.Left.PreRecursionOrder()
node.Right.PreRecursionOrder()
node.Print()
}
func (node *Node) PostNoRecursionOrder() {
//非递归后序遍历
if node == nil {
return
}
stack := list.New()
temp := node
var pre_visited *Node
for temp != nil || stack.Len() != 0 {
for temp != nil {
stack.PushBack(temp)
temp = temp.Left
}
last := stack.Back()
last_node := last.Value.(*Node)
if (last_node.Left == nil && last_node.Right == nil) || (last_node.Right == nil && pre_visited == last_node.Left) || (pre_visited == last_node.Right) {
//列出所有可以输出当前节点的情况(1、左右为空;2、右空左已访问;3、右已访问)
last_node.Print()
stack.Remove(last)
pre_visited = last_node
} else {
temp = last_node.Right
}
}
}
层次遍历
按层输出
func (node *Node) leverOrder() {
if node == nil {
return
}
queue := list.New()
var top *Node
queue.PushFront(node)
for queue.Len() != 0 {
top_node := queue.Front()
top = top_node.Value.(*Node)
top.Print()
queue.Remove(top_node)
if top.Left != nil {
queue.PushBack(top.Left)
}
if top.Right != nil {
queue.PushBack(top.Right)
}
}
}
获取树的高度
func (node *Node) GetDeepth() int {
if node == nil {
return 0
}
left_deepth := node.Left.GetDeepth()
right_deepth := node.Right.GetDeepth()
if left_deepth > right_deepth {
return left_deepth + 1
} else {
return right_deepth + 1
}
}
获取树的节点个数
func (node *Node) GetNum() int {
if node == nil {
return 0
} else {
return node.Left.GetNum() + node.Right.GetNum() + 1
}
}
判断树是否平衡
func (node *Node) IsBalanced() bool {
if node == nil {
return true
}
l_depth := node.Left.GetDeepth()
r_depth := node.Right.GetDeepth()
if (math.Abs(float64(l_depth - r_depth))) <= 1 {
return node.Left.IsBalanced() && node.Right.IsBalanced()
} else {
return false
}
}