KEMBAR78
Introduction and basic of Trees and Binary Trees | PPTX
Tree & Binary Tree
19CSE212 : Data Structures and Algorithms
Dr. Chandan Kumar
Department of Computer Science and Engineering
Jan - Jun 2024
Dr Chandan Kumar Jan - Jun 2024
Contents
1 Introductionto Tree
3
4
2 Binary Tree & Its Types
OperationsonBinaryTree
5
Tree Traversal (Pre-order, In-order & Post-order)
8
Dr Chandan Kumar Jan - Jun 2024
Constructing a Binary Tree from Traversal Results
Tree
Dr Chandan Kumar Jan - Jun 2024
• So far, we have discussed mainly linear data
structures – arrays, linked lists, stacks and queues.
• Now we will discuss a non-linear data structure
called tree.
stack
Tree
Dr Chandan Kumar Jan - Jun 2024
• A tree is recursively defined as a set of one or more
nodes where one node is designated as the root of the
tree and all the remaining nodes can be partitioned
into non-empty sets each of which is a sub-tree of the
root.
• Trees are mainly used to represent data containing a
hierarchical relationship between elements, for
example, records, family trees and table of contents.
Tree
Dr Chandan Kumar Jan - Jun 2024
Tree A Hypothetical Family
Tree
Basic Terminology
• Root node: The root node R is the topmost node in the tree. If R = NULL,
thenitmeansthetreeisempty.
• Sub-trees: If the root node R is not NULL, then the trees T1, T2, and T3 are
called thesub-treesofR.
• Leaf node: Anode that has no children is called the leaf node or the terminal
node
• Path: Asequence of consecutive edges is called a path. For example, in Fig.,
thepathfromtherootnodeAtonodeIisgivenas:A,D,andI.
• Ancestor node: An ancestor of a node is any predecessor node on the path
from root to that node. The root node does not have any ancestors. In the tree
giveninFig.,nodesA,C,andGaretheancestorsofnodeK.
Dr Chandan Kumar Jan - Jun 2024
Basic Terminology
• Descendantnode:Adescendant nodeis anysuccessornodeon anypath from
the node to a leaf node. Leaf nodes do not have any descendants. In the tree
giveninFig.,nodesC,G,J,andKare thedescendantsofnodeA.
• Level number: Every node in the tree is assigned a level number in such a
way that the root node is at level 0, children of the root node are at level
number 1. Thus, every node is at one level higher than its parent. So, all child
nodeshavealevel numbergivenbyparent’slevelnumber+1.
• Degree: Degree of a node is equal to the number of children that a node has.
Thedegreeofaleafnodeiszero.
Dr Chandan Kumar Jan - Jun 2024
Basic Terminology
Dr Chandan Kumar Jan - Jun 2024
Basic Terminology
Dr Chandan Kumar Jan - Jun 2024
Basic Terminology
Dr Chandan Kumar Jan - Jun 2024
Binary Tree
Dr Chandan Kumar Jan - Jun 2024
• A binary tree, is a tree in which no node can have more than
two children. The topmost element is called the root node, and
each node has 0, 1, or at the most 2 children.
• Consider a binary tree T,
here ‘A’ is the root node
of the binary tree T.
• ‘B’ is the left child of ‘A’ and
‘C’ is the right
child of ‘A’
• i.e A is a father of B and C.
• The node B and C are called
siblings.
• Nodes D,H,I,F,J are leaf node
Binary Tree
Dr Chandan Kumar Jan - Jun 2024
• A binary tree is a finite set of elements that are either empty or
is partitioned into three disjoint subsets.
• The first subset contains a single element called the root of the
tree.
• The other two subsets are themselves binary trees called the
left and right sub-trees of the original tree.
• A left or right sub-tree can be empty.
• Each element of a binary tree is called a node of the tree.
Binary Tree
Dr Chandan Kumar Jan - Jun 2024
• The following figure shows a binary tree with 9 nodes where A
is the root.
• The root node of LA is node B, the root node of RA is C and
so on
• Since a binary tree can contain at most 1 node at level 0 (the
root), it contains at most 2L nodes at level L.
• The left sub tree of the root
node, which we denoted by
LA, is the set LA = {B,D,E,G}
and the right sub tree of the
root node, RA is the set
RA={C,F,H,I}
Types of Binary Tree
Dr Chandan Kumar Jan - Jun 2024
Types of Binary Tree based on the number of children:
1. Full Binary Tree
2. Degenerate Binary Tree
3. Skewed Binary Trees
Types of Binary Tree on the basis of the completion of levels:
1. Strictly binary tree
2. Complete binary tree
3. Almost complete binary tree
Types of Binary Tree
Dr Chandan Kumar Jan - Jun 2024
• Full Binary Tree: A Binary Tree is a full
binary tree if every node has 0 or 2 children.
• Degenerate (or pathological) tree: A Tree where
every internal node has one child.
• A skewed binary tree: It is a
pathological/degenerate tree in which
the tree is either dominated by the left
nodes or the right nodes.
Types of Binary Tree
Dr Chandan Kumar Jan - Jun 2024
• Strictly binary tree: If every non-leaf node in a binary tree has
nonempty left and right sub-trees, then such a tree is called a
strictly binary tree.
• Or, to put it another way, all
of the nodes in a strictly binary
tree are of degree zero or two,
never degree one
• A strictly binary tree with
N leaves always contains
2N – 1 nodes.
Types of Binary Tree
Dr Chandan Kumar Jan - Jun 2024
• A complete binary tree is a binary tree in which every level,
except possibly the last, is completely filled, and all nodes are as
far left as possible
• A complete binary tree of depth d is called strictly binary tree
if all of whose leaves are at level d.
• A complete binary tree has
2d nodes at every depth d and
2d -1 non leaf nodes.
Types of Binary Tree
Dr Chandan Kumar Jan - Jun 2024
• An almost complete binary tree is a tree where for a right
child, there is always a left child, but for a left child there may
not be a right child.
Fig. Almost complete binary tree but not a Strictly
binary tree. Since node E has a left son but not a
right son
Operations on Binary Tree
Dr Chandan Kumar Jan - Jun 2024
Node of Binary Tree using Java
Dr Chandan Kumar Jan - Jun 2024
class Node {
int key;
Node left, right;
public Node(int item)
{
key = item;
left = right = null;
}
}
Tree Traversal
Dr Chandan Kumar Jan - Jun 2024
• Traversal is a process of visiting all the nodes of a tree and may
print their values too.
• All nodes are connected via edges (links) we always start from
the root (head) node.
• There are three ways which we use to traverse a tree:
i. Pre-order Traversal
ii. In-order Traversal
iii. Post-order Traversal
• Pre-order: <root><left><right>
• In-order: <left><root><right>
• Post-order: <left><right><root>
• Generally, we traverse a tree to search or locate a given item or
key in the tree or to print all the values it contains.
Pre-order Traversal
Dr Chandan Kumar Jan - Jun 2024
• The preorder traversal of a nonempty binary tree is defined as
follows:
 Visit the root node
 Traverse the left sub-tree in preorder
 Traverse the right sub-tree in preorder
In-order Traversal
Dr Chandan Kumar Jan - Jun 2024
• The in-order traversal of a nonempty binary tree is defined as
follows:
 Traverse the left sub-tree in in-order
 Visit the root node
 Traverse the right sub-tree in inorder
The in-order traversal output of the given tree is H D I B E A F C G
Post-order Traversal
Dr Chandan Kumar Jan - Jun 2024
• The post-order traversal of a nonempty binary tree is defined as
follows:
 Traverse the left sub-tree in post-order
 Traverse the right sub-tree in post-order
 Visit the root node
The post-order traversal output of the given tree is H I D E B F G C A
Example
Dr Chandan Kumar Jan - Jun 2024
The outcome of different traversals on the above tree:
• Pre-order traversal: 7 5 10 31 11 23 15
• In-order traversal: 10 5 7 11 23 31 15
• Post-order traversal: 10 5 23 11 15 31 7
• Level-order traversal: 7 5 31 10 11 15 23
Example
Dr Chandan Kumar Jan - Jun 2024
• Pre-order traversal:
a) A, B, D, G, H, L, E, C, F, I, J, K b) A, B, D, C, E, F, G, H, I
• In-order traversal:
a) G, D, H, L, B, E, A, C, I, F, K, J b) B, D, A, E, H, G, I, F, C
• Post-order traversal:
a) G, L, H, D, E, B, I, K, J, F, C, A b) D, B, H, I, G, F, E, C, and A
Example: Level-order Traversal
Dr Chandan Kumar Jan - Jun 2024
Constructing a Binary Tree from Traversal Results
Dr Chandan Kumar Jan - Jun 2024
• We can construct a binary tree if we are given at least two traversal
results. The first traversal must be the in-order traversal and the
second can be either pre-order or post-order traversal.
• The in-order traversal result will be used to determine the left and
the right child nodes, and the pre-order/post-order can be used to
determine the root node.
Step 1: Use the pre-order sequence to determine the root node of the
tree. The first element would be the root node.
Step 2: In an in-order traversal sequence, elements to the left of the
root node constitute the left subtree, while those to the right form the
right subtree.
Step 3: Recursively select each element from pre-order traversal
sequence and create its left and right sub-trees from the in-order
traversal sequence.
Constructing a Binary Tree from Traversal Results
Dr Chandan Kumar Jan - Jun 2024
Example:
In–order Traversal: D B E A F C G
Pre–order Traversal: A B D E C F G
Constructing a Binary Tree from Traversal Results
Dr Chandan Kumar Jan - Jun 2024
Example:
Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
Constructing a Binary Tree from Traversal Results
Dr Chandan Kumar Jan - Jun 2024
Example:
Inorder:[ 40, 20 , 50, 10, 60, 30], Postorder: [40, 50, 20, 60, 30, 10]
List of References
Dr Chandan Kumar Jan - Jun 2024
https://www.javatpoint.com/
https://www.tutorialspoint.com
https://www.geeksforgeeks.org/
https://www.programiz.com/dsa/
https://chat.openai.com/

Introduction and basic of Trees and Binary Trees

  • 1.
    Tree & BinaryTree 19CSE212 : Data Structures and Algorithms Dr. Chandan Kumar Department of Computer Science and Engineering Jan - Jun 2024 Dr Chandan Kumar Jan - Jun 2024
  • 2.
    Contents 1 Introductionto Tree 3 4 2Binary Tree & Its Types OperationsonBinaryTree 5 Tree Traversal (Pre-order, In-order & Post-order) 8 Dr Chandan Kumar Jan - Jun 2024 Constructing a Binary Tree from Traversal Results
  • 3.
    Tree Dr Chandan KumarJan - Jun 2024 • So far, we have discussed mainly linear data structures – arrays, linked lists, stacks and queues. • Now we will discuss a non-linear data structure called tree. stack
  • 4.
    Tree Dr Chandan KumarJan - Jun 2024 • A tree is recursively defined as a set of one or more nodes where one node is designated as the root of the tree and all the remaining nodes can be partitioned into non-empty sets each of which is a sub-tree of the root. • Trees are mainly used to represent data containing a hierarchical relationship between elements, for example, records, family trees and table of contents.
  • 5.
    Tree Dr Chandan KumarJan - Jun 2024 Tree A Hypothetical Family Tree
  • 6.
    Basic Terminology • Rootnode: The root node R is the topmost node in the tree. If R = NULL, thenitmeansthetreeisempty. • Sub-trees: If the root node R is not NULL, then the trees T1, T2, and T3 are called thesub-treesofR. • Leaf node: Anode that has no children is called the leaf node or the terminal node • Path: Asequence of consecutive edges is called a path. For example, in Fig., thepathfromtherootnodeAtonodeIisgivenas:A,D,andI. • Ancestor node: An ancestor of a node is any predecessor node on the path from root to that node. The root node does not have any ancestors. In the tree giveninFig.,nodesA,C,andGaretheancestorsofnodeK. Dr Chandan Kumar Jan - Jun 2024
  • 7.
    Basic Terminology • Descendantnode:Adescendantnodeis anysuccessornodeon anypath from the node to a leaf node. Leaf nodes do not have any descendants. In the tree giveninFig.,nodesC,G,J,andKare thedescendantsofnodeA. • Level number: Every node in the tree is assigned a level number in such a way that the root node is at level 0, children of the root node are at level number 1. Thus, every node is at one level higher than its parent. So, all child nodeshavealevel numbergivenbyparent’slevelnumber+1. • Degree: Degree of a node is equal to the number of children that a node has. Thedegreeofaleafnodeiszero. Dr Chandan Kumar Jan - Jun 2024
  • 8.
    Basic Terminology Dr ChandanKumar Jan - Jun 2024
  • 9.
    Basic Terminology Dr ChandanKumar Jan - Jun 2024
  • 10.
    Basic Terminology Dr ChandanKumar Jan - Jun 2024
  • 11.
    Binary Tree Dr ChandanKumar Jan - Jun 2024 • A binary tree, is a tree in which no node can have more than two children. The topmost element is called the root node, and each node has 0, 1, or at the most 2 children. • Consider a binary tree T, here ‘A’ is the root node of the binary tree T. • ‘B’ is the left child of ‘A’ and ‘C’ is the right child of ‘A’ • i.e A is a father of B and C. • The node B and C are called siblings. • Nodes D,H,I,F,J are leaf node
  • 12.
    Binary Tree Dr ChandanKumar Jan - Jun 2024 • A binary tree is a finite set of elements that are either empty or is partitioned into three disjoint subsets. • The first subset contains a single element called the root of the tree. • The other two subsets are themselves binary trees called the left and right sub-trees of the original tree. • A left or right sub-tree can be empty. • Each element of a binary tree is called a node of the tree.
  • 13.
    Binary Tree Dr ChandanKumar Jan - Jun 2024 • The following figure shows a binary tree with 9 nodes where A is the root. • The root node of LA is node B, the root node of RA is C and so on • Since a binary tree can contain at most 1 node at level 0 (the root), it contains at most 2L nodes at level L. • The left sub tree of the root node, which we denoted by LA, is the set LA = {B,D,E,G} and the right sub tree of the root node, RA is the set RA={C,F,H,I}
  • 14.
    Types of BinaryTree Dr Chandan Kumar Jan - Jun 2024 Types of Binary Tree based on the number of children: 1. Full Binary Tree 2. Degenerate Binary Tree 3. Skewed Binary Trees Types of Binary Tree on the basis of the completion of levels: 1. Strictly binary tree 2. Complete binary tree 3. Almost complete binary tree
  • 15.
    Types of BinaryTree Dr Chandan Kumar Jan - Jun 2024 • Full Binary Tree: A Binary Tree is a full binary tree if every node has 0 or 2 children. • Degenerate (or pathological) tree: A Tree where every internal node has one child. • A skewed binary tree: It is a pathological/degenerate tree in which the tree is either dominated by the left nodes or the right nodes.
  • 16.
    Types of BinaryTree Dr Chandan Kumar Jan - Jun 2024 • Strictly binary tree: If every non-leaf node in a binary tree has nonempty left and right sub-trees, then such a tree is called a strictly binary tree. • Or, to put it another way, all of the nodes in a strictly binary tree are of degree zero or two, never degree one • A strictly binary tree with N leaves always contains 2N – 1 nodes.
  • 17.
    Types of BinaryTree Dr Chandan Kumar Jan - Jun 2024 • A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible • A complete binary tree of depth d is called strictly binary tree if all of whose leaves are at level d. • A complete binary tree has 2d nodes at every depth d and 2d -1 non leaf nodes.
  • 18.
    Types of BinaryTree Dr Chandan Kumar Jan - Jun 2024 • An almost complete binary tree is a tree where for a right child, there is always a left child, but for a left child there may not be a right child. Fig. Almost complete binary tree but not a Strictly binary tree. Since node E has a left son but not a right son
  • 19.
    Operations on BinaryTree Dr Chandan Kumar Jan - Jun 2024
  • 20.
    Node of BinaryTree using Java Dr Chandan Kumar Jan - Jun 2024 class Node { int key; Node left, right; public Node(int item) { key = item; left = right = null; } }
  • 21.
    Tree Traversal Dr ChandanKumar Jan - Jun 2024 • Traversal is a process of visiting all the nodes of a tree and may print their values too. • All nodes are connected via edges (links) we always start from the root (head) node. • There are three ways which we use to traverse a tree: i. Pre-order Traversal ii. In-order Traversal iii. Post-order Traversal • Pre-order: <root><left><right> • In-order: <left><root><right> • Post-order: <left><right><root> • Generally, we traverse a tree to search or locate a given item or key in the tree or to print all the values it contains.
  • 22.
    Pre-order Traversal Dr ChandanKumar Jan - Jun 2024 • The preorder traversal of a nonempty binary tree is defined as follows:  Visit the root node  Traverse the left sub-tree in preorder  Traverse the right sub-tree in preorder
  • 23.
    In-order Traversal Dr ChandanKumar Jan - Jun 2024 • The in-order traversal of a nonempty binary tree is defined as follows:  Traverse the left sub-tree in in-order  Visit the root node  Traverse the right sub-tree in inorder The in-order traversal output of the given tree is H D I B E A F C G
  • 24.
    Post-order Traversal Dr ChandanKumar Jan - Jun 2024 • The post-order traversal of a nonempty binary tree is defined as follows:  Traverse the left sub-tree in post-order  Traverse the right sub-tree in post-order  Visit the root node The post-order traversal output of the given tree is H I D E B F G C A
  • 25.
    Example Dr Chandan KumarJan - Jun 2024 The outcome of different traversals on the above tree: • Pre-order traversal: 7 5 10 31 11 23 15 • In-order traversal: 10 5 7 11 23 31 15 • Post-order traversal: 10 5 23 11 15 31 7 • Level-order traversal: 7 5 31 10 11 15 23
  • 26.
    Example Dr Chandan KumarJan - Jun 2024 • Pre-order traversal: a) A, B, D, G, H, L, E, C, F, I, J, K b) A, B, D, C, E, F, G, H, I • In-order traversal: a) G, D, H, L, B, E, A, C, I, F, K, J b) B, D, A, E, H, G, I, F, C • Post-order traversal: a) G, L, H, D, E, B, I, K, J, F, C, A b) D, B, H, I, G, F, E, C, and A
  • 27.
    Example: Level-order Traversal DrChandan Kumar Jan - Jun 2024
  • 28.
    Constructing a BinaryTree from Traversal Results Dr Chandan Kumar Jan - Jun 2024 • We can construct a binary tree if we are given at least two traversal results. The first traversal must be the in-order traversal and the second can be either pre-order or post-order traversal. • The in-order traversal result will be used to determine the left and the right child nodes, and the pre-order/post-order can be used to determine the root node. Step 1: Use the pre-order sequence to determine the root node of the tree. The first element would be the root node. Step 2: In an in-order traversal sequence, elements to the left of the root node constitute the left subtree, while those to the right form the right subtree. Step 3: Recursively select each element from pre-order traversal sequence and create its left and right sub-trees from the in-order traversal sequence.
  • 29.
    Constructing a BinaryTree from Traversal Results Dr Chandan Kumar Jan - Jun 2024 Example: In–order Traversal: D B E A F C G Pre–order Traversal: A B D E C F G
  • 30.
    Constructing a BinaryTree from Traversal Results Dr Chandan Kumar Jan - Jun 2024 Example: Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
  • 31.
    Constructing a BinaryTree from Traversal Results Dr Chandan Kumar Jan - Jun 2024 Example: Inorder:[ 40, 20 , 50, 10, 60, 30], Postorder: [40, 50, 20, 60, 30, 10]
  • 32.
    List of References DrChandan Kumar Jan - Jun 2024 https://www.javatpoint.com/ https://www.tutorialspoint.com https://www.geeksforgeeks.org/ https://www.programiz.com/dsa/ https://chat.openai.com/