SlideShare a Scribd company logo
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

What's hot

non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of treeSiddhi Viradiya
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
Guru Nanak Institute Of Tech
 
Data structure tree- advance
Data structure tree- advanceData structure tree- advance
Data structure tree- advance
MD. MARUFUZZAMAN .
 
Tree
TreeTree
Data structure tree - intermediate
Data structure tree - intermediateData structure tree - intermediate
Data structure tree - intermediate
MD. MARUFUZZAMAN .
 
Tree data structure
Tree data structureTree data structure
Tree data structure
Lovely Professional University
 
Tree
TreeTree
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
maamir farooq
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
Himanshu Choudhary
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
Dabbal Singh Mahara
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
Tribhuvan University
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
Rai University
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
Priyanka Rana
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
Danish Aakash
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
 
Data structure using c module 2
Data structure using c module 2Data structure using c module 2
Data structure using c module 2
smruti sarangi
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
Mahmoud Alfarra
 
Trees
TreesTrees

What's hot (20)

non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Dsc++ unit 3 notes
Dsc++ unit 3 notesDsc++ unit 3 notes
Dsc++ unit 3 notes
 
Data structure tree- advance
Data structure tree- advanceData structure tree- advance
Data structure tree- advance
 
Tree
TreeTree
Tree
 
Data structure tree - intermediate
Data structure tree - intermediateData structure tree - intermediate
Data structure tree - intermediate
 
Tree data structure
Tree data structureTree data structure
Tree data structure
 
Tree
TreeTree
Tree
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
Data structure using c module 2
Data structure using c module 2Data structure using c module 2
Data structure using c module 2
 
Tree
TreeTree
Tree
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
Trees
TreesTrees
Trees
 

Similar to Lecture 5 tree.pptx

Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
worldchannel
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
Rajitha Reddy Alugati
 
Binary tree
Binary treeBinary tree
Binary tree
Rajendran
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
AnuradhaJadiya1
 
Tree chapter
Tree chapterTree chapter
Tree chapter
LJ Projects
 
Binary tree
Binary  treeBinary  tree
Binary tree
Vanitha Chandru
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
SSE_AndyLi
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
SurajSharma266169
 
7.tree
7.tree7.tree
Trees
TreesTrees
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
nakulvarshney371
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
Revathiparamanathan
 
tree.ppt
tree.ppttree.ppt
tree.ppt
wondmhunegn
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsAakash deep Singhal
 
Trees.pptx
Trees.pptxTrees.pptx
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
SIVAKUMARM603675
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
DhanushSrinivasulu
 

Similar to Lecture 5 tree.pptx (20)

Trees
TreesTrees
Trees
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
NON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptxNON-LINEAR DATA STRUCTURE-TREES.pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
Tree chapter
Tree chapterTree chapter
Tree chapter
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
 
7.tree
7.tree7.tree
7.tree
 
Trees
TreesTrees
Trees
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
 
Unit 3,4.docx
Unit 3,4.docxUnit 3,4.docx
Unit 3,4.docx
 
tree.ppt
tree.ppttree.ppt
tree.ppt
 
Lecture 8 data structures and algorithms
Lecture 8 data structures and algorithmsLecture 8 data structures and algorithms
Lecture 8 data structures and algorithms
 
Trees.pptx
Trees.pptxTrees.pptx
Trees.pptx
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
binary tree.pptx
binary tree.pptxbinary tree.pptx
binary tree.pptx
 

More from Abirami A

Lecture 3 time complexity
Lecture 3 time complexityLecture 3 time complexity
Lecture 3 time complexity
Abirami A
 
Lecture 2 Data Structure Introduction
Lecture 2 Data Structure IntroductionLecture 2 Data Structure Introduction
Lecture 2 Data Structure Introduction
Abirami A
 
Lecture 1 introduction
Lecture 1 introductionLecture 1 introduction
Lecture 1 introduction
Abirami A
 
Lecture 14 splay tree
Lecture 14 splay treeLecture 14 splay tree
Lecture 14 splay tree
Abirami A
 
Lecture 16 graphs traversal
Lecture 16 graphs traversalLecture 16 graphs traversal
Lecture 16 graphs traversal
Abirami A
 
Lecture 16 graph introduction
Lecture 16 graph introductionLecture 16 graph introduction
Lecture 16 graph introduction
Abirami A
 
Lecture 6 disjoint set
Lecture 6 disjoint setLecture 6 disjoint set
Lecture 6 disjoint set
Abirami A
 
Lecture 8 tree traversal
Lecture 8 tree traversalLecture 8 tree traversal
Lecture 8 tree traversal
Abirami A
 
Lecture 9 b tree
Lecture 9 b treeLecture 9 b tree
Lecture 9 b tree
Abirami A
 
Lecture 7 bst
Lecture 7 bstLecture 7 bst
Lecture 7 bst
Abirami A
 
Lecture 6 tree traversal
Lecture 6 tree traversalLecture 6 tree traversal
Lecture 6 tree traversal
Abirami A
 
Lecture 1 sorting insertion & shell sort
Lecture 1 sorting insertion & shell sortLecture 1 sorting insertion & shell sort
Lecture 1 sorting insertion & shell sort
Abirami A
 

More from Abirami A (12)

Lecture 3 time complexity
Lecture 3 time complexityLecture 3 time complexity
Lecture 3 time complexity
 
Lecture 2 Data Structure Introduction
Lecture 2 Data Structure IntroductionLecture 2 Data Structure Introduction
Lecture 2 Data Structure Introduction
 
Lecture 1 introduction
Lecture 1 introductionLecture 1 introduction
Lecture 1 introduction
 
Lecture 14 splay tree
Lecture 14 splay treeLecture 14 splay tree
Lecture 14 splay tree
 
Lecture 16 graphs traversal
Lecture 16 graphs traversalLecture 16 graphs traversal
Lecture 16 graphs traversal
 
Lecture 16 graph introduction
Lecture 16 graph introductionLecture 16 graph introduction
Lecture 16 graph introduction
 
Lecture 6 disjoint set
Lecture 6 disjoint setLecture 6 disjoint set
Lecture 6 disjoint set
 
Lecture 8 tree traversal
Lecture 8 tree traversalLecture 8 tree traversal
Lecture 8 tree traversal
 
Lecture 9 b tree
Lecture 9 b treeLecture 9 b tree
Lecture 9 b tree
 
Lecture 7 bst
Lecture 7 bstLecture 7 bst
Lecture 7 bst
 
Lecture 6 tree traversal
Lecture 6 tree traversalLecture 6 tree traversal
Lecture 6 tree traversal
 
Lecture 1 sorting insertion & shell sort
Lecture 1 sorting insertion & shell sortLecture 1 sorting insertion & shell sort
Lecture 1 sorting insertion & shell sort
 

Recently uploaded

Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABSDESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
itech2017
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
heavyhaig
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
Kerry Sado
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Soumen Santra
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
zwunae
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
symbo111
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
Intella Parts
 

Recently uploaded (20)

Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABSDESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
DESIGN AND ANALYSIS OF A CAR SHOWROOM USING E TABS
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Technical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prismsTechnical Drawings introduction to drawing of prisms
Technical Drawings introduction to drawing of prisms
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
Hierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power SystemHierarchical Digital Twin of a Naval Power System
Hierarchical Digital Twin of a Naval Power System
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Building Electrical System Design & Installation
Building Electrical System Design & InstallationBuilding Electrical System Design & Installation
Building Electrical System Design & Installation
 
Forklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella PartsForklift Classes Overview by Intella Parts
Forklift Classes Overview by Intella Parts
 

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.