0% found this document useful (0 votes)
6 views

Unit4 Lect2 BinaryTree 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit4 Lect2 BinaryTree 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

• TEXT/REFERENCE BOOKS

1. “How to Solve it by Computer”, 2nd Impression by R. G. Dromey, Pearson Education.


2. Ellis Horowitz, Sartaj Sahni, “Fundamentals of Data Structures”, Galgotia Books Source. ISBN 10:
0716782928
3. Java: The Complete Reference, Seventh Edition, Herbert Schildt, McGraw Hill
4. Richard F. Gilberg & Behrouz A. Forouzan, Data Structures: A Pseudocode Approach with C, Cengage
Learning, second edition. ISBN-10: 0534390803.
5. Seymour Lipschutz, Data Structure with C, Schaum‟s Outlines, Tata Mc Graw Hill. ISBN-10: 1259029964.
6.John Hubbard Anita Huray, “ Data Structures with JAVA”, Person Education
UNIT – 4

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

Example:To construct a binary tree of height = 4, we need at least 4 + 1 = 5 nodes.


Binary tree property
2] Maximum number of nodes in a binary tree of height H = 2 H+1 – 1
Example: Maximum number of nodes in a binary tree of height 3
= 23+1 – 1
= 16 – 1 = 15 nodes
Thus, in a binary tree of height = 3, maximum number of nodes that can be inserted = 15.
Binary tree property
3] Total Number of leaf nodes in a Binary Tree = Total Number of nodes with 2 children + 1

Here,
•Number of leaf nodes = 3
•Number of nodes with 2 children = 2

Clearly, number of leaf nodes is one greater than


number of nodes with 2 children.
This verifies the above relation.

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

• Thus, Option (B) is correct.


• 2] In a binary tree, for every node the difference between the
number of nodes in the left and right subtrees is at most 2. If
the height of the tree is h > 0, then the minimum number of
nodes in the tree is ______?
1. 2h-1
2. 2h-1 + 1
3. 2h – 1
4. 2h

• 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

• Now, consider the following binary tree with height h = 3-


3] In a binary tree, the number of internal nodes of degree-1 is 5 and the number of internal nodes
of degree-2 is 10. The number of leaf nodes in the binary tree is ______?
1. 10
2. 11
3. 12
4. 15

• Solution-

• Using property-3, we have-
• Number of leaf nodes in a binary tree
• = Number of degree-2 nodes + 1
• = 10 + 1
• = 11

• Thus, Option (B) is correct.
4] The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum
number of nodes in a binary tree of height h is ______?
1. 2h
2. 2h-1 – 1
3. 2h+1 – 1
4. 2h+1
Solution
• Using property-2, Option (C) is correct.

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

• The main operations in binary tree are: search, insert


and delete. We will see the worst case time complexity
of these operations in binary trees.
• Binary Tree –
In a binary tree, a node can have maximum two
children. Consider the left skewed binary tree shown in
Figure 1.
• Searching: For searching element 2, we have to traverse all
elements (assuming we do breadth first traversal).
Therefore, searching in binary tree has worst case
complexity of O(n).
• Insertion: For inserting element as left child of 2, we have
to traverse all elements. Therefore, insertion in binary tree
has worst case complexity of O(n).
• Deletion: For deletion of element 2, we have to traverse all
elements to find 2 (assuming we do breadth first traversal).
Therefore, deletion in binary tree has worst case complexity
of O(n).
Following are the Applications of Binary Tree:
• Binary Tree is used to as the basic data structure in Microsoft Excel
and spreadsheets in usual.
• Binary Tree is used to implement indexing of Segmented Database.
• Splay Tree (Binary Tree variant) is used in implemented efficient
cache is hardware and software systems.
• Binary Space Partition Trees are used in Computer Graphics, Back
face Culling, Collision detection, Ray Tracing and algorithms in
rendering game graphics.
• Syntax Tree (Binary Tree with nodes as operations) are used to
compute arithmetic expressions in compilers like GCC, AOCL and
others.
• Binary Heap (Binary Tree variant of Heap) is used to implement
Priority Queue efficiently which in turn is used in Heap Sort Algorithm.
• Binary Search Tree is used to search elements efficiently and used as
a collision handling technique in Hash Map implementations.
Alternatives to 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

You might also like