In this article, you will learn about the different Binary tree types in Data structure.
A binary tree in data structure is an ordered tree where each node has a maximum of two children. These are referred to as the left and right child. As per the set theory notions, a binary tree is a tuple (L, S, R), in which L and R are binary trees or empty sets and S is a singleton set that contains the root. The aim of this article is to develop an understanding about binary trees in data structure. Let’s discuss their types and uses.
Binary Tree Types in Data Structure
There are seven types of binary tree in data structure. Let’s know more about these trees in detail.
1. Rooted Binary Tree
A rooted binary tree is the type of binary tree in which the root is allowed to have only degree 2 which means a root node and every node can have a maximum of two children. A rooted binary tree is connected acyclic graph that has a special node called the root of tree and each edge originates directly or indirectly from the root.
An ordered rooted tree is a rooted tree in which the children of every internal vertex are ordered. An m-ary tree is a type of rooted tree in which every internal vertex does not have more than m children. If every internal vertex has exactly m children, it is a full m-ary tree. If m = 2, then this rooted tree is a binary tree.
2. Full Binary Tree
Full binary tree in data structure is a special kind of binary tree that has either zero or two children. It means that all the nodes in that binary tree should either have two child nodes of its parent node or the parent node is itself the leaf node or the external node. In other words, a full binary tree is a unique binary tree where every node except the external node has two children. When it holds a single child, such a binary tree will not be a full binary tree.
Here, the quantity of leaf nodes is equal to the number of internal nodes plus one. The equation is like L=I+1, where L is the number of leaf nodes, and I are the number of internal nodes. A full binary tree is either a single vertex and tree having root node has two subtrees. both of which are complete binary trees.
Learn more about tree data structures
3. Complete Binary Tree
A complete binary tree in data structure is completely filled at every level possible except at last. All leaf nodes lean towards the left. There is a possibility that left leaf nodes might not have right nodes. You can find out parents and children of any node using a complete binary tree.
Check out data structure courses
4. Perfect Binary Tree
All interior nodes in a perfect binary tree have two children and every leaf have the same depth. A perfect binary tree is always complete. A tree is considered to be a perfect binary tree if:
- A single node has no children then it is a perfect binary tree having height h = 0 and
- A node having h > 0 is a perfect binary tree if both of its subtrees have height h – 1 and both are non-overlapping.
5. Infinite Complete Binary Tree
Every node in an infinite complete binary tree has two children The set of all nodes is countably infinite while the set of all infinite paths from the root is uncountable due to the cardinality of the continuum. This is due to the fact that these paths correspond by order-preserving bijection to the points of Cantor set, or to the set of positive irrational numbers.
6. Balance Binary Tree
Balance binary tree in data structure is a type of binary tree in which no leaf is farther away from the root than any other leaf. A balance binary tree has a height equal to O(logN). Red-Black Tree and AVL Tree are its examples. It is also important that left and right subtrees are balanced and do not differ in height by more than 1.
Learn Data Structures Fundamentals Today!
7. Degenerate Binary Tree
A degenerate tree is a binary tree in data structure in which each parent node has only one child node associated with it. Such a tree will behave in the manner of linked list data structure. If all the nodes in the degenerate tree have only left child, then it is known as a left-skewed tree. If all the nodes have only right child, then it is known as the right-skewed tree.
How to check if a tree is height-balanced?
You can check whether a binary tree is height-balanced in Python through the following: class Node: u00a0 u00a0 u00a0 u00a0def __init__(self, data): u00a0u00a0u00a0u00a0u00a0u00a0u00a0u00a0self.data = data u00a0u00a0u00a0u00a0u00a0u00a0u00a0u00a0self.left = self.right = None class Height: u00a0u00a0u00a0u00a0def __init__(self): u00a0u00a0u00a0u00a0u00a0u00a0u00a0u00a0self.height = 0 def isHeightBalanced(root, height): u00a0u00a0u00a0u00a0left_height = Height() u00a0u00a0u00a0u00a0right_height = Height() u00a0u00a0u00a0u00a0if root is None: u00a0u00a0u00a0u00a0u00a0u00a0u00a0u00a0return True u00a0u00a0u00a0u00a0l = isHeightBalanced(root.left, left_height) u00a0u00a0u00a0u00a0r = isHeightBalanced(root.right, right_height) u00a0u00a0u00a0u00a0height.height = max(left_height.height, right_height.height) + 1 u00a0u00a0u00a0u00a0if abs(left_height.height - right_height.height)
State two properties of a full binary tree.
Let's consider nodes to be n, height if the tree to be h then: The number of nodes in a full binary tree can be at least 2h + 1 and it can be at most 2h+ 1- 1. Any tree that consists of only a single root node has h= 0. Null links in a binary tree that has u2018nu2019 nodes will be equal to (n+1).
What are the primary uses of binary trees?
There are primarily two ways for using binary trees including: 1. Binary trees in data structure can be used as a means to access nodes based on some value or label associated with every node. Binary trees that are labelled in such a way are used for implementing binary heaps and binary search trees. These trees are used for searching and sorting as well.u00a0 2. These are used as the representation of data along with relevant bifurcating structure. In these cases, the particular arrangement of nodes under and to the left or right of other nodes is a part of information Huffman coding and cladograms are such examples.u00a0
What are the operations that can be performed on binary trees?
Insertion, Deletion and Traversal are the three operations that can be performed on binary trees.
Explain the u2018insertionu2019 operation for a binary tree in data structure?
Insertion operation is used for inserting nodes either between two nodes or after leaf nodes. A leaf node assigns a new node as one of its children and this new node assigns a leaf node as its parent.
How can you find the parent of any node?
You can find parents of any node in the following example: Parent of 9 (position 2) =(2-1)/2 =1/2 =0.5 ~ 0 index = 1 The parent of any node N > 0 in such array will always be at the index (N-1)/2
What is Depth-first search?
Depth-first search is an algorithm to traverse and search tree or graph data structures. The algorithm starts from the root node and explores as far as possible along every branch before backtracking.
What is a breadth-first search?
Breadth-first search is an algorithm used to search a tree data structure for a node that satisfies a given property. It starts from the tree root and explores every node at the present depth before moving on to the nodes present at the next depth level. Extra memory is required for keeping a track of child nodes that were encountered but are not yet explored.
Explain the deletion operation?
Deletion is the operation used for the removal of nodes from the tree. Only certain nodes in the binary tree can be removed unambiguously. 1.. Node with either no or one child Suppose you need to delete node n. If n has no child, deletion will take place by setting the child of nu2019s parent to null. If n has a single child, then, set the parent of n's child to n's parent and set child of n's parent to n's child. 2. Node with 2 children It is not possible to unambiguously delete a node that has two children. In some binary trees, these nodes can be deleted with the rearrangement of the binary tree structure.
What is a sentinel node?
A sentinel node is a designated node that is used with linked lists and trees as the traversal path terminator. This node does not hold any data that is managed by the data structure.
How can you find the number of nodes in a full binary tree and a perfect full binary tree in data structure?
Let's say that leaves are l. Then, in a full binary tree, n= 2l-1 and in perfect full binary structure, n= 2h+ 1- 1.
What is the use of a binary search tree?
A binary search tree helps in the quick search, insert, delete on the sorted data. You can find the closest item using this search tree.
What is an AVL tree?
AVL tree is the height balancing search tree that checks the height of left and right sub trees and ensures the difference is not more than 1.