Photo by Kenny Eliason on Unsplash
Binary Tree/ N-Ary Tree
2 min read
Hello Everyone 🙋🏻♂️,
In this article, we will try to understand what are binary trees, as we go along we will be exploring different tree data structures so we must have a basic understanding of the binary tree.
A binary tree is a tree comprising nodes, with each node having at most two other nodes referred to as the left child and the right child of the node.
Each node contains three components data element, pointer to the left child and pointer to the right child, they are recursively defined. If there is no child the pointer points to null.
A data element can be any type of data, while the left and the right pointer point to binary trees on the left and the right side of the node respectively.
The image below represents a binary tree and the node structure if you extract a node element.
Binary Tree in Python
class BinaryTree: def __init__(self, data): self.data = data self.left = None self.right = None root_node = Node(1)
Some important definitions
- The topmost node is called the root node.
- The nodes that are connected downwards and are next to each other are called children.
- The node without any children is called a leaf node.
- Node with at least one child is called an internal node.
- Node without any child is called an external node.
- Nodes with the same parent are called siblings.
Types of Binary Trees
- Full Binary Tree
- Complete Binary Tree
- Perfect Binary Tree
- Degenerate Binary Tree
- Balanced Binary Tree
I hope this article gave you a basic understanding of the binary tree. We will be exploring each type of BT in the later articles.
Thanks for reading 🙇🏻♂️