Binary tree can be classified based on its structure,
Complete binary tree, if all the levels are completely filled except possibly the last level and the nodes in the last level are as left as possible. In other words, a binary tree where nodes are filled like left to right at each depth(top to down).
Full binary tree, All the nodes in such a binary tree possesses either 0 or 2 children.
Perfect binary tree
It must be a Full binary tree
All the leaves must be at the same level
Full binary tree and Perfect binary tree, have no any standard definition. The aforementioned definition is more widely preferred.
There are some lesser known types also such as incomplete binary tree, strict binary tree, almost complete binary tree, degenerate tree etc.
For full binary tree and complete binary tree, array is used. See below, an array binTree of size 7 is created, each node of the shown binary tree is stored as root at index 0, left child of root at index 1 and so on. If the parent is at index i then left and right child at 2i and 2i+1 respectively.
Pointer based implementation, Under this, a node is created like we do for the linked list. There will be a structure that contains a key, a pointer to the left subtree and a pointer to the right subtree.
The same known operation Insertion, Deletion, Searching, Traversal and some others are applied to the binary tree. Howsoever, a binary tree is implemented with modification to have a more optimized operation. Binary Search Tree(BST), Balanced BST are some of the modified binary trees. Thus, operations on binary tree are more emphasized on BST and Balanced BST rather than plainly applying on it.
Q1) What are the maximum number of nodes in a binary tree of height h(that is a perfect binary tree)? Ans) For height h – maximum number of noder are 2(h+1) -1, where height of the leaf node is zero
Q2) What is the relationship between leaf node and an internal node? Ans) In a binary tree, the number of leaf node is always one more than the number of internal nodes with two children.
Q3) What is the height of a complete binary tree? Ans) Height of tree = ⌊logn⌋, where n is the total number of nodes
Q4) What is the number of internal nodes in a tree? Ans) Internal nodes = ⌊(n/2)⌋ + 1 to n, where n is the total number of nodes
Heap tree, used to implement priority queue, and in dynamic memory allocation
Huffman coding used in compression/decompression
Knowledge is most useful when liberated and shared. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help. Keep Learning… Happy Learning.. 🙂
Binary Tree – Introduction, Explanation and Implementation was last modified: October 28th, 2016 by Vivek Kumar