What is a binary tree?
A binary tree is a data structure in which each node can have at most two children:
- a left child
- a right child
Structure
javascript
A
/ \
B C
/ \
D EHere:
Ais the root,BandCare children ofA,Bhas two children (DandE),Cis a leaf (no children).
Features
- Each node has up to 2 edges (unlike a general tree, where it can have more).
- Simplifies the implementation of search, sorting, and traversal.
Main types
- Complete - every level is filled except possibly the last, and the elements in it are arranged left to right.
- Perfect - every level is fully filled.
- Balanced - the height of the left and right subtrees differs by no more than 1.
- Binary search tree (BST) - the left subtree contains elements smaller than the parent, the right subtree contains larger ones.
Summary
A binary tree is a hierarchical structure in which each node is connected to at most two children, which makes it the basis for many search, sorting, and data-storage algorithms.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.