A balanced binary tree is a type of binary tree where the depth of two subtrees of every node never differ by more than one. This balance condition ensures that the tree remains approximately balanced, leading to O(log n) time complexities for insertions, deletions, and lookups, where n is the number of nodes in the tree.
In a balanced binary tree, operations like insertion and deletion might require rebalancing to maintain the tree's balanced property. There are several types of balanced binary trees, including AVL trees, Red-Black trees, and B-trees, each with its own balancing algorithm and rules.
Here is an example of the AVL tree implementation in Go (Golang), which is a self-balancing binary search tree. For simplicity, I'm providing a basic example with the key parts needed for balancing:
go
package main
import (
"fmt"
)
// TreeNode represents a node in the AVL tree
type TreeNode struct {
Value int
Height int
Left *TreeNode
Right *TreeNode
}
// NewTreeNode creates a new TreeNode with the given value
func NewTreeNode(value int) *TreeNode {
return &TreeNode{Value: value, Height: 1}
}
// GetHeight returns the height of the node or 0 if it's nil
func (n *TreeNode) GetHeight() int {
if n == nil {
return 0
}
return n.Height
}
// UpdateHeight updates the height of the node
func (n *TreeNode) UpdateHeight() {
n.Height = 1 + max(n.Left.GetHeight(), n.Right.GetHeight())
}
// BalanceFactor returns the balance factor of the node
func (n *TreeNode) BalanceFactor() int {
if n == nil {
return 0
}
return n.Left.GetHeight() - n.Right.GetHeight()
}
// RotateRight performs a right rotation on the node
func (n *TreeNode) RotateRight() *TreeNode {
x := n.Left
t2 := x.Right
x.Right = n
n.Left = t2
n.UpdateHeight()
x.UpdateHeight()
return x
}
// RotateLeft performs a left rotation on the node
func (n *TreeNode) RotateLeft() *TreeNode {
y := n.Right
t2 := y.Left
y.Left = n
n.Right = t2
n.UpdateHeight()
y.UpdateHeight()
return y
}
// Insert inserts a new value into the tree and returns the new root after rebalancing
func (n *TreeNode) Insert(value int) *TreeNode {
if n == nil {
return NewTreeNode(value)
}
if value < n.Value {
n.Left = n.Left.Insert(value)
} else if value > n.Value {
n.Right = n.Right.Insert(value)
} else {
return n // Duplicate values are not allowed
}
n.UpdateHeight()
balance := n.BalanceFactor()
// Left Left Case
if balance > 1 && value < n.Left.Value {
return n.RotateRight()
}
// Right Right Case
if balance < -1 && value > n.Right.Value {
return n.RotateLeft()
}
// Left Right Case
if balance > 1 && value > n.Left.Value {
n.Left = n.Left.RotateLeft()
return n.RotateRight()
}
// Right Left Case
if balance < -1 && value < n.Right.Value {
n.Right = n.Right.RotateRight()
return n.RotateLeft()
}
return n
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func main() {
root := NewTreeNode(10)
root = root.Insert(20)
root = root.Insert(30)
root = root.Insert(40)
root = root.Insert(50)
root = root.Insert(25)
fmt.Println("Preorder traversal of the constructed AVL tree is:")
preOrder(root)
}
// preOrder performs a preorder traversal of the AVL tree
func preOrder(node *TreeNode) {
if node != nil {
fmt.Printf("%d ", node.Value)
preOrder(node.Left)
preOrder(node.Right)
}
}
In this implementation, the TreeNode struct represents a node in the AVL tree, which contains an integer value, a height, and pointers to its left and right children. The Insert method inserts a new value into the tree while maintaining its balanced property by performing appropriate rotations.
The RotateLeft and RotateRight methods perform rotations to rebalance the tree. The BalanceFactor method calculates the balance factor of a node to determine whether it's unbalanced. If the balance factor is greater than 1 or less than -1, it means the tree is unbalanced at that node, and rotations are required to balance it.
Running the main function would insert a series of values into the AVL tree, and perform a preorder traversal to print the values. The output