SlideShare a Scribd company logo
Binary search tree
Sana Yameen
Presented By:
Definition
• A binary search tree is a rooted binary tree, whose internal nodes
each store a key (and optionally, an associated value) and each
have two distinguished sub-trees, commonly denoted left and
right.
• The left sub-tree of a node has a key less than or equal to its
parent node's key.
• The right sub-tree of a node has a key greater than to its parent
node's key.
Representation
• BST is a collection of nodes arranged in a way where they
maintain BST properties. Each node has a key and an associated
value. While searching, the desired key is compared to the keys in
BST and if found, the associated value is retrieved.
• left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
Some BST terminologies
• The root node is the top node of tree.
• A child node has exactly one parent node and a parent node has at
most two child nodes, sibling nodes shares the same parent node.
• A leaf node has no child nodes, an interior node has at least one
child node.
• Every node in BST is a sub tree of the BST rooted at the node.
Basic Operations
• Search
• Inserting
• Deleting
• Traversing
Searching in BST
• Searching a binary search tree for a specific key can be
programmed recursively or iteratively.
• To search a given key in Binary Search Tree, we first compare it
with root, if the key is present at root, we return root. If key is
greater than root’s key, we recur for right sub tree of root node.
Otherwise we recur for left sub tree.
Algorithm of searching
If root==search-data
Return root
Else
While data not found
If search-data>node-data
Go to right sub tree
Else
Go to left sub tree
If data found
Return node
End while
Return data not found
End if
Inserting
Whenever an element is to be inserted, first locate its
proper location. Start searching from the root node, then
if the data is less than the key value, search for the empty
location in the left subtree and insert the data.
Otherwise, search for the empty location in the right sub
tree and insert the data.
Binary search tree. Adding a value
Adding a value to BST can be divided into two stages:
• Search for a place to put a new element
• Insert the new element to this place.
• A new key is always inserted at leaf. We start searching a key from
root till we hit a leaf node. Once a leaf node is found, the new
node is added as a child of the leaf node.
Algorithm of inserting
If root==null
Create root node
Return
If root exist then
If data> node
Go to right sub tree
Else
Go to left sub tree
End if
Insert data
End if
Deleting
There are three possible cases to consider:
• Deleting a node with no children: simply remove the node from
the tree.
• Deleting a node with one child: remove the node and replace it
with its child.
• Deleting a node with two children: call the node to be deleted N.
Do not delete N. Instead, choose either its in-order successor node
or its in-order predecessor node, R. Copy the value of R to N, then
recursively call delete on the original R until reaching one of the
first two cases.
Deleting a node with no children: simply remove
the node from the tree.
Deleting a node with one child: remove the
node and replace it with its child.
Deleting a node with two children:
Traversing
Traversal of a binary tree is to access every node of binary tree at
most.
Such traversals are classified by the order in which the nodes are
visited.
There are three ways of traversing:
Pre order (Root, Left, Right)
In order (Left, Root, Right)
Post order (Left, Right, Root)
Pre order traversing
In this traversal method, the root node is visited first, then the left
subtree and finally the right subtree.
Pre-order (Root, Left, Right) : F, B, A, D, C, E, G, I, H.
Algorithm of pre order
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
In order traversing
• In this traversal method, the left subtree is visited first, then the
root and later the right sub-tree. We should always remember that
every node may represent a subtree itself.
• If a binary tree is traversed in-order, the output will produce
sorted key values in an ascending order.
In-order (Left, Root, Right) : A, B, C, D, E, F, G, H, I.
Algorithm of in order
• Until all nodes are traversed −
• Step 1 − Recursively traverse left subtree.
• Step 2 − Visit root node.
• Step 3 − Recursively traverse right subtree.
Post order traversing
In this traversal method, the root node is visited last, hence the
name. First we traverse the left subtree, then the right subtree and
finally the root node.
Post-order(Left, Right, Root): A, C, E, D, B, H, I, G, F.
Algorithm of post order traversing
• Until all nodes are traversed −
• Step 1 − Recursively traverse left subtree.
• Step 2 − Recursively traverse right subtree.
• Step 3 − Visit root node.
Any Question
Binary search tree

More Related Content

What's hot

Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
leminhvuong
 

What's hot (20)

Lecture 5 trees
Lecture 5 treesLecture 5 trees
Lecture 5 trees
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Ch13 Binary Search Tree
Ch13 Binary Search TreeCh13 Binary Search Tree
Ch13 Binary Search Tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Tree
TreeTree
Tree
 
Treesandgraphs
TreesandgraphsTreesandgraphs
Treesandgraphs
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 
introduction to_trees
introduction to_treesintroduction to_trees
introduction to_trees
 
Trees
TreesTrees
Trees
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Tree
TreeTree
Tree
 
Unit – vi tree
Unit – vi   treeUnit – vi   tree
Unit – vi tree
 
Tree
TreeTree
Tree
 
Binary tree traversal ppt
Binary tree traversal pptBinary tree traversal ppt
Binary tree traversal ppt
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Tree
TreeTree
Tree
 
Trees
TreesTrees
Trees
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 

Similar to Binary search tree

B Tree, Introduction ,example,Splay tree
B Tree, Introduction ,example,Splay treeB Tree, Introduction ,example,Splay tree
B Tree, Introduction ,example,Splay tree
VikasNirgude2
 
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
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 

Similar to Binary search tree (20)

BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Unit iv data structure-converted
Unit  iv data structure-convertedUnit  iv data structure-converted
Unit iv data structure-converted
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Search tree,Tree and binary tree and heap tree
Search tree,Tree  and binary tree and heap treeSearch tree,Tree  and binary tree and heap tree
Search tree,Tree and binary tree and heap tree
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
B Tree, Introduction ,example,Splay tree
B Tree, Introduction ,example,Splay treeB Tree, Introduction ,example,Splay tree
B Tree, Introduction ,example,Splay tree
 
Binary tree
Binary treeBinary tree
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
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 
Tree
TreeTree
Tree
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
 
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
 
Creating a Binary tree from a General Tree.pptx
Creating a Binary tree from a General Tree.pptxCreating a Binary tree from a General Tree.pptx
Creating a Binary tree from a General Tree.pptx
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
Tree data structure
Tree data structureTree data structure
Tree data structure
 

Recently uploaded

一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
nscud
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Domenico Conte
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage s
MAQIB18
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 

Recently uploaded (20)

一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 
tapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive datatapal brand analysis PPT slide for comptetive data
tapal brand analysis PPT slide for comptetive data
 
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
Innovative Methods in Media and Communication Research by Sebastian Kubitschk...
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
2024-05-14 - Tableau User Group - TC24 Hot Topics - Tableau Pulse and Einstei...
 
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
一比一原版(CBU毕业证)卡普顿大学毕业证成绩单
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 
How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?How can I successfully sell my pi coins in Philippines?
How can I successfully sell my pi coins in Philippines?
 
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
Professional Data Engineer Certification Exam Guide  _  Learn  _  Google Clou...
 
Computer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage sComputer Presentation.pptx ecommerce advantage s
Computer Presentation.pptx ecommerce advantage s
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 
Criminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdfCriminal IP - Threat Hunting Webinar.pdf
Criminal IP - Threat Hunting Webinar.pdf
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 

Binary search tree

  • 1.
  • 2. Binary search tree Sana Yameen Presented By:
  • 3. Definition • A binary search tree is a rooted binary tree, whose internal nodes each store a key (and optionally, an associated value) and each have two distinguished sub-trees, commonly denoted left and right. • The left sub-tree of a node has a key less than or equal to its parent node's key. • The right sub-tree of a node has a key greater than to its parent node's key.
  • 4. Representation • BST is a collection of nodes arranged in a way where they maintain BST properties. Each node has a key and an associated value. While searching, the desired key is compared to the keys in BST and if found, the associated value is retrieved. • left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)
  • 5. Some BST terminologies • The root node is the top node of tree. • A child node has exactly one parent node and a parent node has at most two child nodes, sibling nodes shares the same parent node. • A leaf node has no child nodes, an interior node has at least one child node. • Every node in BST is a sub tree of the BST rooted at the node.
  • 6. Basic Operations • Search • Inserting • Deleting • Traversing
  • 7. Searching in BST • Searching a binary search tree for a specific key can be programmed recursively or iteratively. • To search a given key in Binary Search Tree, we first compare it with root, if the key is present at root, we return root. If key is greater than root’s key, we recur for right sub tree of root node. Otherwise we recur for left sub tree.
  • 8. Algorithm of searching If root==search-data Return root Else While data not found If search-data>node-data Go to right sub tree Else Go to left sub tree If data found Return node End while Return data not found End if
  • 9.
  • 10. Inserting Whenever an element is to be inserted, first locate its proper location. Start searching from the root node, then if the data is less than the key value, search for the empty location in the left subtree and insert the data. Otherwise, search for the empty location in the right sub tree and insert the data.
  • 11. Binary search tree. Adding a value Adding a value to BST can be divided into two stages: • Search for a place to put a new element • Insert the new element to this place. • A new key is always inserted at leaf. We start searching a key from root till we hit a leaf node. Once a leaf node is found, the new node is added as a child of the leaf node.
  • 12. Algorithm of inserting If root==null Create root node Return If root exist then If data> node Go to right sub tree Else Go to left sub tree End if Insert data End if
  • 13.
  • 14. Deleting There are three possible cases to consider: • Deleting a node with no children: simply remove the node from the tree. • Deleting a node with one child: remove the node and replace it with its child. • Deleting a node with two children: call the node to be deleted N. Do not delete N. Instead, choose either its in-order successor node or its in-order predecessor node, R. Copy the value of R to N, then recursively call delete on the original R until reaching one of the first two cases.
  • 15. Deleting a node with no children: simply remove the node from the tree.
  • 16. Deleting a node with one child: remove the node and replace it with its child.
  • 17. Deleting a node with two children:
  • 18. Traversing Traversal of a binary tree is to access every node of binary tree at most. Such traversals are classified by the order in which the nodes are visited. There are three ways of traversing: Pre order (Root, Left, Right) In order (Left, Root, Right) Post order (Left, Right, Root)
  • 19. Pre order traversing In this traversal method, the root node is visited first, then the left subtree and finally the right subtree. Pre-order (Root, Left, Right) : F, B, A, D, C, E, G, I, H.
  • 20. Algorithm of pre order Until all nodes are traversed − Step 1 − Visit root node. Step 2 − Recursively traverse left subtree. Step 3 − Recursively traverse right subtree.
  • 21. In order traversing • In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We should always remember that every node may represent a subtree itself. • If a binary tree is traversed in-order, the output will produce sorted key values in an ascending order. In-order (Left, Root, Right) : A, B, C, D, E, F, G, H, I.
  • 22. Algorithm of in order • Until all nodes are traversed − • Step 1 − Recursively traverse left subtree. • Step 2 − Visit root node. • Step 3 − Recursively traverse right subtree.
  • 23. Post order traversing In this traversal method, the root node is visited last, hence the name. First we traverse the left subtree, then the right subtree and finally the root node. Post-order(Left, Right, Root): A, C, E, D, B, H, I, G, F.
  • 24. Algorithm of post order traversing • Until all nodes are traversed − • Step 1 − Recursively traverse left subtree. • Step 2 − Recursively traverse right subtree. • Step 3 − Visit root node.