...

/

Introduction to Binary Trees

Introduction to Binary Trees

Let’s learn about the various types of binary trees.

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:

  1. The maximum number of nodes on a level i of a binary tree can be 2i2^{i} ...