SlideShare a Scribd company logo
1 of 47
Advanced Algorithm Analysis
MSCS 1
Binary Tree Data Structure
• A tree whose
elements have at
most 2 children is
called a binary
tree. Since each
element in a binary
tree can have only
2 children, we
typically name
them the left and
right child.
Binary Search Tree Data Structure
• Binary Search Tree is a node-based binary tree data structure which
has the following properties:
• The left sub-tree of a node contains only nodes with keys lesser
than the node’s key.
• The right sub-tree of a node contains only nodes with keys greater
than the node’s key.
• The left and right sub-tree each must also be a binary search tree.
• There must be no duplicate nodes.
Binary Search Tree Data Structure
8, 3, 1, 6, 4, 7, 10, 14, 13
Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• Binary tree mean a node having at most 2
children. Either 0, 1 or 2.
• The left subtree is less then its parent
node and right subtree is grater then its
parent.
Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 9 is less then 11 and 9 is greater then 7.
Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• 6 is less then 11 and 8 is also less then 11,
so put it in left but 8 is greater then 6 so
put it in right.
Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• Now 19 is greater then 11. place it in right.
Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• 10 is less then 11 so go on left side.
Binary Search Tree
• Draw a binary search tree by inserting the
following numbers from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• 5 is less then 11, it could be in left of the
tree.
• 17 is greater then 11, it will be placed in
right.
Binary Search Tree
• Draw a binary search tree by
inserting the following numbers
from left to right.
• 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31
• 43 on right, 31 is left of 43 and 49
will be in right of 43.
Binary Search Tree
• Draw a binary search tree by
inserting the following numbers
from left to right.
• If insert 60 in BST.
Binary Search Tree
Binary Search Tree
• O(h) time on a tree of height h.
• The binary search tree is a balanced binary search tree.
• Height of the binary search tree becomes log(n).
• So, Time complexity of BST Operations = O(logn).
• The binary search tree is a skewed binary search tree.
• Height of the binary search tree becomes n.
• So, Time complexity of BST Operations = O(n).
Binary Search Tree (Deletion)
• Have 3 possibilities
• Node have 0 child
• Have 1 child
• Have 2 child
Binary Search Tree (Deletion)
• Node have 0 child
Binary Search Tree (Deletion)
• Node have 1 child
Binary Search Tree (Deletion)
• Node have 2 children
• In order predecessor
• In order successor
Binary Search Tree (Deletion)
• In order predecessor
• 10 is the largest element in left of the tree
Binary Search Tree (Deletion)
• In order successor
• The smallest element in right of the tree
Difference between Binary Tree and Binary
Search Tree:
• BINARY TREE is a non linear data structure where each node can
have almost two child nodes.
• BINARY SEARCH TREE is a node based binary tree which further has
right and left subtree that too are binary search tree.
• BINARY TREE is unordered hence slower in process of insertion,
deletion and searching.
• Insertion, deletion, searching of an element is faster in BINARY
SEARCH TREE than BINARY TREE due to the ordered characteristics
Difference between Binary Tree and Binary
Search Tree:
• IN BINARY TREE there is no ordering in terms of how the nodes are
arranged.
• IN BINARY SEARCH TREE the left subtree has elements less than the
nodes element and the right subtree has elements greater than the
nodes element.
Problems with BST:
Problems with BST:
• Height is not under control
• It depends on that how the elements we are going to insert.
• Sometimes its logn and sometimes its n.
• It’s a big issue in BST.
Problems with BST:
• Can we improve BST?
• Is it a chance to improve?
• YES
AVL Tree
Can we
arrange the
key elements
in BST?
First 4 orders
height is 3
and last two
elements its
2.
AVL Tree
• If we are arranging the elements in a order we get the
minimum height is the best order.
• We can reduce the time complexity of searching in entire
binary tree.
• That is drawback of a binary tree that we can make different
trees in different shapes with different heights.
• Whether BST can be improved or not?
• Yes, we should have a procedure which can convert the best
order tree.
AVL Tree
• Rotation is the
process to
change the
ideal shape.
AVL Tree
• In computer science, an AVL tree (named after inventors Adelson-Velsky and
Landis) is a self-balancing binary search tree. It was the first such data
structure to be invented.[2] In an AVL tree, the heights of the two child
subtrees of any node differ by at most one; if at any time they differ by more
than one, rebalancing is done to restore this property. Lookup, insertion,
and deletion all take O(log n) time in both the average and worst cases,
where n {displaystyle n} n is the number of nodes in the tree prior to the
operation. Insertions and deletions may require the tree to be rebalanced
by one or more tree rotations.
• The AVL tree is named after its two Soviet inventors, Georgy Adelson-Velsky
and Evgenii Landis, who published it in their 1962 paper "An algorithm for
the organization of information".[
•AVL is a height balanced binary search tree.
AVL Tree
• Balance factor = Height of left sub tree – height of right sub tree.
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
AVL Tree
• Balance factor = Height of left sub tree – height of right sub tree.
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• So the height of first tree is balanced and others are not balanced.
AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• Right of right
imbalanced
• 2 step rotation.
AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• 2 step rotation.
AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• Left of left imbalanced
AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• left of right imbalanced
AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• So here 2 step imbalanced
AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• Left to right imbalanced
• So here 2 step imbalanced
AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• Left to right imbalanced
• Now every thing is balanced.
AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• Imbalanced
AVL Tree
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• Imbalanced
AVL Tree (new example)
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• Insert 42
AVL Tree (new example)
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• Insert 42
AVL Tree (new example)
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• Insert 42
AVL Tree (new example)
• bf = hl-hr = {-1,0,+1}
• |bf| = |hl – hr| <= 1
• Insert 42
AVL Tree (new example)
• We can get the balanced height
• Average and worst time complexity will be O(logn)

More Related Content

What's hot

Employee management system in Software Engineering
Employee management system in Software EngineeringEmployee management system in Software Engineering
Employee management system in Software EngineeringSadia Akter
 
Synopsis on railway reservation system
Synopsis on railway reservation systemSynopsis on railway reservation system
Synopsis on railway reservation systemAnkit Verma
 
DBMS - Normalization
DBMS - NormalizationDBMS - Normalization
DBMS - NormalizationJitendra Tomar
 
Online Bus Ticketing System
Online Bus Ticketing SystemOnline Bus Ticketing System
Online Bus Ticketing SystemKiran Shahi
 
Online Ticket Reservation System-SRS, ERD, DFD, Structured Charts
Online Ticket Reservation System-SRS, ERD, DFD, Structured ChartsOnline Ticket Reservation System-SRS, ERD, DFD, Structured Charts
Online Ticket Reservation System-SRS, ERD, DFD, Structured Chartsgrandhiprasuna
 
Relational algebra in DBMS
Relational algebra in DBMSRelational algebra in DBMS
Relational algebra in DBMSArafat Hossan
 
project report V 2.0 By Amit Mangukiya
project report V 2.0 By Amit Mangukiyaproject report V 2.0 By Amit Mangukiya
project report V 2.0 By Amit MangukiyaAmit Mangukiya
 
Leave management ppt made by krishna ballabh gupta
Leave management ppt made by krishna ballabh gupta Leave management ppt made by krishna ballabh gupta
Leave management ppt made by krishna ballabh gupta Shivalik college of engineering
 
University Database Management Project
University Database Management Project University Database Management Project
University Database Management Project Kavi
 
JUSTCABS - an Online Cab Reservation System (Final Year Project)
JUSTCABS - an Online Cab Reservation System (Final Year Project)JUSTCABS - an Online Cab Reservation System (Final Year Project)
JUSTCABS - an Online Cab Reservation System (Final Year Project)Amartya .
 
ICT Software Industry in Bangladesh
ICT Software Industry in Bangladesh ICT Software Industry in Bangladesh
ICT Software Industry in Bangladesh Aleef Muhammad
 
E-Restaurant Management System
E-Restaurant Management SystemE-Restaurant Management System
E-Restaurant Management SystemArno Lordkronos
 
E book management system
E book management systemE book management system
E book management systemBarani Tharan
 
Library Management System.powerpoint.pptx
Library Management System.powerpoint.pptxLibrary Management System.powerpoint.pptx
Library Management System.powerpoint.pptxKaiumShuvo1
 
Hostel managements system
Hostel managements systemHostel managements system
Hostel managements systemFahad Chishti
 
Online vehicle renting website
Online vehicle renting websiteOnline vehicle renting website
Online vehicle renting websiteSomendra Singh
 
Design and Implementation of Student Profile and Placement management system
Design and Implementation of Student Profile and Placement management systemDesign and Implementation of Student Profile and Placement management system
Design and Implementation of Student Profile and Placement management systemChamanth MVS
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Trainingbixxman
 

What's hot (20)

Employee management system in Software Engineering
Employee management system in Software EngineeringEmployee management system in Software Engineering
Employee management system in Software Engineering
 
Synopsis on railway reservation system
Synopsis on railway reservation systemSynopsis on railway reservation system
Synopsis on railway reservation system
 
DBMS - Normalization
DBMS - NormalizationDBMS - Normalization
DBMS - Normalization
 
Online Bus Ticketing System
Online Bus Ticketing SystemOnline Bus Ticketing System
Online Bus Ticketing System
 
Online examination system
Online examination systemOnline examination system
Online examination system
 
Online Ticket Reservation System-SRS, ERD, DFD, Structured Charts
Online Ticket Reservation System-SRS, ERD, DFD, Structured ChartsOnline Ticket Reservation System-SRS, ERD, DFD, Structured Charts
Online Ticket Reservation System-SRS, ERD, DFD, Structured Charts
 
Relational algebra in DBMS
Relational algebra in DBMSRelational algebra in DBMS
Relational algebra in DBMS
 
project report V 2.0 By Amit Mangukiya
project report V 2.0 By Amit Mangukiyaproject report V 2.0 By Amit Mangukiya
project report V 2.0 By Amit Mangukiya
 
Leave management ppt made by krishna ballabh gupta
Leave management ppt made by krishna ballabh gupta Leave management ppt made by krishna ballabh gupta
Leave management ppt made by krishna ballabh gupta
 
University Database Management Project
University Database Management Project University Database Management Project
University Database Management Project
 
JUSTCABS - an Online Cab Reservation System (Final Year Project)
JUSTCABS - an Online Cab Reservation System (Final Year Project)JUSTCABS - an Online Cab Reservation System (Final Year Project)
JUSTCABS - an Online Cab Reservation System (Final Year Project)
 
Online exam
Online examOnline exam
Online exam
 
ICT Software Industry in Bangladesh
ICT Software Industry in Bangladesh ICT Software Industry in Bangladesh
ICT Software Industry in Bangladesh
 
E-Restaurant Management System
E-Restaurant Management SystemE-Restaurant Management System
E-Restaurant Management System
 
E book management system
E book management systemE book management system
E book management system
 
Library Management System.powerpoint.pptx
Library Management System.powerpoint.pptxLibrary Management System.powerpoint.pptx
Library Management System.powerpoint.pptx
 
Hostel managements system
Hostel managements systemHostel managements system
Hostel managements system
 
Online vehicle renting website
Online vehicle renting websiteOnline vehicle renting website
Online vehicle renting website
 
Design and Implementation of Student Profile and Placement management system
Design and Implementation of Student Profile and Placement management systemDesign and Implementation of Student Profile and Placement management system
Design and Implementation of Student Profile and Placement management system
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 

Similar to Binary tree data structure

Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREESTABISH HAMID
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)amna izzat
 
BinarySearchTree-bddicken
BinarySearchTree-bddickenBinarySearchTree-bddicken
BinarySearchTree-bddickenBenjamin Dicken
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11sumitbardhan
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structureAnusruti Mitra
 
Binary search tree
Binary search treeBinary search tree
Binary search treeSana Yameen
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologyKamranAli649587
 
4a searching-more
4a searching-more4a searching-more
4a searching-moreShahzad Ali
 
Avl trees
Avl treesAvl trees
Avl treesXad Kuain
 
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfSanghdipUdrake
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptxTrad5
 

Similar to Binary tree data structure (20)

Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
 
AVL Trees
AVL TreesAVL Trees
AVL Trees
 
BinarySearchTree-bddicken
BinarySearchTree-bddickenBinarySearchTree-bddicken
BinarySearchTree-bddicken
 
Binary tree
Binary treeBinary tree
Binary tree
 
6_1 (1).ppt
6_1 (1).ppt6_1 (1).ppt
6_1 (1).ppt
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Binary search tree.pptx
Binary search tree.pptxBinary search tree.pptx
Binary search tree.pptx
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
lecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminologylecture10 date structure types of graph and terminology
lecture10 date structure types of graph and terminology
 
Avl tree
Avl treeAvl tree
Avl tree
 
4a searching-more
4a searching-more4a searching-more
4a searching-more
 
Avl trees
Avl treesAvl trees
Avl trees
 
TREES.pptx
TREES.pptxTREES.pptx
TREES.pptx
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
LEC 5-DS ALGO(updated).pdf
LEC 5-DS  ALGO(updated).pdfLEC 5-DS  ALGO(updated).pdf
LEC 5-DS ALGO(updated).pdf
 
Lecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdfLecture 10 - AVL Trees.pdf
Lecture 10 - AVL Trees.pdf
 
AVL Tree.pptx
AVL Tree.pptxAVL Tree.pptx
AVL Tree.pptx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 

More from Learning Courses Online

Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmLearning Courses Online
 
Introduction to Hash Tables | What is a HashTable in Algorithm
Introduction to Hash Tables | What is a HashTable in AlgorithmIntroduction to Hash Tables | What is a HashTable in Algorithm
Introduction to Hash Tables | What is a HashTable in AlgorithmLearning Courses Online
 

More from Learning Courses Online (6)

Convolutional neural networks
Convolutional neural  networksConvolutional neural  networks
Convolutional neural networks
 
Heap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap AlgorithmHeap Sort || Heapify Method || Build Max Heap Algorithm
Heap Sort || Heapify Method || Build Max Heap Algorithm
 
Introduction to Hash Tables | What is a HashTable in Algorithm
Introduction to Hash Tables | What is a HashTable in AlgorithmIntroduction to Hash Tables | What is a HashTable in Algorithm
Introduction to Hash Tables | What is a HashTable in Algorithm
 
Fractional knapsack problem
Fractional knapsack problemFractional knapsack problem
Fractional knapsack problem
 
8 queens problem using ga
8 queens problem using ga8 queens problem using ga
8 queens problem using ga
 
Simple auto encoder
Simple auto encoderSimple auto encoder
Simple auto encoder
 

Recently uploaded

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersChitralekhaTherkar
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Micromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of PowdersMicromeritics - Fundamental and Derived Properties of Powders
Micromeritics - Fundamental and Derived Properties of Powders
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Binary tree data structure

  • 2. Binary Tree Data Structure • A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right child.
  • 3. Binary Search Tree Data Structure • Binary Search Tree is a node-based binary tree data structure which has the following properties: • The left sub-tree of a node contains only nodes with keys lesser than the node’s key. • The right sub-tree of a node contains only nodes with keys greater than the node’s key. • The left and right sub-tree each must also be a binary search tree. • There must be no duplicate nodes.
  • 4. Binary Search Tree Data Structure 8, 3, 1, 6, 4, 7, 10, 14, 13
  • 5. Binary Search Tree • Draw a binary search tree by inserting the following numbers from left to right. • 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31 • Binary tree mean a node having at most 2 children. Either 0, 1 or 2. • The left subtree is less then its parent node and right subtree is grater then its parent.
  • 6. Binary Search Tree • Draw a binary search tree by inserting the following numbers from left to right. • 9 is less then 11 and 9 is greater then 7.
  • 7. Binary Search Tree • Draw a binary search tree by inserting the following numbers from left to right. • 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31 • 6 is less then 11 and 8 is also less then 11, so put it in left but 8 is greater then 6 so put it in right.
  • 8. Binary Search Tree • Draw a binary search tree by inserting the following numbers from left to right. • 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31 • Now 19 is greater then 11. place it in right.
  • 9. Binary Search Tree • Draw a binary search tree by inserting the following numbers from left to right. • 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31 • 10 is less then 11 so go on left side.
  • 10. Binary Search Tree • Draw a binary search tree by inserting the following numbers from left to right. • 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31 • 5 is less then 11, it could be in left of the tree. • 17 is greater then 11, it will be placed in right.
  • 11. Binary Search Tree • Draw a binary search tree by inserting the following numbers from left to right. • 11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31 • 43 on right, 31 is left of 43 and 49 will be in right of 43.
  • 12. Binary Search Tree • Draw a binary search tree by inserting the following numbers from left to right. • If insert 60 in BST.
  • 14. Binary Search Tree • O(h) time on a tree of height h. • The binary search tree is a balanced binary search tree. • Height of the binary search tree becomes log(n). • So, Time complexity of BST Operations = O(logn). • The binary search tree is a skewed binary search tree. • Height of the binary search tree becomes n. • So, Time complexity of BST Operations = O(n).
  • 15. Binary Search Tree (Deletion) • Have 3 possibilities • Node have 0 child • Have 1 child • Have 2 child
  • 16. Binary Search Tree (Deletion) • Node have 0 child
  • 17. Binary Search Tree (Deletion) • Node have 1 child
  • 18. Binary Search Tree (Deletion) • Node have 2 children • In order predecessor • In order successor
  • 19. Binary Search Tree (Deletion) • In order predecessor • 10 is the largest element in left of the tree
  • 20. Binary Search Tree (Deletion) • In order successor • The smallest element in right of the tree
  • 21. Difference between Binary Tree and Binary Search Tree: • BINARY TREE is a non linear data structure where each node can have almost two child nodes. • BINARY SEARCH TREE is a node based binary tree which further has right and left subtree that too are binary search tree. • BINARY TREE is unordered hence slower in process of insertion, deletion and searching. • Insertion, deletion, searching of an element is faster in BINARY SEARCH TREE than BINARY TREE due to the ordered characteristics
  • 22. Difference between Binary Tree and Binary Search Tree: • IN BINARY TREE there is no ordering in terms of how the nodes are arranged. • IN BINARY SEARCH TREE the left subtree has elements less than the nodes element and the right subtree has elements greater than the nodes element.
  • 24. Problems with BST: • Height is not under control • It depends on that how the elements we are going to insert. • Sometimes its logn and sometimes its n. • It’s a big issue in BST.
  • 25. Problems with BST: • Can we improve BST? • Is it a chance to improve? • YES
  • 26. AVL Tree Can we arrange the key elements in BST? First 4 orders height is 3 and last two elements its 2.
  • 27. AVL Tree • If we are arranging the elements in a order we get the minimum height is the best order. • We can reduce the time complexity of searching in entire binary tree. • That is drawback of a binary tree that we can make different trees in different shapes with different heights. • Whether BST can be improved or not? • Yes, we should have a procedure which can convert the best order tree.
  • 28. AVL Tree • Rotation is the process to change the ideal shape.
  • 29. AVL Tree • In computer science, an AVL tree (named after inventors Adelson-Velsky and Landis) is a self-balancing binary search tree. It was the first such data structure to be invented.[2] In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n {displaystyle n} n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations. • The AVL tree is named after its two Soviet inventors, Georgy Adelson-Velsky and Evgenii Landis, who published it in their 1962 paper "An algorithm for the organization of information".[ •AVL is a height balanced binary search tree.
  • 30. AVL Tree • Balance factor = Height of left sub tree – height of right sub tree. • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1
  • 31. AVL Tree • Balance factor = Height of left sub tree – height of right sub tree. • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • So the height of first tree is balanced and others are not balanced.
  • 32. AVL Tree • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • Right of right imbalanced • 2 step rotation.
  • 33. AVL Tree • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • 2 step rotation.
  • 34. AVL Tree • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • Left of left imbalanced
  • 35. AVL Tree • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • left of right imbalanced
  • 36. AVL Tree • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1
  • 37. AVL Tree • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1
  • 38. AVL Tree • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • So here 2 step imbalanced
  • 39. AVL Tree • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • Left to right imbalanced • So here 2 step imbalanced
  • 40. AVL Tree • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • Left to right imbalanced • Now every thing is balanced.
  • 41. AVL Tree • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • Imbalanced
  • 42. AVL Tree • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • Imbalanced
  • 43. AVL Tree (new example) • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • Insert 42
  • 44. AVL Tree (new example) • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • Insert 42
  • 45. AVL Tree (new example) • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • Insert 42
  • 46. AVL Tree (new example) • bf = hl-hr = {-1,0,+1} • |bf| = |hl – hr| <= 1 • Insert 42
  • 47. AVL Tree (new example) • We can get the balanced height • Average and worst time complexity will be O(logn)