Introduction to Binary Trees
Explore the fundamentals of binary trees, including their structure where each node has up to two children. Understand key properties like node limits and path uniqueness. Discover various types of binary trees, including complete, full, perfect, right-skewed, left-skewed, and height-balanced trees, with practical Go code examples for implementation.
Binary tree
As we discussed earlier, a binary tree is a type of tree in which each node has at most two children, which means a node in the binary tree can have one, two, or no children. These children are referred to as the left child and the right child.
The above diagram shows a node of the binary tree with a stored as data and whose left child and right child both pointing to null.
We can code the above node as follows:
type Node struct {
value int
left, right *Node
}
type Tree struct {
root *Node
}
Example
A binary tree whose nodes contain data from 1 to 10 is given below:
For simplicity, we can represent them as follows:
Properties of a binary tree
The properties of a binary tree are as follows:
-
The maximum number of nodes on a level
iof a binary tree can be ...