SlideShare uma empresa Scribd logo
1 de 32
Binary tree
Group Members
Maria Saleem Mcsm-F15-91
Anisa Zia Mcsm-F15-07
What is tree
Terminologies of tree
Edge and cycle
Principle of tree
Binary tree and binary search tree
Construction of binary search tree from given
array
Insertion and deletion of node from BST
Algorithm for insertion and deletion in BST
applications of binary tree
Outlines
Linear data structure
Data
structure
Linear data
structure
Array &
linkedlist Stack & queue
Non-linear
data structure
Tree & graph
Data Structure flow chart
Tree
Tree is data structure that is non linear and can
be used to represents data in hierarchy
between those elements. For example:
organization structure, family tree, and the
tournament.
Terminology of Tree
• node : each element of tree
• Edges: lines connecting the nodes are called edges
• Root : has no parent, top most element
• leaf : has no child also called external node
• Parent if any node has child
• Sibling nodes that share same parent
Components of Tree
A
B C D
E F G
Root(head)
Leaf
Level
0
1
2
Left Subtree
Node/Vertex
Edge/Link
Terminology of Tree
• Degree number of child of particular node is called degree
• Of that node.
• level : level of a node is distance of that node from root.
• depth of tree: the number of edges from the root to node
in a path
height of tree: total number edges from the node to the
the deepest leaf.
• Ancestor :Total no of parents and grand parents for specific
node
• Descendant :no of child and grand child in same path
 Every child must be connected with its parent
node
 Disconnection are not allowed in binary tree
 Every node must have only single parent
 If any node have more than one parent than it
creates cycle
Principle of tree
cycle
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
Cont…
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 child Address Left Info Right Right child Address
Linked Representation using linkedlist
Ptr -> Info = Data
Ptr -> Left = Left Child
Ptr -> Right = Right Child
A
B C
11
32
D E
54
F G
76
E.g.
Left Info Right
Binary tree Representation using linkedlist
 Tree is store in single array
From General Tree (Program)
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.
Making of Binary TreeConstruction of BST
Eg. 30, 26, 35, 9, 12, 19, 32, 40, 50, 45, 2, 9
30
26 35
9 12 32 40
45 50
192 9
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
23
4418
5220 35
12
E.g. if we have to search 12 than go to left sub
tree
Binary SEARCH TREE (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 SEARCH TREE (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 access every node of binary
tree at most once
• Tree traversal ways
a) Pre0rder
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 12 973 8
Preorder
start
Left sub
tree
Right sub
tree
1. Visit left sub-tree
2. Visit root
3. Visit right sub-tree
5
2 1
3 8
97
2 1 3 5 7 9 8
Inorder
2 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 71 3 9 8 5
Postorder
start
When we delete a node, we need to consider
how we take care of the children of the deleted
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 1 or 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

Mais conteúdo relacionado

Mais procurados

Introduction to Data Structure
Introduction to Data StructureIntroduction to Data Structure
Introduction to Data Structure
Jazz Jinia Bhowmik
 

Mais procurados (20)

Tree
TreeTree
Tree
 
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
 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)
 
Binary trees
Binary treesBinary trees
Binary trees
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
 
Binary tree and operations
Binary tree and operations Binary tree and operations
Binary tree and operations
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
BinarySearchTree-bddicken
BinarySearchTree-bddickenBinarySearchTree-bddicken
BinarySearchTree-bddicken
 
Introduction to Data Structure
Introduction to Data StructureIntroduction to Data Structure
Introduction to Data Structure
 
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
 
Data Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceData Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer Science
 
Tree
TreeTree
Tree
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 trees
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
08 B Trees
08 B Trees08 B Trees
08 B Trees
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
linked_lists4
linked_lists4linked_lists4
linked_lists4
 
Unit 4.1 (tree)
Unit 4.1 (tree)Unit 4.1 (tree)
Unit 4.1 (tree)
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
 

Semelhante a Binary tree

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
 
Final tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentationFinal tree.ppt tells about tree presentation
Final tree.ppt tells about tree presentation
nakulvarshney371
 

Semelhante a Binary tree (20)

Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
 
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
 
Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
TREES.pptx
TREES.pptxTREES.pptx
TREES.pptx
 
Tree
TreeTree
Tree
 
Mca admission in india
Mca admission in indiaMca admission in india
Mca admission in india
 
Tree
TreeTree
Tree
 
Trees in Data Structure
Trees in Data StructureTrees in Data Structure
Trees in Data Structure
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
DAA PPT.pptx
DAA PPT.pptxDAA PPT.pptx
DAA PPT.pptx
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
B+ trees and height balance tree
B+ trees and height balance treeB+ trees and height balance tree
B+ trees and height balance tree
 
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
 
L 19 ct1120
L 19 ct1120L 19 ct1120
L 19 ct1120
 
Binary tree
Binary treeBinary tree
Binary tree
 

Último

₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
Diya Sharma
 
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
nilamkumrai
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 

Último (20)

Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
VVIP Pune Call Girls Sinhagad WhatSapp Number 8005736733 With Elite Staff And...
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts ServiceReal Escorts in Al Nahda +971524965298 Dubai Escorts Service
Real Escorts in Al Nahda +971524965298 Dubai Escorts Service
 
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
 
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
VVIP Pune Call Girls Mohammadwadi WhatSapp Number 8005736733 With Elite Staff...
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
( Pune ) VIP Baner Call Girls 🎗️ 9352988975 Sizzling | Escorts | Girls Are Re...
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Himatnagar 7001035870 Whatsapp Number, 24/07 Booking
 
Moving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providersMoving Beyond Twitter/X and Facebook - Social Media for local news providers
Moving Beyond Twitter/X and Facebook - Social Media for local news providers
 
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...Russian Call Girls Pune  (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
Russian Call Girls Pune (Adult Only) 8005736733 Escort Service 24x7 Cash Pay...
 
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
 
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
 

Binary tree

  • 1.
  • 3. Group Members Maria Saleem Mcsm-F15-91 Anisa Zia Mcsm-F15-07
  • 4. What is tree Terminologies of tree Edge and cycle Principle of tree Binary tree and binary search tree Construction of binary search tree from given array Insertion and deletion of node from BST Algorithm for insertion and deletion in BST applications of binary tree Outlines
  • 5. Linear data structure Data structure Linear data structure Array & linkedlist Stack & queue Non-linear data structure Tree & graph Data Structure flow chart
  • 6. Tree Tree is data structure that is non linear and can be used to represents data in hierarchy between those elements. For example: organization structure, family tree, and the tournament.
  • 7. Terminology of Tree • node : each element of tree • Edges: lines connecting the nodes are called edges • Root : has no parent, top most element • leaf : has no child also called external node • Parent if any node has child • Sibling nodes that share same parent
  • 8. Components of Tree A B C D E F G Root(head) Leaf Level 0 1 2 Left Subtree Node/Vertex Edge/Link
  • 9. Terminology of Tree • Degree number of child of particular node is called degree • Of that node. • level : level of a node is distance of that node from root. • depth of tree: the number of edges from the root to node in a path height of tree: total number edges from the node to the the deepest leaf. • Ancestor :Total no of parents and grand parents for specific node • Descendant :no of child and grand child in same path
  • 10.  Every child must be connected with its parent node  Disconnection are not allowed in binary tree  Every node must have only single parent  If any node have more than one parent than it creates cycle Principle of tree cycle
  • 11. 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
  • 12. Binary Tree A B G C D E F Left Child Right Child Root Parent Left subtree
  • 13. Cont… 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 child Address Left Info Right Right child Address
  • 14. Linked Representation using linkedlist Ptr -> Info = Data Ptr -> Left = Left Child Ptr -> Right = Right Child A B C 11 32 D E 54 F G 76 E.g. Left Info Right Binary tree Representation using linkedlist  Tree is store in single array
  • 15. From General Tree (Program) Binary Tree (Linked List) A B D E F C I G H Head Binary Tree A B H CD E F GI
  • 16. 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.
  • 17. 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
  • 18. 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.
  • 19. Making of Binary TreeConstruction of BST Eg. 30, 26, 35, 9, 12, 19, 32, 40, 50, 45, 2, 9 30 26 35 9 12 32 40 45 50 192 9
  • 20. 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
  • 21. Example of searching in BST 23 4418 5220 35 12 E.g. if we have to search 12 than go to left sub tree
  • 22. Binary SEARCH TREE (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
  • 23. Example of insertion in BST 23 4418 5220 35 12 23 4418 5220 35 12 Before insertion 19 After insertion
  • 24. Binary SEARCH TREE (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
  • 25.  Traversal of a binary tree is to access every node of binary tree at most once • Tree traversal ways a) Pre0rder b) In Order c) Post Order BST Traversal
  • 26. 1. start 2. Visit root 3. Visit left sub-tree 4. Visit right sub-tree 5. stop 5 2 1 3 8 97 5 12 973 8 Preorder start Left sub tree Right sub tree
  • 27. 1. Visit left sub-tree 2. Visit root 3. Visit right sub-tree 5 2 1 3 8 97 2 1 3 5 7 9 8 Inorder 2 1 3 8 97 Right sub tree
  • 28. 1. Visit left sub-tree 2. Visit right sub-tree 3. Visit root 5 2 1 3 8 97 2 71 3 9 8 5 Postorder start
  • 29. When we delete a node, we need to consider how we take care of the children of the deleted node. Delete Node
  • 30. 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…..
  • 31. 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 1 or 2.
  • 32. 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