SlideShare ist ein Scribd-Unternehmen logo
1 von 23
PRESENTED BY:
MEGHAJ KUMAR MALLICK
MCA 1ST YEAR ,2ND SEMESTER
(MCA/25017/18)
Binarytree
Binary tree
A binary tree is simply a tree in which each node can have at
most two children. It may be 0, 1 or 2.
Root node
left sub tree
Right sub tree
Binary Tree
Binary Tree
A
B
G
C
D E F
Left Child Right Child
Root
Parent
Left
subtree
CONTINUED
Left Child: The node on the left of any node is called left child.
Right child: The node on the right of any node is called right
child.
Left Sub tree: sub tree attached to left side of root node is called
left sub tree.
Right Sub tree: sub tree attached to right side of root node is
called right sub tree.
The node of a binary tree is divided into three parts :
Left childAddress Left Info Right Right child Address
Linked Representation
Ptr -> Info = Data
Ptr -> Left = Left Child
Ptr -> Right = Right Child
A
B C
11
32
D E
54
F G
76
Left Info Right
Binary tree Representation using Linked List
y
From General Tree
Binary Tree
(Linked List)
A
B
D
E F
C
I G
H
Head
Binary Tree
A
B
H
CD
E F
GI
Types of Binary Tree
Full Binary Tree Complete Binary Tree
A
B
G
C
D E F
A
B C
D E
• All nodes (except leaf)
have two children.
• Each subtree has same
length of path.
• All nodes (except leaf)
have two children.
• Each subtree can has
different length of path.
Binary search tree
A binary Search Tree is special kind if tree that
satisfied the following conditions
• If value of inserted node is bigger than parent
then it will be right subtree.
• If value of inserted node is smaller than parent
then it will be left subtree.
• This tree is known as binary search tree or
ordered binary tree.
• It used for searching
Construction of binary Search Tree (BST)
1. Construction of binary Search Tree (BST)
Step.1: Initially tree is empty ,place the first element at the root.
Step.2: Compare the next element with root
if element is grater than or equal to root then place it in
right child position.
else
place it in the left child position.
Step.3: Repeat the process (step 2) for the next element until
end of elements or nodes.
ALGORITHM FOR SEARCHING IN BST
Algorithm :
Search BST(key,targetkey)
1) START
2) If root==NULL
3) Return value not found
4) End if
5) If(target key<root)
6) Return searchBST (leftsubtree, targetkey)
7) else if(targetkey > root)
8) Return searchBST (rightsubtree, targetkey)
9) ELSE
10)FOUND target key
11)Return root
12)End if
13)End SearchBST
Example of searching in BST
5220 35
12
E.g. if we have to search 12 than go to left sub
tree
23
18 44
Binary SEARCHTREE(INSERTION)
• For insert data in
binary tree two conditions we have
to faced
1. IF tree is empty than insert to a new node make them root node.
2. If tree is not empty than all inserts take place at a leaf or at a
• leaf like node (a tree that has only one NULL subtree
Example of insertion in BST
23
4418
5220 35
12
23
4418
5220 35
12
Before insertion
19
After insertion
Binary SEARCHTREE(INSERTION)
Algorithm
1. START
2. If root==NULL
3. Root=newnode
4. If (newnode<root)
5. Return addBST(leftsubtree,newnode)
6. ELSE
7. Return addBST(rightsubtree,newnode)
8. End if
9. End addBST
 Traversal of a binary tree is to accessevery node of binary
tree at most once
• Tree traversal ways
a) Pre 0rder
b) In Order
c) Post Order
BST Traversal
1. start
2. Visit root
3. Visit left sub-tree
4. Visit right sub-tree
5. stop
5
2 1
3 8
97
5 2 1 3 7 9 8
Preorder
start
Left sub
tree
Right sub
tree
1. Visit left sub-tree
2. Visit root
3. Visit right sub-tree
5
2
2 1 3 5 7 9 8
Inorder
1
3 8
97
Right sub
tree
1. Visit left sub-tree
2. Visit right sub-tree
3. Visit root
5
2 1
3 8
97
2 1 3 7 9 8 5
Postorder
start
When we delete a node, we need to consider
how we take care of the children of thedeleted
node.
DELETE NODE
Three cases:
(1) the node is a leaf
 Delete it immediately
(2) the node has one child
 Adjust a pointer from the parent to bypass that node
Delete Cont…..
3)the node has 2 children
 replace the key of that node with the
minimum element at the right sub tree
 delete the minimum element
 Has either no child or only right child because
if it has a left child, that left child would be
smaller and would have been chosen. So
invoke case 1or 2.
Binary tree Applications
 File System(storing naturally hierarchical
data)
 Organize data(searching,insertion,deletion
Binary search tree used for this purpose
 Telephone dictionary
 Arithmetic operations
 Network routing etc
Binary Tree in Data Structure

Weitere ähnliche Inhalte

Was ist angesagt?

Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
Jsaddam Hussain
 

Was ist angesagt? (20)

Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Linked List
Linked ListLinked List
Linked List
 
Binary search
Binary searchBinary search
Binary search
 
Heaps
HeapsHeaps
Heaps
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Linked list implementation of Queue
Linked list implementation of QueueLinked list implementation of Queue
Linked list implementation of Queue
 
Expression trees
Expression treesExpression trees
Expression trees
 
Linked lists
Linked listsLinked lists
Linked lists
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 

Ähnlich wie Binary Tree in Data Structure

TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
asimshahzad8611
 
presentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingpresentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashing
Bindiya syed
 

Ähnlich wie Binary Tree in Data Structure (20)

Binary tree
Binary treeBinary tree
Binary tree
 
Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
 
Tree
TreeTree
Tree
 
Tree
TreeTree
Tree
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Materi Searching
Materi Searching Materi Searching
Materi Searching
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
 
Binary search trees (1)
Binary search trees (1)Binary search trees (1)
Binary search trees (1)
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Binary tree
Binary treeBinary tree
Binary tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
DAA PPT.pptx
DAA PPT.pptxDAA PPT.pptx
DAA PPT.pptx
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
L 17 ct1120
L 17 ct1120L 17 ct1120
L 17 ct1120
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
presentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingpresentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashing
 
Trees
TreesTrees
Trees
 

Mehr von Meghaj Mallick

Mehr von Meghaj Mallick (20)

24 partial-orderings
24 partial-orderings24 partial-orderings
24 partial-orderings
 
PORTFOLIO BY USING HTML & CSS
PORTFOLIO BY USING HTML & CSSPORTFOLIO BY USING HTML & CSS
PORTFOLIO BY USING HTML & CSS
 
Introduction to Software Testing
Introduction to Software TestingIntroduction to Software Testing
Introduction to Software Testing
 
Introduction to System Programming
Introduction to System ProgrammingIntroduction to System Programming
Introduction to System Programming
 
MACRO ASSEBLER
MACRO ASSEBLERMACRO ASSEBLER
MACRO ASSEBLER
 
Icons, Image & Multimedia
Icons, Image & MultimediaIcons, Image & Multimedia
Icons, Image & Multimedia
 
Project Tracking & SPC
Project Tracking & SPCProject Tracking & SPC
Project Tracking & SPC
 
Peephole Optimization
Peephole OptimizationPeephole Optimization
Peephole Optimization
 
Routing in MANET
Routing in MANETRouting in MANET
Routing in MANET
 
Macro assembler
 Macro assembler Macro assembler
Macro assembler
 
Architecture and security in Vanet PPT
Architecture and security in Vanet PPTArchitecture and security in Vanet PPT
Architecture and security in Vanet PPT
 
Design Model & User Interface Design in Software Engineering
Design Model & User Interface Design in Software EngineeringDesign Model & User Interface Design in Software Engineering
Design Model & User Interface Design in Software Engineering
 
Text Mining of Twitter in Data Mining
Text Mining of Twitter in Data MiningText Mining of Twitter in Data Mining
Text Mining of Twitter in Data Mining
 
DFS & BFS in Computer Algorithm
DFS & BFS in Computer AlgorithmDFS & BFS in Computer Algorithm
DFS & BFS in Computer Algorithm
 
Software Development Method
Software Development MethodSoftware Development Method
Software Development Method
 
Secant method in Numerical & Statistical Method
Secant method in Numerical & Statistical MethodSecant method in Numerical & Statistical Method
Secant method in Numerical & Statistical Method
 
Motivation in Organization
Motivation in OrganizationMotivation in Organization
Motivation in Organization
 
Communication Skill
Communication SkillCommunication Skill
Communication Skill
 
Partial-Orderings in Discrete Mathematics
 Partial-Orderings in Discrete Mathematics Partial-Orderings in Discrete Mathematics
Partial-Orderings in Discrete Mathematics
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 

Kürzlich hochgeladen

Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
raffaeleoman
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
Sheetaleventcompany
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
Kayode Fayemi
 

Kürzlich hochgeladen (20)

Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
 
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, YardstickSaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
 
Dreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video TreatmentDreaming Marissa Sánchez Music Video Treatment
Dreaming Marissa Sánchez Music Video Treatment
 
If this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New NigeriaIf this Giant Must Walk: A Manifesto for a New Nigeria
If this Giant Must Walk: A Manifesto for a New Nigeria
 
lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.lONG QUESTION ANSWER PAKISTAN STUDIES10.
lONG QUESTION ANSWER PAKISTAN STUDIES10.
 
Presentation on Engagement in Book Clubs
Presentation on Engagement in Book ClubsPresentation on Engagement in Book Clubs
Presentation on Engagement in Book Clubs
 
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdfThe workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
The workplace ecosystem of the future 24.4.2024 Fabritius_share ii.pdf
 
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara ServicesVVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
 
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
Busty Desi⚡Call Girls in Sector 51 Noida Escorts >༒8448380779 Escort Service-...
 
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 97 Noida Escorts >༒8448380779 Escort Service
 
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
 
Air breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animalsAir breathing and respiratory adaptations in diver animals
Air breathing and respiratory adaptations in diver animals
 
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdfAWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
AWS Data Engineer Associate (DEA-C01) Exam Dumps 2024.pdf
 
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
 
Causes of poverty in France presentation.pptx
Causes of poverty in France presentation.pptxCauses of poverty in France presentation.pptx
Causes of poverty in France presentation.pptx
 
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night EnjoyCall Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
 
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
 
Dreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio IIIDreaming Music Video Treatment _ Project & Portfolio III
Dreaming Music Video Treatment _ Project & Portfolio III
 
Report Writing Webinar Training
Report Writing Webinar TrainingReport Writing Webinar Training
Report Writing Webinar Training
 

Binary Tree in Data Structure

  • 1. PRESENTED BY: MEGHAJ KUMAR MALLICK MCA 1ST YEAR ,2ND SEMESTER (MCA/25017/18) Binarytree
  • 2. Binary tree A binary tree is simply a tree in which each node can have at most two children. It may be 0, 1 or 2. Root node left sub tree Right sub tree Binary Tree
  • 3. Binary Tree A B G C D E F Left Child Right Child Root Parent Left subtree
  • 4. CONTINUED Left Child: The node on the left of any node is called left child. Right child: The node on the right of any node is called right child. Left Sub tree: sub tree attached to left side of root node is called left sub tree. Right Sub tree: sub tree attached to right side of root node is called right sub tree. The node of a binary tree is divided into three parts : Left childAddress Left Info Right Right child Address
  • 5. Linked Representation Ptr -> Info = Data Ptr -> Left = Left Child Ptr -> Right = Right Child A B C 11 32 D E 54 F G 76 Left Info Right Binary tree Representation using Linked List y
  • 6. From General Tree Binary Tree (Linked List) A B D E F C I G H Head Binary Tree A B H CD E F GI
  • 7. Types of Binary Tree Full Binary Tree Complete Binary Tree A B G C D E F A B C D E • All nodes (except leaf) have two children. • Each subtree has same length of path. • All nodes (except leaf) have two children. • Each subtree can has different length of path.
  • 8. Binary search tree A binary Search Tree is special kind if tree that satisfied the following conditions • If value of inserted node is bigger than parent then it will be right subtree. • If value of inserted node is smaller than parent then it will be left subtree. • This tree is known as binary search tree or ordered binary tree. • It used for searching
  • 9. Construction of binary Search Tree (BST) 1. Construction of binary Search Tree (BST) Step.1: Initially tree is empty ,place the first element at the root. Step.2: Compare the next element with root if element is grater than or equal to root then place it in right child position. else place it in the left child position. Step.3: Repeat the process (step 2) for the next element until end of elements or nodes.
  • 10. ALGORITHM FOR SEARCHING IN BST Algorithm : Search BST(key,targetkey) 1) START 2) If root==NULL 3) Return value not found 4) End if 5) If(target key<root) 6) Return searchBST (leftsubtree, targetkey) 7) else if(targetkey > root) 8) Return searchBST (rightsubtree, targetkey) 9) ELSE 10)FOUND target key 11)Return root 12)End if 13)End SearchBST
  • 11. Example of searching in BST 5220 35 12 E.g. if we have to search 12 than go to left sub tree 23 18 44
  • 12. Binary SEARCHTREE(INSERTION) • For insert data in binary tree two conditions we have to faced 1. IF tree is empty than insert to a new node make them root node. 2. If tree is not empty than all inserts take place at a leaf or at a • leaf like node (a tree that has only one NULL subtree
  • 13. Example of insertion in BST 23 4418 5220 35 12 23 4418 5220 35 12 Before insertion 19 After insertion
  • 14. Binary SEARCHTREE(INSERTION) Algorithm 1. START 2. If root==NULL 3. Root=newnode 4. If (newnode<root) 5. Return addBST(leftsubtree,newnode) 6. ELSE 7. Return addBST(rightsubtree,newnode) 8. End if 9. End addBST
  • 15.  Traversal of a binary tree is to accessevery node of binary tree at most once • Tree traversal ways a) Pre 0rder b) In Order c) Post Order BST Traversal
  • 16. 1. start 2. Visit root 3. Visit left sub-tree 4. Visit right sub-tree 5. stop 5 2 1 3 8 97 5 2 1 3 7 9 8 Preorder start Left sub tree Right sub tree
  • 17. 1. Visit left sub-tree 2. Visit root 3. Visit right sub-tree 5 2 2 1 3 5 7 9 8 Inorder 1 3 8 97 Right sub tree
  • 18. 1. Visit left sub-tree 2. Visit right sub-tree 3. Visit root 5 2 1 3 8 97 2 1 3 7 9 8 5 Postorder start
  • 19. When we delete a node, we need to consider how we take care of the children of thedeleted node. DELETE NODE
  • 20. Three cases: (1) the node is a leaf  Delete it immediately (2) the node has one child  Adjust a pointer from the parent to bypass that node Delete Cont…..
  • 21. 3)the node has 2 children  replace the key of that node with the minimum element at the right sub tree  delete the minimum element  Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1or 2.
  • 22. Binary tree Applications  File System(storing naturally hierarchical data)  Organize data(searching,insertion,deletion Binary search tree used for this purpose  Telephone dictionary  Arithmetic operations  Network routing etc