SlideShare a Scribd company logo
1 of 35
UNIT 3 - TREES
Tree ADT
• Treesare mainly usedto represent data containing a
hierarchical relationship between elements, for example,
records, family treesand table of contents.
• Consider aparent-child relationship
• Treeis asequence of nodes
• There is astarting node known asaroot node
• Everynode other than the root hasaparentnode.
• Nodes mayhaveany number of children
Key terms
• Root−Node at the top of the treeiscalled root.
• Parent −Anynode except root node hasone edgeupward to anode called parent.
• Child−Node below agiven node connected by its edgedownward is called its child
node.
• Sibling– Childof samenode are calledsiblings
• Leaf−Node which does not haveanychild node is called leaf node.
• Subtree −Subtree represents descendants of anode.
• Levels−Levelof anode represents the generation of anode. If root node is at level
0, then itsnext child node isat level 1, its grandchild isat level 2 and soon.
• keys−Keyrepresents avalue of anode basedon which asearch operation is to be
carried out for anode.
• Degreeof anode:
• Thedegree of anode isthe number of children of that node
• Degreeof aTree:
• Thedegree of atree isthe maximum degree of nodes in agiventree
• Path:
• It isthe sequenceof consecutive edgesfrom sourcenode to
destination node.
• Height of anode:
• Theheight of anode isthe maxpath length form that node to aleaf
node.
• Height of atree:
• Theheight of atree isthe height ofthe root
• Depth of atree:
• Depth of atree isthe maxlevel of anyleaf in the tree
Characteristicsof trees
• Non-linear datastructure
• Combinesadvantagesof anordered array
• Searchingasfast asin ordered array
• Insertion and deletion asfast asin linked list
• Simple and fast
Applications
• Directory structure of afilestore
• Structure of an arithmeticexpressions
• Usedin almost every 3Dvideo gameto determine
what objects need tobe rendered.
• Usedin almost every high-bandwidth router for storing
router-tables.
• used in compression algorithms, suchasthose used by the
.jpeg and .mp3 file- formats.
Tree Traversal
• Traversalis aprocessto visit all the nodes of atree
and mayprinttheir values too.
• All nodes are connected via edges(links) we always
start from theroot (head) node.
• There are three wayswhich we useto traverse atree
• In-orderTraversal
• Pre-orderTraversal
• Post-orderTraversal
• Generally we traverse atree to search or locate given item
or keyin the tree or to print all the values it contains.
Pre-order,In-order,Post-order
• Pre-order
• <root><left><right>
• In-order
• <left><root><right>
• Post-order
• <left><right><root>
Pre-orderTraversal
• Thepreorder traversal of anonempty binary tree
is defined asfollows:
• Visit the root node
• Traversethe left sub-tree inpreorder
• Traversethe right sub-tree inpreorder
In-ordertraversal
• Thein-order traversal of anonempty binary tree is
defined asfollows:
• Traversethe left sub-tree inin-order
• Visit the root node
• Traversethe right sub-tree ininorder
• Thein-order traversal output of the given treeis
• H D I B E A F C G
Post-ordertraversal
• Thein-order traversal of anonempty binary tree is
defined asfollows:
• Traversethe left sub-tree inpost-order
• Traversethe right sub-tree inpost-order
• Visit
• Thein-order traversal output of the given treeis
• H I D E B F G C A
• the root node
Binary Trees
• Abinary tree, isatree in which no node canhave
more thantwo children.
• Consider abinary tree T
,here ‘A’isthe root node of the
binary treeT
.
• ‘B’ isthe left child of ‘A’and
‘C’istheright child of ‘A’
• i.eAisafather of BandC.
• Thenode BandCarecalled siblings.
• NodesD,H,I,F
,Jare leafnode
• Theroot node of this binary tree isA.
• Theleft subtree of the root node, which we denoted by
LA,istheset LA ={B,D,E,G}andthe right subtree of the
root node, RA
isthe set RA={C,F
,H}
• Theroot node of LAisnode B,the root node of RA
isCandso
on
BinaryTreeProperties
• If abinary tree contains m nodesat level L,it contains
atmost 2m nodes at levelL+1
• Sinceabinary tree cancontain at most 1 node at level 0
(the root),it contains at most 2Lnodesat levelL.
Typesof BinaryTree
• Complete binary tree
• Strictly binary tree
• Full binary tree
• Perfect binary tree
• Balanced binary tree
• Degenerate binary tree
Strictly binarytree
• If every non-leaf node in abinary tree hasnonempty leftandright sub-
trees,then suchatree iscalled astrictly binarytree.
• Or,to put it anotherway,all of the nodesin astrictly binary tree areof
degreezero or two, never degreeone.
• Astrictly binary tree with
• Nleavesalwayscontains 2N–1 nodes.
Completebinarytree
• Acomplete binary tree is a binary tree in which every
level, except possibly the last, is completely
• filled, and all nodes are asfar left aspossible.
• Acomplete binary tree of depth d is called strictly binary
tree if all of whose leaves are at level d.
• Acomplete binary tree has2d nodes at every depthd and
2d -1 non leaf nodes
Full binary tree
• It is a special kind of a binary tree that has either zero children or
two children. It means that all the nodes in that binary tree
should either have two child nodes of its parent node or the
parent node is itself the leaf node or the external node.
• In other words, a full binary tree is a unique binary tree where
every node except the external node has two children. When it
holds a single child, such a binary tree will not be a full binary
tree. Here, the quantity of leaf nodes is equal to the number of
internal nodes plus one. The equation is like L=I+1, where L is the
number of leaf nodes, and I is the number of internal nodes.
Perfect binary tree
• A binary tree is said to be ‘perfect’ if all the internal nodes
have strictly two children, and every external or leaf node is at
the same level or same depth within a tree. A perfect binary
tree having height ‘h’ has 2h – 1 node. Here is the structure of
a perfect binary tree
Balanced Binary Tree
• A binary tree is said to be ‘balanced’ if the tree height is
O(logN), where ‘N’ is the number of nodes. In a balanced
binary tree, the height of the left and the right subtrees of
each node should vary by at most one.
• An AVL Tree and a Red-Black Tree are some common examples
of data structure that can generate a balanced binary search
tree. Here is an example of a balanced binary tree
Degenerate Binary Tree
• A binary tree is said to be a degenerate binary tree or
pathological binary tree if every internal node has only a single
child.
• Such trees are similar to a linked list performance-wise. Here
is an example of a degenerate binary tree:
Benefits of a Binary Tree
• The search operation in a binary tree is faster as
compared to other trees
• Only two traversals are enough to provide the elements
in sorted order
• It is easy to pick up the maximum and minimum
elements
• Graph traversal also uses binary trees
• Converting different postfix and prefix expressions are
possible using binary trees
Expression Trees
• An Expression tree is a binary tree in which the operators are
stored in the interior nodes and the operands are stored in
the exterior nodes which are the leaves
• Construction of Expression Tree:
• 1) If character is operand push that into stack
2) If character is operator pop two values from stack make
them its child and push current node again.
At the end only element of stack will be root of expression
tree.
• https://www.techiedelight.com/expression-tree/
Applications of Trees
1) Manipulate hierarchical data
2) Make information easy to search
3) Manipulate sorted lists of data
4) Router algorithms
5) Form of a multi-stage decision-making, like Chess Game
BinarySearchTree(BST)
• Abinary search tree (BST)is a binary tree that is either
empty or in which every node contains a key (value)
and satisfies the following conditions:
• All keys in the left sub-tree of the root are smaller than
the key in the root
• node
• All keysin the right sub-tree of the root are greater than the
keyin the root node
• Theleft and right sub-trees of the root are againbinary
search trees
WhyBinarySearchTree?
• Some maythink of usingalinked list becauseit permits
insertion and deletion to be carried outby adjusting only
few pointers.
• But in ann-linked list, there isno wayto move through
thelist other than one node at atime, permitting only
sequentialaccess.
• Binary trees provide anexcellent solution to this problem.
Bymaking the entries of anordered list into the nodes of a
binary searchtree, we find that we cansearchfor akeyin
O(logn)
Insertion,Deletion,Search
• https://www.codesdope.com/course/data-structures-binary-
search-trees/
• https://www.codespeedy.com/insertion-and-deletion-in-a-
binary-search-tree-in-python/
Threaded binary trees
• Threaded binary tree is a simple binary tree but they have a
speciality that null pointers of leaf node of the binary tree is
set to inorder predecessor or inorder successor.
• https://www.youtube.com/watch?v=ffgg_zmbaxw
• Operations:
• Insert
• Search
• Delete
Applications
• To make inorder traversal of the binary tree faster and do it
without using any extra space, so sometimes in small systems
where hardware is very limited we use threaded binary tree
for better efficiency of the software in a limited hardware
space.

More Related Content

What's hot (20)

Tree
TreeTree
Tree
 
Tree
TreeTree
Tree
 
Tree
TreeTree
Tree
 
Data structure(Part 2)
Data structure(Part 2)Data structure(Part 2)
Data structure(Part 2)
 
Tree
TreeTree
Tree
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Introduction to tree ds
Introduction to tree dsIntroduction to tree ds
Introduction to tree ds
 
Binary tree traversal ppt - 02.03.2020
Binary tree traversal   ppt - 02.03.2020Binary tree traversal   ppt - 02.03.2020
Binary tree traversal ppt - 02.03.2020
 
Trees
TreesTrees
Trees
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structures
 
Trees
TreesTrees
Trees
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary trees
Binary treesBinary trees
Binary trees
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
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
 

Similar to Unit 3 trees

tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptxMouDhara1
 
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 presentationnakulvarshney371
 
Tree Basic concepts of Tree in Data Structure
Tree Basic concepts of Tree in Data StructureTree Basic concepts of Tree in Data Structure
Tree Basic concepts of Tree in Data StructureManoj PAtil
 
Lecture 9: Binary tree basics
Lecture 9: Binary tree basicsLecture 9: Binary tree basics
Lecture 9: Binary tree basicsVivek Bhargav
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graphRai University
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana Shaikh
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptxRahulAI
 
Binary search tree
Binary search treeBinary search tree
Binary search treeSana Yameen
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10sumitbardhan
 

Similar to Unit 3 trees (20)

tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 
Unit 5 Tree.pptx
Unit 5 Tree.pptxUnit 5 Tree.pptx
Unit 5 Tree.pptx
 
Data Structures 4
Data Structures 4Data Structures 4
Data Structures 4
 
TREES34.pptx
TREES34.pptxTREES34.pptx
TREES34.pptx
 
Module - 5_Trees.pdf
Module - 5_Trees.pdfModule - 5_Trees.pdf
Module - 5_Trees.pdf
 
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
 
Binary tree
Binary treeBinary tree
Binary tree
 
Tree 11.ppt
Tree 11.pptTree 11.ppt
Tree 11.ppt
 
Tree Basic concepts of Tree in Data Structure
Tree Basic concepts of Tree in Data StructureTree Basic concepts of Tree in Data Structure
Tree Basic concepts of Tree in Data Structure
 
Unit III.ppt
Unit III.pptUnit III.ppt
Unit III.ppt
 
Lecture 9: Binary tree basics
Lecture 9: Binary tree basicsLecture 9: Binary tree basics
Lecture 9: Binary tree basics
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 
Bsc cs ii dfs u-3 tree and graph
Bsc cs  ii dfs u-3 tree and graphBsc cs  ii dfs u-3 tree and graph
Bsc cs ii dfs u-3 tree and graph
 
Farhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructureFarhana shaikh webinar_treesindiscretestructure
Farhana shaikh webinar_treesindiscretestructure
 
Tree
TreeTree
Tree
 
Tree Introduction.pptx
Tree Introduction.pptxTree Introduction.pptx
Tree Introduction.pptx
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 

More from LavanyaJ28

Cs1301 syllabus
Cs1301  syllabusCs1301  syllabus
Cs1301 syllabusLavanyaJ28
 
Ds important questions
Ds important questionsDs important questions
Ds important questionsLavanyaJ28
 
2 marks- DS using python
2 marks- DS using python2 marks- DS using python
2 marks- DS using pythonLavanyaJ28
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sortingLavanyaJ28
 
Heap types & Trees
Heap types & TreesHeap types & Trees
Heap types & TreesLavanyaJ28
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures LavanyaJ28
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures LavanyaJ28
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack LavanyaJ28
 
Stack and queue
Stack and queueStack and queue
Stack and queueLavanyaJ28
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked listLavanyaJ28
 
Unit 1 Basic concepts to DS
Unit 1 Basic concepts to DSUnit 1 Basic concepts to DS
Unit 1 Basic concepts to DSLavanyaJ28
 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementationLavanyaJ28
 
Unit 1 polynomial manipulation
Unit 1   polynomial manipulationUnit 1   polynomial manipulation
Unit 1 polynomial manipulationLavanyaJ28
 
Unit 1 abstract data types
Unit 1 abstract data typesUnit 1 abstract data types
Unit 1 abstract data typesLavanyaJ28
 

More from LavanyaJ28 (16)

Cs1301 syllabus
Cs1301  syllabusCs1301  syllabus
Cs1301 syllabus
 
Ds important questions
Ds important questionsDs important questions
Ds important questions
 
2 marks- DS using python
2 marks- DS using python2 marks- DS using python
2 marks- DS using python
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sorting
 
Hashing
HashingHashing
Hashing
 
Graphs
GraphsGraphs
Graphs
 
Heap types & Trees
Heap types & TreesHeap types & Trees
Heap types & Trees
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
Unit 1 linked list
Unit 1 linked listUnit 1 linked list
Unit 1 linked list
 
Unit 1 Basic concepts to DS
Unit 1 Basic concepts to DSUnit 1 Basic concepts to DS
Unit 1 Basic concepts to DS
 
Unit 1 array based implementation
Unit 1  array based implementationUnit 1  array based implementation
Unit 1 array based implementation
 
Unit 1 polynomial manipulation
Unit 1   polynomial manipulationUnit 1   polynomial manipulation
Unit 1 polynomial manipulation
 
Unit 1 abstract data types
Unit 1 abstract data typesUnit 1 abstract data types
Unit 1 abstract data types
 

Recently uploaded

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 

Recently uploaded (20)

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 

Unit 3 trees

  • 1. UNIT 3 - TREES
  • 2. Tree ADT • Treesare mainly usedto represent data containing a hierarchical relationship between elements, for example, records, family treesand table of contents. • Consider aparent-child relationship
  • 3. • Treeis asequence of nodes • There is astarting node known asaroot node • Everynode other than the root hasaparentnode. • Nodes mayhaveany number of children
  • 4.
  • 5.
  • 6. Key terms • Root−Node at the top of the treeiscalled root. • Parent −Anynode except root node hasone edgeupward to anode called parent. • Child−Node below agiven node connected by its edgedownward is called its child node. • Sibling– Childof samenode are calledsiblings • Leaf−Node which does not haveanychild node is called leaf node. • Subtree −Subtree represents descendants of anode. • Levels−Levelof anode represents the generation of anode. If root node is at level 0, then itsnext child node isat level 1, its grandchild isat level 2 and soon. • keys−Keyrepresents avalue of anode basedon which asearch operation is to be carried out for anode.
  • 7. • Degreeof anode: • Thedegree of anode isthe number of children of that node • Degreeof aTree: • Thedegree of atree isthe maximum degree of nodes in agiventree • Path: • It isthe sequenceof consecutive edgesfrom sourcenode to destination node. • Height of anode: • Theheight of anode isthe maxpath length form that node to aleaf node. • Height of atree: • Theheight of atree isthe height ofthe root • Depth of atree: • Depth of atree isthe maxlevel of anyleaf in the tree
  • 8.
  • 9. Characteristicsof trees • Non-linear datastructure • Combinesadvantagesof anordered array • Searchingasfast asin ordered array • Insertion and deletion asfast asin linked list • Simple and fast
  • 10. Applications • Directory structure of afilestore • Structure of an arithmeticexpressions • Usedin almost every 3Dvideo gameto determine what objects need tobe rendered. • Usedin almost every high-bandwidth router for storing router-tables. • used in compression algorithms, suchasthose used by the .jpeg and .mp3 file- formats.
  • 11. Tree Traversal • Traversalis aprocessto visit all the nodes of atree and mayprinttheir values too. • All nodes are connected via edges(links) we always start from theroot (head) node. • There are three wayswhich we useto traverse atree • In-orderTraversal • Pre-orderTraversal • Post-orderTraversal • Generally we traverse atree to search or locate given item or keyin the tree or to print all the values it contains.
  • 12. Pre-order,In-order,Post-order • Pre-order • <root><left><right> • In-order • <left><root><right> • Post-order • <left><right><root>
  • 13. Pre-orderTraversal • Thepreorder traversal of anonempty binary tree is defined asfollows: • Visit the root node • Traversethe left sub-tree inpreorder • Traversethe right sub-tree inpreorder
  • 14. In-ordertraversal • Thein-order traversal of anonempty binary tree is defined asfollows: • Traversethe left sub-tree inin-order • Visit the root node • Traversethe right sub-tree ininorder • Thein-order traversal output of the given treeis • H D I B E A F C G
  • 15. Post-ordertraversal • Thein-order traversal of anonempty binary tree is defined asfollows: • Traversethe left sub-tree inpost-order • Traversethe right sub-tree inpost-order • Visit • Thein-order traversal output of the given treeis • H I D E B F G C A • the root node
  • 16. Binary Trees • Abinary tree, isatree in which no node canhave more thantwo children. • Consider abinary tree T ,here ‘A’isthe root node of the binary treeT . • ‘B’ isthe left child of ‘A’and ‘C’istheright child of ‘A’ • i.eAisafather of BandC. • Thenode BandCarecalled siblings. • NodesD,H,I,F ,Jare leafnode
  • 17.
  • 18. • Theroot node of this binary tree isA. • Theleft subtree of the root node, which we denoted by LA,istheset LA ={B,D,E,G}andthe right subtree of the root node, RA isthe set RA={C,F ,H} • Theroot node of LAisnode B,the root node of RA isCandso on
  • 19. BinaryTreeProperties • If abinary tree contains m nodesat level L,it contains atmost 2m nodes at levelL+1 • Sinceabinary tree cancontain at most 1 node at level 0 (the root),it contains at most 2Lnodesat levelL.
  • 20. Typesof BinaryTree • Complete binary tree • Strictly binary tree • Full binary tree • Perfect binary tree • Balanced binary tree • Degenerate binary tree
  • 21. Strictly binarytree • If every non-leaf node in abinary tree hasnonempty leftandright sub- trees,then suchatree iscalled astrictly binarytree. • Or,to put it anotherway,all of the nodesin astrictly binary tree areof degreezero or two, never degreeone. • Astrictly binary tree with • Nleavesalwayscontains 2N–1 nodes.
  • 22. Completebinarytree • Acomplete binary tree is a binary tree in which every level, except possibly the last, is completely • filled, and all nodes are asfar left aspossible. • Acomplete binary tree of depth d is called strictly binary tree if all of whose leaves are at level d. • Acomplete binary tree has2d nodes at every depthd and 2d -1 non leaf nodes
  • 23. Full binary tree • It is a special kind of a binary tree that has either zero children or two children. It means that all the nodes in that binary tree should either have two child nodes of its parent node or the parent node is itself the leaf node or the external node. • In other words, a full binary tree is a unique binary tree where every node except the external node has two children. When it holds a single child, such a binary tree will not be a full binary tree. Here, the quantity of leaf nodes is equal to the number of internal nodes plus one. The equation is like L=I+1, where L is the number of leaf nodes, and I is the number of internal nodes.
  • 24. Perfect binary tree • A binary tree is said to be ‘perfect’ if all the internal nodes have strictly two children, and every external or leaf node is at the same level or same depth within a tree. A perfect binary tree having height ‘h’ has 2h – 1 node. Here is the structure of a perfect binary tree
  • 25. Balanced Binary Tree • A binary tree is said to be ‘balanced’ if the tree height is O(logN), where ‘N’ is the number of nodes. In a balanced binary tree, the height of the left and the right subtrees of each node should vary by at most one. • An AVL Tree and a Red-Black Tree are some common examples of data structure that can generate a balanced binary search tree. Here is an example of a balanced binary tree
  • 26. Degenerate Binary Tree • A binary tree is said to be a degenerate binary tree or pathological binary tree if every internal node has only a single child. • Such trees are similar to a linked list performance-wise. Here is an example of a degenerate binary tree:
  • 27. Benefits of a Binary Tree • The search operation in a binary tree is faster as compared to other trees • Only two traversals are enough to provide the elements in sorted order • It is easy to pick up the maximum and minimum elements • Graph traversal also uses binary trees • Converting different postfix and prefix expressions are possible using binary trees
  • 28. Expression Trees • An Expression tree is a binary tree in which the operators are stored in the interior nodes and the operands are stored in the exterior nodes which are the leaves • Construction of Expression Tree: • 1) If character is operand push that into stack 2) If character is operator pop two values from stack make them its child and push current node again. At the end only element of stack will be root of expression tree. • https://www.techiedelight.com/expression-tree/
  • 29. Applications of Trees 1) Manipulate hierarchical data 2) Make information easy to search 3) Manipulate sorted lists of data 4) Router algorithms 5) Form of a multi-stage decision-making, like Chess Game
  • 30. BinarySearchTree(BST) • Abinary search tree (BST)is a binary tree that is either empty or in which every node contains a key (value) and satisfies the following conditions: • All keys in the left sub-tree of the root are smaller than the key in the root • node • All keysin the right sub-tree of the root are greater than the keyin the root node • Theleft and right sub-trees of the root are againbinary search trees
  • 31.
  • 32. WhyBinarySearchTree? • Some maythink of usingalinked list becauseit permits insertion and deletion to be carried outby adjusting only few pointers. • But in ann-linked list, there isno wayto move through thelist other than one node at atime, permitting only sequentialaccess. • Binary trees provide anexcellent solution to this problem. Bymaking the entries of anordered list into the nodes of a binary searchtree, we find that we cansearchfor akeyin O(logn)
  • 34. Threaded binary trees • Threaded binary tree is a simple binary tree but they have a speciality that null pointers of leaf node of the binary tree is set to inorder predecessor or inorder successor. • https://www.youtube.com/watch?v=ffgg_zmbaxw • Operations: • Insert • Search • Delete
  • 35. Applications • To make inorder traversal of the binary tree faster and do it without using any extra space, so sometimes in small systems where hardware is very limited we use threaded binary tree for better efficiency of the software in a limited hardware space.