This document discusses binary trees and their properties. It begins with defining key terms like root, parent, child, internal and external nodes. It then explains the differences between linear and non-linear data structures, providing examples of each. The document goes on to cover binary tree representations, properties such as balance factors, and applications of binary trees like search engines and game AI. It concludes by listing references for further reading.
1. Data Structures
Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423603
(An Autonomous Institute Affiliated to Savitribai Phule Pune University, Pune)
NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified
Department of Information Technology
(NBAAccredited)
Ms. K. D. Patil
Assistant Professor
2. Tree
• General Trees, Tree Terminology, Binary Trees, Use binary trees,
Conversion of general tree to binary tree, Array Based
representation of Binary Tree, Binary tree as an ADT, Binary tree
traversals - recursive and non-recursive algorithms, Construction
of tree from its traversals, Huffman coding algorithm
Data Structures Mrs. Kanchan Patil Department of Information Technology
3. Tree
• Linear Vs. Non-linear Data Structure
• Introduction to Tree
• Basic Tree Terminologies
• Example
Data Structures Mrs. Kanchan Patil Department of Information Technology
4. Learning Outcomes
• At the end of this lecture, student will able to-
• Differentiate linear and Non-linear Data Structures
• Introduce Tree data structure
• Explain different terms related to Tree data structure
Data Structures Mrs. Kanchan Patil Department of Information Technology
5. Recap!!!
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Data Structures: It is a particular way of organizing data in a computer.
Reference https://www.geeksforgeeks.org/difference-between-linear-and-non-linear-data-structures/
6. Linear Vs. Non-Linear Data Structures
Data Structures Mrs. Kanchan Patil Department of Information Technology
Sr. No. Linear Data Structure Non-Linear Data Structure
1 Data elements are arranged in a linear order. Elements
are attached to its previous and next adjacent.
Data elements are attached in hierarchically manner.
2 Single level is involved. Multiple levels are involved.
3 Its implementation is easy in comparison to non-linear
data structure.
Its implementation is complex in comparison to linear
data structure.
4 Data elements can be traversed in a single run only. Data elements can’t be traversed in a single run only.
5 Memory is not utilized in an efficient way. Memory is utilized in an efficient way.
6 Examples: array, stack, queue, linked list, etc. Examples: trees and graphs.
7 Applications:
Stack: undo, redo functions in editor
Queue: CPU job scheduling
Arrays: Storage of matrices
Linked List: Polynomial implementation for
mathematical operations
Applications: In Artificial Intelligence and image
processing.
Tree: 1. Implementing the hierarchical structures in
computer systems like directory and file system.
2. Implementing the navigation structure of a website.
Graph: Routes in GPS
7. Introduction to Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Non-linear data structure
• It has a structure that forms a parent-child relationship
• Collection of nodes and edges, the edges could be directed or undirected
• Definition (recursively):
• A tree is a finite set of one or more nodes such that
• There is a specially designated node called root.
• The remaining nodes are partitioned into n>=0 disjoint set T1,…,Tn, where
each of these sets is a tree. T1,…,Tn are called the subtrees of the root.
• Every node in the tree is the root of some subtree
9. Basic Tree Terminologies
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Node: It is a separate data structure that contains data or
value for a tree.
• Root: It is the top node in a tree. In any tree, first node is root
node.
• Parent: A node that has sub trees is the parent of the roots of
the sub-trees. The node which is a predecessor of any node is
called as Parent Node. The node which has a branch from it
to any other node is called a parent node. Parent node can
also be defined as "The node which has child / children".
10. Basic Tree Terminologies
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Children: A node connected to the previous node is known as
the child node. Each node in the tree is the child node of the
root node. The node which is descendant of any node is called
as Child Node. The node which has a link from its parent node is
called as child node. In a tree, any parent node can have any
number of child nodes.
• Siblings: Children of the same parent are siblings. The nodes
with the same parent are called Sibling nodes.
• Size: The total number of nodes present in the tree.
11. Basic Tree Terminologies
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Ancestors: All the nodes along the path from the root to the
node.
• Edge: The connecting link between any two nodes is called
as Edge. In a tree with 'N' number of nodes there will be a
maximum of 'N-1' number of edges.
• Terminal nodes (or leaf): The node which does not have a
child is called as leaf Node. Leaf has degree 0. A leaf is a
node with no child. The leaf nodes are also called
as External Nodes/Terminal Nodes.
12. Basic Tree Terminologies
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Non-terminal nodes: An internal node is a node with atleast
one child. Nodes other than leaf nodes are called as Internal
Nodes/ Non-terminal nodes. The root node is also said to be
Internal Node if the tree has more than one node.
• Degree of a node: The degree of a node is the number of sub
trees of the node. The total number of children of a node is
called as Degree of that Node.
• Degree of a tree: The highest degree of a node among all the
nodes in a tree is called as 'Degree of Tree'
13. Basic Tree Terminologies
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Level of Node: If a node is at level l, then it children are at level l+1. if, the root
node is said to be at Level 0 then children of root node are at Level 1 and the
children of the nodes which are at Level 1 will be at Level 2 and so on. Each step
from top to bottom is called as a Level and the Level count starts with '0' and
incremented by one at each level.
14. Basic Tree Terminologies
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Depth/ Height:
• The height of tree is the level of the leaf in the longest path from the root plus 1
• So, By definition,
• Height of empty tree is -1
• Example:
• Height of given tree = 3+1 =4
15. Basic Tree Terminologies
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Path: The sequence of Nodes and Edges from one
node to another node is called as path between that
two Nodes. Length of a Path is total number of nodes
in that path. In given example, the path A - B - E - J has
length 4.
• Sub-tree: A tree T is a tree consisting of a node
in T and all of its descendants in T. Each child from a
node forms a subtree recursively. Every child node will
form a subtree on its parent node.
17. Quiz…
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Root Node: A
• Parent of D and E: B
• sibling of B: C
• children of B: D and E
• External nodes : D, E, F, G, I
• Internal Nodes : A, B, C, H
• The level of E: 2
• The height (depth) of the tree: 4
• The degree of node B: 2
• The degree of the tree: 3
• The ancestors of node I: A, C, H
• The descendants of node C: F, G, H, I
18. Tree Representation
Data Structures Mrs. Kanchan Patil Department of Information Technology
• A tree is generally implemented in computer using “pointers/links”
• But, outside the computer systems, it can be represented using
• Organization chart format
• Indented list
• Parenthetical listing
19. Tree Representation
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Organization chart format
• Notations we use to represent tree is called
general tree
• Not easily implemented in computer systems
database
• Large structure may get complex
• Example:
• Computer parts list as a general tree
Computer
CPU
Controller ALU
Hardware
devices
Pen-Drive CD-ROM
20. Tree Representation
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Indented list
• Represents the assembly structure of an item
• Overcomes the disadvantage of chart format
• Sometimes, called as “goezinto” means “goes
into”
• Example:
• Computer parts list as a indented list
3. Computer
3.1 CPU
3.1.1 Controller
3.1.2 ALU
3.2 Hardware Devices
3.2.1 CD-ROM
3.2.2 Pen-Drive
21. Tree Representation
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Parenthetical listing
• It is used in algebraic expressions
• When a tree is represented in
parenthetical notations, each open
parenthesis indicates the start of new
level
• Each closing parenthesis completes
the current level and moves up one
level in the tree
• Example
• Parenthetical notation for below
tree is: A(B(D(HI)E(J))C(FG(K)))
22. Introduction to Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• A tree in which every node can have a maximum of two children is called as Binary
Tree
• It is a tree in which no node can have more than two sub-trees
• Every node can have either 0 children or 1 child or 2 children but not more than 2
children
• The sub-trees are designated as left sub-tree (left child) and the other one is right
sub-tree (right child). One or both can be NULL
• Each sub-tree is itself binary tree
• A null tree is a tree with no nodes
• Symmetry is not a tree requirement
24. Properties of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Height of Binary Tree:
• Considering number of nodes of binary tree as N,
• Maximum height Hmax is given as,
Hmax = N
• The tree with maximum height is rare, it occurs when
the entire tree is build in one direction
• Minimum height, Hmin is calculated as,
Hmin = [log2N] + 1
25. Properties of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Height of Binary Tree:
• Given a height of tree H,
• Minimum number of nodes in tree, Nmin = H
(in case of left skewed and right skewed binary tree)
• Maximum number of nodes in tree, Nmax = 𝟐𝑯 - 1
(in case of full binary tree)
• Example: Height of left hand side tree is 3, so minimum
number of nodes in tree = 3
• Height of right hand side tree is 3, so maximum number of
nodes in tree = 23-1 = 7
26. Quiz..
Data Structures Mrs. Kanchan Patil Department of Information Technology
• What will be the maximum and the minimum number of nodes in a binary tree of
height 5 are?
• Which of the following height is not possible for a binary tree with 50 nodes?
(A) 4 (B) 5 (C) 6 (D) None
Solution: Minimum height with 50 nodes = floor(log50) = 5. Therefore, height 4 is not
possible.
27. Properties of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Complete/Nearly Complete Binary Tree:
• A binary tree T is said to be complete binary tree if it has
maximum number of nodes for its height
• The maximum entry is reached when the last level is full
• A tree is considered nearly complete if it has minimum
height for it’s nodes and all the nodes in the last level are
found on left
• All the levels are completely filled except possibly the last
level and the last level has all keys as left as possible
Nearly Complete Binary Tree
Complete Binary Tree
28. Properties of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Full Binary Tree:
• A Binary Tree is a full binary tree if every node has 0 or 2 children
• It is a binary tree in which all nodes except leaf nodes have two
children
• Perfect Binary Tree:
• A Binary tree is a Perfect Binary Tree in which all the internal
nodes have two children and all leaf nodes are at the same level.
• It has 2i nodes at level i
• No. of leaf nodes = No. of internal nodes + 1
• Total no. of nodes = (2 * i) +1 where, i is internal node
Full Binary Tree
Perfect Binary Tree
29. Properties of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
31. Properties of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Balance Factor:
• The distance of a node from the root determines how efficiently it can be located
• The children of any node in a tree can be accessed by following only one path, the one
that needs to the desired node
• Shorter the tree, it is easier to locate any node in the tree
• To determine weather the tree is balanced, balance factor can be calculated
• Balance factor of a tree is the difference in height between its left and right sub-trees
• Balance factor, B can be determined by the formula,
B = HL – HR,
where, HL = Height of left subtree and HR is height of right subtree
32. Properties of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Balance Factor:
• The tree is said to be balanced, if its balance factor is 0 and its sub-trees are also
balanced.
• As, A binary tree is balanced if the height of its sub-trees differs by no more than one
(it can be -1, 0, +1) and its sub-trees are also balanced
A
33. Properties of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Example:
35. Properties of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Binary Tree Structure:
• Each node contains data and two pointers, one to point left sub-tree and another to
point right sub-tree
Node
• left subtree <pointer to node>
• data <data type>
• right subtree <pointer to node>
End node
36. Advantages of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Searching in Binary tree become faster
• Binary tree provides six traversals
• Two of six traversals give sorted order of elements
• Maximum and minimum elements can be directly picked up
• It is used for graph traversal and to convert an expression to postfix and prefix forms
37. Applications of Binary Tree
Data Structures Mrs. Kanchan Patil Department of Information Technology
• Search Engines: Organize and index web pages
• File System: Organize and index files
• Searching and Sorting: Binary Search, quicksort, merge sort
• Expression Tree: To represent mathematical expressions
• Huffman Coding: data compression
• Decision Tree: Machine Learning and Data Mining model
• Game AI: to model different possibilities and outcome of game
• TRIE: Storage and string retrieval
• Cryptography: generate and manage encryption, decryption keys
38. References
Data Structures Mrs. Kanchan Patil Department of Information Technology
• R. Lafore, “Data structures and Algorithms in Java”, Pearson education, ISBN: 9788
131718124.
• Michael Goodrich, Roberto Tamassia, Michael H. Goldwasser, “Data Structures and
Algorithms in Java”, 6th edition, wiley publication, ISBN: 978-1-118-77133-4
• R. Gilberg, B. Forouzan, “Data Structure: A Pseudo code approach with C++”, Cengage
Learning.
• Sartaj Sahni, “Data Structures, Algorithms and Applications in C++”, 2 nd Edition,
Universities Press.
• E. Horowitz, S. Sahni, S. Anderson-freed, “Fundamentals of Data Structures in C”, 2 nd
Edition, University Press, ISBN 978-81-7371-605-8.
• https://www.geeksforgeeks.org/difference-between-linear-and-non-linear-data-
structures/
• http://www.btechsmartclass.com/data_structures/tree-terminology.html
39. Data Structures Mrs. Kanchan Patil Department of Information Technology
Thank You!!!
Happy Learning!!!