Unit4 Lect2 BinaryTree 2
Unit4 Lect2 BinaryTree 2
Trees
• Basic Tree Terminologies, Different types of Trees: Binary Tree, Threaded
Binary Tree, Binary Search Tree, AVL Tree; Tree operations on each of the
trees and their algorithms with complexity analysis.
• Applications of Binary Trees, B Tree, B+ Tree: definitions, algorithms and
analysis.
Contain
• Types of tree:
• Binary tree
https://www.youtube.com/watch?v=QhIM-G7FAow
Types of tree
• Binary tree
• Binary search tree
• Threaded Binary tree
• AVL(Self balancing binary search tree)
Why is Binary Tree is so widely
used?
• Binary Tree is the most widely used Data Structure because:
• Binary Tree is the most simplest and efficient data structure to be used in
most Software Systems. It is the properties of Binary Tree that makes it so
widely used.
• N-array Tree which is the generalization of Binary Tree is complex to
implement and is rarely a better fit.
• Binary Tree can be implemented as an array using ideas of Binary Heap.
Hence, the ideas of OOP (Object Oriented Programming) is not necessary
for a safe implementation.
• There is a wide range of variants of Binary Tree which makes it very likely
to find a suitable variant for a specific problem. For example, if we want a
Data Structure where recently accessed elements are closer to the
beginning of the data structure so that access is fast, then we have a
variant of Binary Tree known as Splay Tree.
Binary tree
• Binary tree is a special tree data structure in which each node can have at most 2
children. Thus, in a binary tree, Each node has either 0 child or 1 child or 2 children.
Types of Binary Tree
1.Rooted Binary Tree
2.Full / Strictly Binary Tree
3.Complete / Perfect Binary Tree
4.Almost Complete Binary Tree
5.Skewed Binary Tree
Rooted Binary Tree (
• A rooted binary tree is a binary tree that satisfies the following 2 properties-
• It has a root node.
• Each node has at most 2 children.
Full / Strictly Binary Tree
• A binary tree in which every node has either 0 or 2 children is called as a Full binary tree.
• Full binary tree is also called as Strictly binary tree.
Here,
•First binary tree is not a full binary
tree.
•This is because node C has only 1
child.
Complete / Perfect Binary Tree
A complete binary tree is a binary tree that satisfies the following 2 properties-
• Every internal node has exactly 2 children.
• All the leaf nodes are at the same level.
Complete binary tree is also called as Perfect binary tree.
Here,
•First binary tree is not a complete binary
tree.
•This is because all the leaf nodes are not at
the same level.
Almost Complete Binary Tree
• An almost complete binary tree is a binary tree that satisfies the following 2 properties-
• All the levels are completely filled except possibly the last level.
• The last level must be strictly filled from left to right.
Here,
•First binary tree is not an almost complete binary
tree.
•This is because the last level is not filled from left to
right.
Skewed Binary Tree
A skewed binary tree is a binary tree that satisfies the following 2 properties-
• All the nodes except one node has one and only one child.
• The remaining node has no child. OR
• A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
Binary tree property
1]Minimum number of nodes in a binary tree of height H = H + 1
Here,
•Number of leaf nodes = 3
•Number of nodes with 2 children = 2
NOTE
It is interesting to note that-
Number of leaf nodes in any binary tree depends only on
the number of nodes with 2 children.
Binary tree property
4] Maximum number of nodes at any level ‘L’ in a binary tree = 2 L
Maximum number of nodes at level-2 in a binary tree
= 22
=4
Thus, in a binary tree, maximum number of nodes that can be present at level-2 = 4.
Binary Tree
Representation
A node of a binary tree is represented by a structure containing
a data part and two pointers to other structures of the same
type.
Application of Binary Trees:
Search algorithms: Binary search algorithms use the structure of binary trees to
efficiently search for a specific element. The search can be performed in O(log n)
time complexity, where n is the number of nodes in the tree.
Sorting algorithms: Binary trees can be used to implement efficient sorting
algorithms, such as binary search tree sort and heap sort.
Database systems: Binary trees can be used to store data in a database system,
with each node representing a record. This allows for efficient search operations
and enables the database system to handle large amounts of data.
File systems: Binary trees can be used to implement file systems, where each
node represents a directory or file. This allows for efficient navigation and
searching of the file system.
Compression algorithms: Binary trees can be used to implement Huffman
coding, a compression algorithm that assigns variable-length codes to characters
based on their frequency of occurrence in the input data.
Decision trees: Binary trees can be used to implement decision trees, a type of
machine learning algorithm used for classification and regression analysis.
Game AI: Binary trees can be used to implement game AI, where each node
represents a possible move in the game. The AI algorithm can search the tree to
find the best possible move.
Applications of Binary Tree:
• Balanced Binary Search Tree is used to represent memory to enable fast memory
allocation.
• Huffman Tree (Binary Tree variant) is used internally in a Greedy Algorithm for
Data Compression known as Huffman Encoding and Decoding.
• Merkle Tree/ Hash Tree (Binary Tree variant) is used in Blockchain implementations
and p2p programs requiring signatures.
• Binary Tries (Tries with 2 child) is used to represent a routing data which vacillate
efficient traversal.
• Morse code is used to encode data and uses a Binary Tree in its representation.
• Goldreich, Goldwasser and Micali (GGM) Tree (Binary Tree variant) is used compute
pseudorandom functions using an arbitrary pseudorandom generator.
• Scapegoat tree (a self-balancing Binary Search Tree) is used in implementing Paul-
Carole games to model a faulty search process.
• Treap (radomized Binary Search Tree)
Real-time applications of Binary Trees:
DOM in HTML.
File explorer.
Used as the basic data structure in Microsoft Excel and
spreadsheets.
Editor tool: Microsoft Excel and spreadsheets.
Evaluate an expression
Routing Algorithms
Practice problem
• 1] A binary tree T has n leaf nodes. The number of nodes of degree-2 in T is ______?
1. log2n
2. n-1
3. n
4. 2n
Solution-
Using property-3, we have-
Number of degree-2 nodes
= Number of leaf nodes – 1
=n–1
• Solution-
• Let us assume any random value of h. Let h = 3.
• Then the given options reduce to-
1. 4
2. 5
3. 7
4. 8
5] A binary tree T has 20 leaves. The number of nodes in T having 2 children is ______?
• Solution
Using property-3, correct answer is 19.
• To watch video solutions and practice more problems,
Complexity of different operations in Binary tree
• Despite the wide use of Binary Tree, there are a few Data Structures that
have found strong use case and Binary Tree cannot replace them in terms
of performance.
Alternatives to Binary Trees are:
• B-Tree and B+ Tree: used in indexing of database
• Space Partitioning Tree: For higher dimensional games
• Quadtree
• Tree pyramid (T-pyramid)
• Octree
• k-d (K dimensional) tree
• R-tree: to find shortest path or nearby objects in 3D graphs