SlideShare a Scribd company logo
1 of 31
Lecture # 10Lecture # 10
Operations on Binary TreeOperations on Binary Tree
 There are a number of operations that canThere are a number of operations that can
be defined for a binary tree.be defined for a binary tree.
 IfIf pp is pointing to a node in an existing tree thenis pointing to a node in an existing tree then
 left(left(pp)) returns pointer to the left subtreereturns pointer to the left subtree
 right(right(pp)) returns pointer to right subtreereturns pointer to right subtree
 parent(parent(pp)) returns the father ofreturns the father of pp
 brother(brother(pp)) returns brother ofreturns brother of pp..
 info(p)info(p) returns content of the node.returns content of the node.
Operations on Binary TreeOperations on Binary Tree
 In order to construct a binary tree, theIn order to construct a binary tree, the
following can be useful:following can be useful:
 setLeft(p,x)setLeft(p,x) creates the left child node of p.creates the left child node of p.
The child node contains the info ‘x’.The child node contains the info ‘x’.
 setRight(p,x)setRight(p,x) creates the right child node ofcreates the right child node of
p. The child node contains the info ‘x’.p. The child node contains the info ‘x’.
Applications of Binary TreesApplications of Binary Trees
 A binary tree is a useful data structure when two-A binary tree is a useful data structure when two-
way decisions must be made at each point in away decisions must be made at each point in a
process.process.
 For example, suppose we wanted to find allFor example, suppose we wanted to find all
duplicates in a list of numbers:duplicates in a list of numbers:
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 514, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Applications of Binary TreesApplications of Binary Trees
 One way of finding duplicates is to compareOne way of finding duplicates is to compare
each number with all those thateach number with all those that precedeprecede it.it.
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 514, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 514, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
Searching for DuplicatesSearching for Duplicates
 If the list of numbers is large and is growing, thisIf the list of numbers is large and is growing, this
procedure involves a large number ofprocedure involves a large number of
comparisons.comparisons.
 A linked list could handle the growth but theA linked list could handle the growth but the
comparisons would still be large.comparisons would still be large.
 The number of comparisons can be drasticallyThe number of comparisons can be drastically
reduced by using a binary tree.reduced by using a binary tree.
 The tree grows dynamically like the linked list.The tree grows dynamically like the linked list.
Searching for DuplicatesSearching for Duplicates
 The binary tree is built in a special way.The binary tree is built in a special way.
 The first number in the list is placed in a nodeThe first number in the list is placed in a node
that is designated as the root of a binary tree.that is designated as the root of a binary tree.
 Initially, both left and right subtrees of the root areInitially, both left and right subtrees of the root are
empty.empty.
 We take the next number and compare it with theWe take the next number and compare it with the
number placed in the root.number placed in the root.
 If it is the same then we have a duplicate.If it is the same then we have a duplicate.
Searching for DuplicatesSearching for Duplicates
 Otherwise, we create a new tree node and put theOtherwise, we create a new tree node and put the
new number in it.new number in it.
 The new node is made the left child of the rootThe new node is made the left child of the root
node if the second number is less than the one innode if the second number is less than the one in
the root.the root.
 The new node is made the right child if theThe new node is made the right child if the
number is greater than the one in the root.number is greater than the one in the root.
Searching for DuplicatesSearching for Duplicates
Searching for DuplicatesSearching for Duplicates
Searching for DuplicatesSearching for Duplicates
Searching for DuplicatesSearching for Duplicates
Searching for DuplicatesSearching for Duplicates
Searching for DuplicatesSearching for Duplicates
Searching for DuplicatesSearching for Duplicates
Searching for DuplicatesSearching for Duplicates
Searching for DuplicatesSearching for Duplicates
 DuplicateDuplicate (4)(4) Appeared …..Appeared …..
 DuplicateDuplicate (9),(9), (14),(14), (5),(5), Appeared …..Appeared …..
Tree TraversalsTree Traversals
1.1. PreOrderPreOrder
2.2. InOrderInOrder
3.3. PostOrderPostOrder
PreorderPreorder
 To traverse non empty binary tree inTo traverse non empty binary tree in
PreorderPreorder,, we perform the followingwe perform the following
operations.operations.
1.1. Visit theVisit the RootRoot
2.2. Traverse theTraverse the leftleft sub tree insub tree in preorderpreorder
3.3. Traverse theTraverse the rightright sub tree insub tree in preorderpreorder
InorderInorder
 To traverse a non empty binary tree inTo traverse a non empty binary tree in
Inorder (or Symmetric Order)Inorder (or Symmetric Order),, wewe
perform the following operations.perform the following operations.
1.1. Traverse theTraverse the leftleft sub tree insub tree in InorderInorder
2.2. Visit theVisit the RootRoot
3.3. Traverse theTraverse the rightright sub tree insub tree in InorderInorder
PostorderPostorder
 To traverse non empty binary tree inTo traverse non empty binary tree in
PostorderPostorder,, we perform the followingwe perform the following
operations.operations.
1.1. Traverse theTraverse the leftleft sub tree insub tree in postorderpostorder
2.2. Traverse theTraverse the rightright sub tree insub tree in postorderpostorder
3.3. Visit theVisit the RootRoot
Example of Tree TraversalsExample of Tree Traversals
AA
CC
EE
HH
FF GG
DD
II
BB
JJ
LLKK
 PreOrder : A B C E I F J D G H K LPreOrder : A B C E I F J D G H K L
 InOrder : E I C F J B G D K H L AInOrder : E I C F J B G D K H L A
 PostOrder : I E J F C G K L H D B APostOrder : I E J F C G K L H D B A

More Related Content

What's hot

BinarySearchTree-bddicken
BinarySearchTree-bddickenBinarySearchTree-bddicken
BinarySearchTree-bddickenBenjamin Dicken
 
Binary search trees
Binary search treesBinary search trees
Binary search treesDwight Sabio
 
Phylogenetics in R
Phylogenetics in RPhylogenetics in R
Phylogenetics in Rschamber
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeZafar Ayub
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating stringsNicole Ryan
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data StructureMeghaj Mallick
 
4a searching-more
4a searching-more4a searching-more
4a searching-moreShahzad Ali
 
Mathematics: Addition Then Subtraction
Mathematics: Addition Then  Subtraction Mathematics: Addition Then  Subtraction
Mathematics: Addition Then Subtraction LorenKnights
 
Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)IJERD Editor
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeManishPrajapati78
 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)M Sajid R
 

What's hot (17)

BinarySearchTree-bddicken
BinarySearchTree-bddickenBinarySearchTree-bddicken
BinarySearchTree-bddicken
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Phylogenetics in R
Phylogenetics in RPhylogenetics in R
Phylogenetics in R
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
 
binary search tree
binary search treebinary search tree
binary search tree
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Convert column-number-to-letter
Convert column-number-to-letterConvert column-number-to-letter
Convert column-number-to-letter
 
10 Red-Black Trees
10 Red-Black Trees10 Red-Black Trees
10 Red-Black Trees
 
4a searching-more
4a searching-more4a searching-more
4a searching-more
 
Mathematics: Addition Then Subtraction
Mathematics: Addition Then  Subtraction Mathematics: Addition Then  Subtraction
Mathematics: Addition Then Subtraction
 
Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)Welcome to International Journal of Engineering Research and Development (IJERD)
Welcome to International Journal of Engineering Research and Development (IJERD)
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
PyOhio Recursion Slides
PyOhio Recursion SlidesPyOhio Recursion Slides
PyOhio Recursion Slides
 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)
 
Trees
TreesTrees
Trees
 

Viewers also liked

Viewers also liked (16)

Web 2 y web 3
Web 2 y web 3 Web 2 y web 3
Web 2 y web 3
 
Links
LinksLinks
Links
 
Bullion Trading Tips & News
Bullion Trading Tips & NewsBullion Trading Tips & News
Bullion Trading Tips & News
 
Analysis of ‘Rocky Balboa’
Analysis of ‘Rocky Balboa’Analysis of ‘Rocky Balboa’
Analysis of ‘Rocky Balboa’
 
Person centred care Poll 15
Person centred care Poll 15Person centred care Poll 15
Person centred care Poll 15
 
ExperienceTRP
ExperienceTRPExperienceTRP
ExperienceTRP
 
Web 2 y web 3 1
Web 2 y web 3 1Web 2 y web 3 1
Web 2 y web 3 1
 
How to Deal in Currency Market
How to Deal in Currency MarketHow to Deal in Currency Market
How to Deal in Currency Market
 
My quotes
My quotesMy quotes
My quotes
 
Dd
DdDd
Dd
 
Designing Digital Forms
Designing Digital FormsDesigning Digital Forms
Designing Digital Forms
 
Academic_Progress_Letter_823707666_20140326_2122
Academic_Progress_Letter_823707666_20140326_2122Academic_Progress_Letter_823707666_20140326_2122
Academic_Progress_Letter_823707666_20140326_2122
 
La religion à new york
La  religion à new yorkLa  religion à new york
La religion à new york
 
Présentation Computraining
Présentation ComputrainingPrésentation Computraining
Présentation Computraining
 
States, boiling point,_melting_point,
States, boiling point,_melting_point,States, boiling point,_melting_point,
States, boiling point,_melting_point,
 
Síndrome hipotónico del lactante
Síndrome hipotónico del lactanteSíndrome hipotónico del lactante
Síndrome hipotónico del lactante
 

Similar to Binary Tree Operations and Traversals

computer notes - Binary tree
computer notes - Binary treecomputer notes - Binary tree
computer notes - Binary treeecomputernotes
 
computer notes - Binary search tree
computer notes - Binary search treecomputer notes - Binary search tree
computer notes - Binary search treeecomputernotes
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)amna izzat
 
Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1Nazir Ahmed
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptxRedHeart11
 
e computer notes - Binary search tree
e computer notes - Binary search treee computer notes - Binary search tree
e computer notes - Binary search treeecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13ecomputernotes
 
Extend your work on the OCC logo to add shapes that curve1 the blue.pdf
Extend your work on the OCC logo to add shapes that curve1 the blue.pdfExtend your work on the OCC logo to add shapes that curve1 the blue.pdf
Extend your work on the OCC logo to add shapes that curve1 the blue.pdfarihantcommunication
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanDaniyal Khan
 
Balanced binary search tree on array
Balanced binary search tree on array Balanced binary search tree on array
Balanced binary search tree on array Pier Giuliano Nioi
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms CourseHunt
 
computer notes - Data Structures - 12
computer notes - Data Structures - 12computer notes - Data Structures - 12
computer notes - Data Structures - 12ecomputernotes
 

Similar to Binary Tree Operations and Traversals (20)

6_1 (1).ppt
6_1 (1).ppt6_1 (1).ppt
6_1 (1).ppt
 
Binary trees
Binary treesBinary trees
Binary trees
 
computer notes - Binary tree
computer notes - Binary treecomputer notes - Binary tree
computer notes - Binary tree
 
computer notes - Binary search tree
computer notes - Binary search treecomputer notes - Binary search tree
computer notes - Binary search tree
 
Week 8 (trees)
Week 8 (trees)Week 8 (trees)
Week 8 (trees)
 
Data Structures
Data StructuresData Structures
Data Structures
 
Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
e computer notes - Binary search tree
e computer notes - Binary search treee computer notes - Binary search tree
e computer notes - Binary search tree
 
Unit8 C
Unit8 CUnit8 C
Unit8 C
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Extend your work on the OCC logo to add shapes that curve1 the blue.pdf
Extend your work on the OCC logo to add shapes that curve1 the blue.pdfExtend your work on the OCC logo to add shapes that curve1 the blue.pdf
Extend your work on the OCC logo to add shapes that curve1 the blue.pdf
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
LEC 5-DS ALGO(updated).pdf
LEC 5-DS  ALGO(updated).pdfLEC 5-DS  ALGO(updated).pdf
LEC 5-DS ALGO(updated).pdf
 
Tree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal KhanTree Data Structure by Daniyal Khan
Tree Data Structure by Daniyal Khan
 
Balanced binary search tree on array
Balanced binary search tree on array Balanced binary search tree on array
Balanced binary search tree on array
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
computer notes - Data Structures - 12
computer notes - Data Structures - 12computer notes - Data Structures - 12
computer notes - Data Structures - 12
 
Lecture4
Lecture4Lecture4
Lecture4
 

More from Muhammad Zubair (12)

Cloud computing
Cloud computingCloud computing
Cloud computing
 
Cloud computing disadvantages
Cloud computing disadvantagesCloud computing disadvantages
Cloud computing disadvantages
 
Lecture8
Lecture8Lecture8
Lecture8
 
Lecture7
Lecture7Lecture7
Lecture7
 
Lecture6
Lecture6Lecture6
Lecture6
 
Lecture5
Lecture5Lecture5
Lecture5
 
Lecture3
Lecture3Lecture3
Lecture3
 
Lecture3
Lecture3Lecture3
Lecture3
 
Lecture2
Lecture2Lecture2
Lecture2
 
Lecture1
Lecture1Lecture1
Lecture1
 
Indin report by zubair
Indin report by zubairIndin report by zubair
Indin report by zubair
 
Cyber crime in pakistan by zubair
Cyber crime in pakistan by zubairCyber crime in pakistan by zubair
Cyber crime in pakistan by zubair
 

Recently uploaded

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
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
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
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
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 

Recently uploaded (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
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 ...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
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
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 

Binary Tree Operations and Traversals

  • 2. Operations on Binary TreeOperations on Binary Tree  There are a number of operations that canThere are a number of operations that can be defined for a binary tree.be defined for a binary tree.  IfIf pp is pointing to a node in an existing tree thenis pointing to a node in an existing tree then  left(left(pp)) returns pointer to the left subtreereturns pointer to the left subtree  right(right(pp)) returns pointer to right subtreereturns pointer to right subtree  parent(parent(pp)) returns the father ofreturns the father of pp  brother(brother(pp)) returns brother ofreturns brother of pp..  info(p)info(p) returns content of the node.returns content of the node.
  • 3. Operations on Binary TreeOperations on Binary Tree  In order to construct a binary tree, theIn order to construct a binary tree, the following can be useful:following can be useful:  setLeft(p,x)setLeft(p,x) creates the left child node of p.creates the left child node of p. The child node contains the info ‘x’.The child node contains the info ‘x’.  setRight(p,x)setRight(p,x) creates the right child node ofcreates the right child node of p. The child node contains the info ‘x’.p. The child node contains the info ‘x’.
  • 4. Applications of Binary TreesApplications of Binary Trees  A binary tree is a useful data structure when two-A binary tree is a useful data structure when two- way decisions must be made at each point in away decisions must be made at each point in a process.process.  For example, suppose we wanted to find allFor example, suppose we wanted to find all duplicates in a list of numbers:duplicates in a list of numbers: 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 514, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
  • 5. Applications of Binary TreesApplications of Binary Trees  One way of finding duplicates is to compareOne way of finding duplicates is to compare each number with all those thateach number with all those that precedeprecede it.it. 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 514, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5 14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 514, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5
  • 6. Searching for DuplicatesSearching for Duplicates  If the list of numbers is large and is growing, thisIf the list of numbers is large and is growing, this procedure involves a large number ofprocedure involves a large number of comparisons.comparisons.  A linked list could handle the growth but theA linked list could handle the growth but the comparisons would still be large.comparisons would still be large.  The number of comparisons can be drasticallyThe number of comparisons can be drastically reduced by using a binary tree.reduced by using a binary tree.  The tree grows dynamically like the linked list.The tree grows dynamically like the linked list.
  • 7. Searching for DuplicatesSearching for Duplicates  The binary tree is built in a special way.The binary tree is built in a special way.  The first number in the list is placed in a nodeThe first number in the list is placed in a node that is designated as the root of a binary tree.that is designated as the root of a binary tree.  Initially, both left and right subtrees of the root areInitially, both left and right subtrees of the root are empty.empty.  We take the next number and compare it with theWe take the next number and compare it with the number placed in the root.number placed in the root.  If it is the same then we have a duplicate.If it is the same then we have a duplicate.
  • 8. Searching for DuplicatesSearching for Duplicates  Otherwise, we create a new tree node and put theOtherwise, we create a new tree node and put the new number in it.new number in it.  The new node is made the left child of the rootThe new node is made the left child of the root node if the second number is less than the one innode if the second number is less than the one in the root.the root.  The new node is made the right child if theThe new node is made the right child if the number is greater than the one in the root.number is greater than the one in the root.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.  DuplicateDuplicate (4)(4) Appeared …..Appeared …..
  • 23.
  • 24.
  • 25.
  • 26.  DuplicateDuplicate (9),(9), (14),(14), (5),(5), Appeared …..Appeared …..
  • 27. Tree TraversalsTree Traversals 1.1. PreOrderPreOrder 2.2. InOrderInOrder 3.3. PostOrderPostOrder
  • 28. PreorderPreorder  To traverse non empty binary tree inTo traverse non empty binary tree in PreorderPreorder,, we perform the followingwe perform the following operations.operations. 1.1. Visit theVisit the RootRoot 2.2. Traverse theTraverse the leftleft sub tree insub tree in preorderpreorder 3.3. Traverse theTraverse the rightright sub tree insub tree in preorderpreorder
  • 29. InorderInorder  To traverse a non empty binary tree inTo traverse a non empty binary tree in Inorder (or Symmetric Order)Inorder (or Symmetric Order),, wewe perform the following operations.perform the following operations. 1.1. Traverse theTraverse the leftleft sub tree insub tree in InorderInorder 2.2. Visit theVisit the RootRoot 3.3. Traverse theTraverse the rightright sub tree insub tree in InorderInorder
  • 30. PostorderPostorder  To traverse non empty binary tree inTo traverse non empty binary tree in PostorderPostorder,, we perform the followingwe perform the following operations.operations. 1.1. Traverse theTraverse the leftleft sub tree insub tree in postorderpostorder 2.2. Traverse theTraverse the rightright sub tree insub tree in postorderpostorder 3.3. Visit theVisit the RootRoot
  • 31. Example of Tree TraversalsExample of Tree Traversals AA CC EE HH FF GG DD II BB JJ LLKK  PreOrder : A B C E I F J D G H K LPreOrder : A B C E I F J D G H K L  InOrder : E I C F J B G D K H L AInOrder : E I C F J B G D K H L A  PostOrder : I E J F C G K L H D B APostOrder : I E J F C G K L H D B A