TREES
BINARY TREES
 A binary tree is a data structure which is defined as a collection of
elements called nodes. Every node contains a "left" pointer, a "right"
pointer, and a data element. Every binary tree has a root element pointed
by a "root" pointer. The root element is the topmost node in the tree. If
root = NULL, then it means the tree is empty.
 If the root node R is not NULL, then the two trees T1 and T2 are called
the left and right subtrees of R. if T1 is non-empty, then T1 is said to be
the left successor of R. likewise, if T2 is non-empty then, it is called the
right successor of R.
8
1
32
4
5 6 7
1
2
1
0
1
1
ROOT NODE
T2T1
9
In a binary tree every node has 0, 1 or at the
most 2 successors. A node that has no
successors or 0 successors is called the leaf
node or the terminal node.
KEY TERMS
 Sibling: If N is any node in T that has left successor S1 and right successor
S2, then N is called the parent of S1 and S2. Correspondingly, S1 and S2
are called the left child and the right child of N. Also, S1 and S2 are said to
be siblings. Every node other than the root node has a parent. In other
words, all nodes that are at the same level and share the same parent are
called siblings (brothers).
Level number: Every node in the binary tree is assigned a level number.
The root node is defined to be at level 0. The left and right child of the root
node has a level number 1. Similarly, every node is at one level higher than
its parents. So all child nodes are defined to have level number as parent’s
level number + 1.
 Degree: Degree of a node is equal to the number of children that a node
has. The degree of a leaf node is zero.
In-degree of a node is the number of edges arriving at that node. The root
node is the only node that has an in-degree equal to zero. Similarly,
Out-degree of a node is the number of edges leaving that node.
Leaf node: A leaf node has no children.
KEY TERMS contd.
Similar binary trees: Given two binary trees T and T’ are said to be similar if both
of these trees have the same structure.
A
CB
D
E
F
HG
I
J
TREE T’
TREE T”
Copies of binary trees: Two binary trees T and T’ are said to be copies if they have
similar structure and same contents at the corresponding nodes.
A
CB
D E
A
CB
D
E
TREE T’
TREE T”
KEY TERMS contd.
 Directed edge: Line drawn from a node N to any of its successor is called a
directed edge. A binary tree of n nodes have exactly n – 1 edges (because, every
node except the root node is connected to its parent via an edge).
 Path: A sequence of consecutive edges is called a path.
 Depth: The depth of a node N is given as the length of the path from the root R to
the node N. The depth of the root node is zero. The height/depth of a tree is
defined as the length of the path from the root node to the deepest node in the
tree.
A tree with only a root node has a height of zero. A binary tree of height h, has at
least h nodes and at most 2h – 1
nodes. This is because every level will have at least
one node and can have at most 2 nodes. So, if every level has two nodes then a
tree with height h will have at the most 2h – 1
nodes as at level 0, there is only one
element called the root. The height of a binary tree with n nodes is at least n and
at most log2(n+1)
 Ancestor and descendant nodes: Ancestors of a node are all the nodes along the
path from the root to that node. Similarly, descendants of a node are all the nodes
along the path from that node to the leaf node.
 Binary trees are commonly used to implement binary search trees, expression
trees, tournament trees and binary heaps.
Complete Binary Trees
 A complete binary tree is a binary tree which satisfies two properties. First, in a
complete binary tree every level, except possibly the last, is completely filled.
Second, all nodes appear as far left as possible
 In a complete binary tree Tn, there are exactly n nodes and level r of T can have at
most 2r
nodes.
 The formula to find the parent, left child and right child can be given as- if K is a
parent node, then its left child can be calculated as 2 * K and its right child can be
calculated as 2 * K + 1. For example, the children of node 4 are 8 (2*4) and 9 (2* 4 +
1). Similarly, the parent of the node K can be calculated as | K/2 |. Given the node 4,
its parent can be calculated as | 4/2 | = 2. The height of a tree Tn having exactly n
nodes is given as,
 Hn = | log2 n + 1 |
 This means, if a tree T has 10,00,000 nodes then its height is 21.
1
32
4
8
5 6
7
1310 119 12
Representation of Binary Trees
in Memory
 In computer’s memory, a binary tree can be maintained either using
a linked representation (as in case of a linked list) or using
sequential representation (as in case of single arrays).
Linked Representation of Binary TreesLinked Representation of Binary Trees
 In linked representation of binary tree, every node will have three
parts, the data element, a pointer to the left node and a pointer to
the right node. So in C, the binary tree is built with a node type
given as below.
struct node {
struct node* left;
int data;
struct node* right;
};
1
2 3
4 5 6 7
X 8 X X 9 X X 10 X X 11 X X 12 X
Sequential Representation of
Binary Trees
 Sequential representation of trees is done using single or one
dimensional array. Though, it is the simplest technique for memory
representation but it is very inefficient as it requires a lot of memory
space. A sequential binary tree follows the rules given below:
 One dimensional array called TREE, will be used.
 The root of the tree will be stored in the first location. That is,
TREE[0] will store the data of the root element.
 The children of a node K, will be stored in location (2*K) and
(2*K+1).
 The maximum size of the array TREE is given as (2d+1
-1), where d is
the depth of the tree.
 An empty tree or sub-tree is specified using NULL. If TREE[0] =
NULL, then the tree is empty.
0 20
1 15
2 35
3 12
4 17
5 21
6 39
7
8
9 16
10 18
12
13
14 36
15 45
16
17
18
3515
12
17 2
1
39
45
1
6
18
3
6
20
EXPRESSION TREES
 Binary trees are widely used to store algebraic expressions. For
example, consider the algebraic expression Exp given as,
 Exp = (a – b ) + ( c * d)
 This expression can be represented using a binary tree as shown in
figure
+
*-
a b c d
TRAVERSING OF A BINARY TREE
 Traversing a binary tree is the process of visiting each node in the tree
exactly once, in a systematic way. Unlike linear data structures in which the
elements are traversed sequentially, tree is a non-linear data structure in
which the elements can be traversed in many different ways. There are
different algorithms for tree traversals. These algorithms differ in the order in
which the nodes are visited. In this section, we will read about these
algorithms.
 Pre-order algorithm
To traverse a non-empty binary tree in preorder, the following operations are
performed recursively at each node. The algorithm starts with the root node of
the tree and continues by,
 Visiting the root node.
 Traversing the left subtree.
 Traversing the right subtree.
A
CB
D E
F
IH
GA, B, D, C, E, F, G, H and I
In-order algorithm
To traverse a non-empty binary tree in in-order, the following operations
are performed recursively at each node. The algorithm starts with the
root node of the tree and continues by,
 Traversing the left subtree.
 Visiting the root node.
 Traversing the right subtree.
Post-order algorithm
To traverse a non-empty binary tree in post-order, the following operations
are performed recursively at each node. The algorithm starts with the
root node of the tree and continues by,
 Traversing the left subtree.
 Traversing the right subtree.
 Visiting the root node.
A
CB
D E
F
IH
G
B, D, A, E, H, G, I, F AND C.
D, B, H, I, G, F, E, C and A.
Thank U

More Related Content

PPT
Chapter 8 ds
PPT
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
PPTX
PPTX
Tree
PPT
Tree-In Data Structure
PPTX
Tree in data structure
PPTX
Tree data structure
PPT
358 33 powerpoint-slides_10-trees_chapter-10
Chapter 8 ds
DATA STRUCTURES AND ALGORITHMS UNIT-3 TREES PREPARED BY M V BRAHMANANDA REDDY
Tree
Tree-In Data Structure
Tree in data structure
Tree data structure
358 33 powerpoint-slides_10-trees_chapter-10

What's hot (20)

PPTX
non linear data structure -introduction of tree
PDF
Dsc++ unit 3 notes
PPTX
Data structure tree- advance
PPT
PPTX
Data structure tree - intermediate
PPT
Tree data structure
PDF
Lecture notes data structures tree
PPTX
Types of Tree in Data Structure in C++
PPTX
Unit 6 tree
PPTX
Unit – vi tree
PPTX
Mca iii dfs u-4 tree and graph
PPTX
Tree - Data Structure
PPT
Trees - Non Linear Data Structure
PPT
introduction to_trees
PPSX
Data Structure (Tree)
PPTX
Data structure using c module 2
PPTX
Trees data structure
PPTX
non linear data structure -introduction of tree
Dsc++ unit 3 notes
Data structure tree- advance
Data structure tree - intermediate
Tree data structure
Lecture notes data structures tree
Types of Tree in Data Structure in C++
Unit 6 tree
Unit – vi tree
Mca iii dfs u-4 tree and graph
Tree - Data Structure
Trees - Non Linear Data Structure
introduction to_trees
Data Structure (Tree)
Data structure using c module 2
Trees data structure
Ad

Similar to Lecture 5 tree.pptx (20)

PPTX
Introduction to Tree_Data Structure.pptx
PPT
PPTX
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
PPTX
NON-LINEAR DATA STRUCTURE-TREES.pptx
PPTX
Tree.pptx
PPTX
Binary Trees.pptx module 122img 787554yau
PPTX
Unit-VStackStackStackStackStackStack.pptx
PPTX
Lecture 8 data structures and algorithms
PPTX
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
PPTX
PPTX
binary tree.pptx
PPTX
tree-160731205832.pptx
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPT
Data Structure And Algorithms for Computer Science
PPTX
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
PDF
Chapter 5_Trees.pdf
PDF
Module - 5_Trees.pdf
PPTX
trees in data structure
PPT
Lecture 5 trees
PPTX
tree Data Structures in python Traversals.pptx
Introduction to Tree_Data Structure.pptx
Lecture-7-Binary-Trees-and-Algorithms-11052023-054009pm.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Tree.pptx
Binary Trees.pptx module 122img 787554yau
Unit-VStackStackStackStackStackStack.pptx
Lecture 8 data structures and algorithms
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
binary tree.pptx
tree-160731205832.pptx
UNIT III Non Linear Data Structures - Trees.pptx
Data Structure And Algorithms for Computer Science
DS-UNIT-4zjufrusefihfacbciauhfbaiuhc.pptx
Chapter 5_Trees.pdf
Module - 5_Trees.pdf
trees in data structure
Lecture 5 trees
tree Data Structures in python Traversals.pptx
Ad

More from Abirami A (12)

PPTX
Lecture 3 time complexity
PPTX
Lecture 2 Data Structure Introduction
PPTX
Lecture 1 introduction
PPTX
Lecture 14 splay tree
PPTX
Lecture 16 graphs traversal
PPTX
Lecture 16 graph introduction
PPTX
Lecture 6 disjoint set
PPTX
Lecture 8 tree traversal
PPTX
Lecture 9 b tree
PPTX
Lecture 7 bst
PPTX
Lecture 6 tree traversal
PPTX
Lecture 1 sorting insertion & shell sort
Lecture 3 time complexity
Lecture 2 Data Structure Introduction
Lecture 1 introduction
Lecture 14 splay tree
Lecture 16 graphs traversal
Lecture 16 graph introduction
Lecture 6 disjoint set
Lecture 8 tree traversal
Lecture 9 b tree
Lecture 7 bst
Lecture 6 tree traversal
Lecture 1 sorting insertion & shell sort

Recently uploaded (20)

PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PDF
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PPTX
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PDF
Design Guidelines and solutions for Plastics parts
PPTX
Module 8- Technological and Communication Skills.pptx
PPTX
introduction to high performance computing
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
Abrasive, erosive and cavitation wear.pdf
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PPTX
Amdahl’s law is explained in the above power point presentations
PDF
ChapteR012372321DFGDSFGDFGDFSGDFGDFGDFGSDFGDFGFD
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
distributed database system" (DDBS) is often used to refer to both the distri...
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Improvement effect of pyrolyzed agro-food biochar on the properties of.pdf
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
August 2025 - Top 10 Read Articles in Network Security & Its Applications
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
ASME PCC-02 TRAINING -DESKTOP-NLE5HNP.pptx
August -2025_Top10 Read_Articles_ijait.pdf
Design Guidelines and solutions for Plastics parts
Module 8- Technological and Communication Skills.pptx
introduction to high performance computing
Exploratory_Data_Analysis_Fundamentals.pdf
Abrasive, erosive and cavitation wear.pdf
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
Amdahl’s law is explained in the above power point presentations
ChapteR012372321DFGDSFGDFGDFSGDFGDFGDFGSDFGDFGFD
Fundamentals of Mechanical Engineering.pptx
BIO-INSPIRED ARCHITECTURE FOR PARSIMONIOUS CONVERSATIONAL INTELLIGENCE : THE ...
"Array and Linked List in Data Structures with Types, Operations, Implementat...

Lecture 5 tree.pptx

  • 2. BINARY TREES  A binary tree is a data structure which is defined as a collection of elements called nodes. Every node contains a "left" pointer, a "right" pointer, and a data element. Every binary tree has a root element pointed by a "root" pointer. The root element is the topmost node in the tree. If root = NULL, then it means the tree is empty.  If the root node R is not NULL, then the two trees T1 and T2 are called the left and right subtrees of R. if T1 is non-empty, then T1 is said to be the left successor of R. likewise, if T2 is non-empty then, it is called the right successor of R. 8 1 32 4 5 6 7 1 2 1 0 1 1 ROOT NODE T2T1 9 In a binary tree every node has 0, 1 or at the most 2 successors. A node that has no successors or 0 successors is called the leaf node or the terminal node.
  • 3. KEY TERMS  Sibling: If N is any node in T that has left successor S1 and right successor S2, then N is called the parent of S1 and S2. Correspondingly, S1 and S2 are called the left child and the right child of N. Also, S1 and S2 are said to be siblings. Every node other than the root node has a parent. In other words, all nodes that are at the same level and share the same parent are called siblings (brothers). Level number: Every node in the binary tree is assigned a level number. The root node is defined to be at level 0. The left and right child of the root node has a level number 1. Similarly, every node is at one level higher than its parents. So all child nodes are defined to have level number as parent’s level number + 1.  Degree: Degree of a node is equal to the number of children that a node has. The degree of a leaf node is zero. In-degree of a node is the number of edges arriving at that node. The root node is the only node that has an in-degree equal to zero. Similarly, Out-degree of a node is the number of edges leaving that node. Leaf node: A leaf node has no children.
  • 4. KEY TERMS contd. Similar binary trees: Given two binary trees T and T’ are said to be similar if both of these trees have the same structure. A CB D E F HG I J TREE T’ TREE T” Copies of binary trees: Two binary trees T and T’ are said to be copies if they have similar structure and same contents at the corresponding nodes. A CB D E A CB D E TREE T’ TREE T”
  • 5. KEY TERMS contd.  Directed edge: Line drawn from a node N to any of its successor is called a directed edge. A binary tree of n nodes have exactly n – 1 edges (because, every node except the root node is connected to its parent via an edge).  Path: A sequence of consecutive edges is called a path.  Depth: The depth of a node N is given as the length of the path from the root R to the node N. The depth of the root node is zero. The height/depth of a tree is defined as the length of the path from the root node to the deepest node in the tree. A tree with only a root node has a height of zero. A binary tree of height h, has at least h nodes and at most 2h – 1 nodes. This is because every level will have at least one node and can have at most 2 nodes. So, if every level has two nodes then a tree with height h will have at the most 2h – 1 nodes as at level 0, there is only one element called the root. The height of a binary tree with n nodes is at least n and at most log2(n+1)  Ancestor and descendant nodes: Ancestors of a node are all the nodes along the path from the root to that node. Similarly, descendants of a node are all the nodes along the path from that node to the leaf node.  Binary trees are commonly used to implement binary search trees, expression trees, tournament trees and binary heaps.
  • 6. Complete Binary Trees  A complete binary tree is a binary tree which satisfies two properties. First, in a complete binary tree every level, except possibly the last, is completely filled. Second, all nodes appear as far left as possible  In a complete binary tree Tn, there are exactly n nodes and level r of T can have at most 2r nodes.  The formula to find the parent, left child and right child can be given as- if K is a parent node, then its left child can be calculated as 2 * K and its right child can be calculated as 2 * K + 1. For example, the children of node 4 are 8 (2*4) and 9 (2* 4 + 1). Similarly, the parent of the node K can be calculated as | K/2 |. Given the node 4, its parent can be calculated as | 4/2 | = 2. The height of a tree Tn having exactly n nodes is given as,  Hn = | log2 n + 1 |  This means, if a tree T has 10,00,000 nodes then its height is 21. 1 32 4 8 5 6 7 1310 119 12
  • 7. Representation of Binary Trees in Memory  In computer’s memory, a binary tree can be maintained either using a linked representation (as in case of a linked list) or using sequential representation (as in case of single arrays). Linked Representation of Binary TreesLinked Representation of Binary Trees  In linked representation of binary tree, every node will have three parts, the data element, a pointer to the left node and a pointer to the right node. So in C, the binary tree is built with a node type given as below. struct node { struct node* left; int data; struct node* right; }; 1 2 3 4 5 6 7 X 8 X X 9 X X 10 X X 11 X X 12 X
  • 8. Sequential Representation of Binary Trees  Sequential representation of trees is done using single or one dimensional array. Though, it is the simplest technique for memory representation but it is very inefficient as it requires a lot of memory space. A sequential binary tree follows the rules given below:  One dimensional array called TREE, will be used.  The root of the tree will be stored in the first location. That is, TREE[0] will store the data of the root element.  The children of a node K, will be stored in location (2*K) and (2*K+1).  The maximum size of the array TREE is given as (2d+1 -1), where d is the depth of the tree.  An empty tree or sub-tree is specified using NULL. If TREE[0] = NULL, then the tree is empty. 0 20 1 15 2 35 3 12 4 17 5 21 6 39 7 8 9 16 10 18 12 13 14 36 15 45 16 17 18 3515 12 17 2 1 39 45 1 6 18 3 6 20
  • 9. EXPRESSION TREES  Binary trees are widely used to store algebraic expressions. For example, consider the algebraic expression Exp given as,  Exp = (a – b ) + ( c * d)  This expression can be represented using a binary tree as shown in figure + *- a b c d
  • 10. TRAVERSING OF A BINARY TREE  Traversing a binary tree is the process of visiting each node in the tree exactly once, in a systematic way. Unlike linear data structures in which the elements are traversed sequentially, tree is a non-linear data structure in which the elements can be traversed in many different ways. There are different algorithms for tree traversals. These algorithms differ in the order in which the nodes are visited. In this section, we will read about these algorithms.  Pre-order algorithm To traverse a non-empty binary tree in preorder, the following operations are performed recursively at each node. The algorithm starts with the root node of the tree and continues by,  Visiting the root node.  Traversing the left subtree.  Traversing the right subtree. A CB D E F IH GA, B, D, C, E, F, G, H and I
  • 11. In-order algorithm To traverse a non-empty binary tree in in-order, the following operations are performed recursively at each node. The algorithm starts with the root node of the tree and continues by,  Traversing the left subtree.  Visiting the root node.  Traversing the right subtree. Post-order algorithm To traverse a non-empty binary tree in post-order, the following operations are performed recursively at each node. The algorithm starts with the root node of the tree and continues by,  Traversing the left subtree.  Traversing the right subtree.  Visiting the root node. A CB D E F IH G B, D, A, E, H, G, I, F AND C. D, B, H, I, G, F, E, C and A.