Binary Tree is a non-linear and hierarchicaldata structure where each node has at most two children referred to as the left child and the right child. The topmost node in a binary tree is called the root, and the bottom-most nodes(having no children) are called leaves.
In the above code, we have created four tree nodes firstNode, secondNode, thirdNode and fourthNode with values 2, 3, 4 and 5 respectively, and then established links between those nodes wrt the given tree structure.
Properties of Binary Tree
The maximum number of nodes at level L of a binary tree is 2L.
The maximum number of nodes in a binary tree of height H is 2H+1 – 1.
Total number of leaf nodes in a binary tree = total number of nodes with 2 children + 1.
In a Binary Tree with N nodes, the minimum possible height or the minimum number of levels is ⌊log2N⌋.
A Binary Tree with L leaves has at least ⌈log2L⌉+ 1 levels.
Efficient Search: Binary Search Trees (a variation of Binary Tree) are efficient when searching for a specific element, as each node has at most two child nodes when compared to linked list and arrays
Memory Efficient: Binary trees require lesser memory as compared to other tree data structures, therefore memory-efficient.
Binary trees are relatively easy to implement and understand as each node has at most two children, left child and right child.
Disadvantages of Binary Tree
Limited structure: Binary trees are limited to two child nodes per node, which can limit their usefulness in certain applications. For example, if a tree requires more than two child nodes per node, a different tree structure may be more suitable.
Space inefficiency: Binary trees can be space inefficient when compared to other data structures like arrays and linked list. This is because each node requires two child references or pointers, which can be a significant amount of memory overhead for large trees.
Applications of Binary Tree
Binary Tree can be used to represent hierarchical data.
Huffman Coding trees are used in data compression algorithms.
Useful for indexing segmented at the database is useful in storing cache in the system,
Binary trees can be used to implement decision trees, a type of machine learning algorithm used for classification and regression analysis.