SlideShare a Scribd company logo
BINARY SEARCH TREES
Technical Writing (CS 1305) Assignment
Anya Chaturvedi
20146005
August 31, 2015 1 / 29
What is a Binary Search Tree?
PROPERTY 1:
Each node can have upto two successor nodes.
August 31, 2015 2 / 29
What is a Binary Search Tree?
PROPERTY 2:
A unique path exists from the root to every node.
This is not valid binary tree!
August 31, 2015 3 / 29
Some Basic Information
The successor nodes of a node are called its children.
The predecessor node of a node is called its parent.
The first node is called the root.
A node without a child is called a leaf.
Nodes with the same parent are called siblings.
August 31, 2015 4 / 29
Basic Information(Cont.)
Nodes are organized in levels.indexed from 0.
Level(or depth) of a node is the number of edges in the path
from the root to that node.
Height of a tree (h):No. of levels (L)
Full tree:Every node has exactly two children and all the leaves
are on the same level.
Complete tree:A complete binary tree is a binary tree in which
every level, except possibly the last, is completely filled, and all
nodes are as far left as possible.
Full Tree – Complete Tree
August 31, 2015 5 / 29
Binary Search Tree Property
-
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
tree additionally satisfies thebinary search tree property, which
states that the key in each node must be greater than all keys
stored in the left sub-tree, and smaller than all keys in right
sub-tree
August 31, 2015 6 / 29
Max No.of Nodes
Max no. of nodes at a level l is 2l
.
Total no. of nodes N of a full tree with height h = 2h
− 1.
August 31, 2015 7 / 29
Height h AND It’s Importance
Since, N = 2h
− 1
=> 2h
= N + 1
=> h = log(N + 1)
Hence, h -> O(log(N))
Tree operations (e.g., insert, delete, retrieve etc.)are typically
expressed in terms of h.
So, h determines running time!
August 31, 2015 8 / 29
Search In a Binary Tree
Start at the root
Search the tree level by level, until you find the element you are
searching for or you reach a leaf.
Is this better than searching a linked list?
NO! -> O(N)
August 31, 2015 9 / 29
Search In A Binary Search Tree
1 Start at the root
2 Compare the value of the item you are searching for with the
value stored at the root
3 If the values are equal, then item found; otherwise, if it is a leaf
node, then not found
4 If it is less than the value stored at the root, then search the left
subtree
5 If it is greater than the value stored at the root, then search the
right subtree
6 Repeat steps 2-6 for the root of the subtree chosen in the
previous step 4 or 5.
August 31, 2015 10 / 29
Search in BST(Cont.)
Is this better than searching a linked list?
YES-> O(log(N))
August 31, 2015 11 / 29
Function for No. of Nodes
Recursive implementation
No. of nodes in a tree
= No. of nodes in left subtree + No. of nodes in right
subtree+1
What is the size factor?
Number of nodes in the tree we are examining
What is the base case?
The tree is empty
What is the general case?
CountNodes(Left(tree)) + CountNodes(Right(tree)) +1
August 31, 2015 12 / 29
Function RetrieveItem()
What is the size of the problem?
Number of nodes in the tree we are examining
What is the base case(s)?
1 When the key is found
2 The tree is empty (key was not found)
What is the general case?
Search in the left or right subtrees
August 31, 2015 13 / 29
RetrieveItem() (Cont.)
O(h)
August 31, 2015 14 / 29
Function InsertItem()
What is the size of the problem?
Number of nodes in the tree we are examining
What is the base case(s)?
The tree is empty
What is the general case?
Choose the left or right subtree
August 31, 2015 15 / 29
InsertItem() (Cont.)
Inserting 11 in the given BST
O(h)
August 31, 2015 16 / 29
Function DeleteItem()
First, find the item.
Then, delete it.
Binary search tree property must be preserved!!
We need to consider three different cases
1 Deleting a leaf
2 Deleting a node with only one child
3 Deleting a node with two children
August 31, 2015 17 / 29
Deleting a Leaf
August 31, 2015 18 / 29
Deleting a node with only one child
August 31, 2015 19 / 29
Deleting a node with two children
August 31, 2015 20 / 29
Deleting a node with two children(Cont.)
Find predecessor (i.e., rightmost node in the left subtree)
Replace the data of the node to be deleted with predecessor’s
data
Delete predecessor node
August 31, 2015 21 / 29
Function DeleteItem()
What is the size of the problem?
Number of nodes in the tree we are examining
What is the base case(s)?
Key to be deleted was found
What is the general case?
Choose the left or right subtree
August 31, 2015 22 / 29
Traversals
There are mainly three ways to traverse a tree:
1 Inorder Traversal
2 Postorder Traversal
3 Preorder Traversal
August 31, 2015 23 / 29
Traversal Example
August 31, 2015 24 / 29
Print the Tree
For printing the Binary Search Tree in sorted order we can use
Inorder Traversal.
Result: A D J M Q R T
August 31, 2015 25 / 29
Use Of A Binary Search Tree
In comparison to other trees using a Binary Search Tree we can
efficiently perform the following:
Search for a particular element.
Sorting of elements.
Find Maximum/Minimum.
Delete a particular element.
August 31, 2015 26 / 29
REFRENCES
Images:
www.google.com
Text:
www.wikipedia.com
www.google.com
tex.stackexchange.com
August 31, 2015 27 / 29
August 31, 2015 28 / 29
August 31, 2015 29 / 29

More Related Content

What's hot

Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 

What's hot (20)

Expression trees
Expression treesExpression trees
Expression trees
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
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
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Binary tree traversal ppt - 02.03.2020
Binary tree traversal   ppt - 02.03.2020Binary tree traversal   ppt - 02.03.2020
Binary tree traversal ppt - 02.03.2020
 
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
 
BinarySearchTree-bddicken
BinarySearchTree-bddickenBinarySearchTree-bddicken
BinarySearchTree-bddicken
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Linear Search
Linear SearchLinear Search
Linear Search
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
Binary Search Tree (BST)
Binary Search Tree (BST)Binary Search Tree (BST)
Binary Search Tree (BST)
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
B+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletionB+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletion
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Singly linked list
Singly linked listSingly linked list
Singly linked list
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
UNIT-4 TREES.ppt
UNIT-4 TREES.pptUNIT-4 TREES.ppt
UNIT-4 TREES.ppt
 

Similar to Binary Search Tree

Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptx
AhmedEldesoky24
 

Similar to Binary Search Tree (20)

data structures module III & IV.pptx
data structures module III & IV.pptxdata structures module III & IV.pptx
data structures module III & IV.pptx
 
mitochondria moment and super computer integration.ppt
mitochondria moment and super computer integration.pptmitochondria moment and super computer integration.ppt
mitochondria moment and super computer integration.ppt
 
bst.ppt
bst.pptbst.ppt
bst.ppt
 
Lecture4b dynamic data_structure
Lecture4b dynamic data_structureLecture4b dynamic data_structure
Lecture4b dynamic data_structure
 
Best for b trees
Best for b treesBest for b trees
Best for b trees
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
unit06-3-Trees.pdf
unit06-3-Trees.pdfunit06-3-Trees.pdf
unit06-3-Trees.pdf
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
Tree
TreeTree
Tree
 
Trees
TreesTrees
Trees
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
08 B Trees
08 B Trees08 B Trees
08 B Trees
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Trees
TreesTrees
Trees
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Bao cao
Bao caoBao cao
Bao cao
 
Data structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptxData structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptx
 
Data structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptxData structures and Algorithm analysis_Lecture4.pptx
Data structures and Algorithm analysis_Lecture4.pptx
 

More from Shivam Singh (7)

Stacks
StacksStacks
Stacks
 
Trees
TreesTrees
Trees
 
What If Microsoft sold diapers!? MS Diapers!
What If Microsoft sold diapers!? MS Diapers!What If Microsoft sold diapers!? MS Diapers!
What If Microsoft sold diapers!? MS Diapers!
 
Search Algprithms
Search AlgprithmsSearch Algprithms
Search Algprithms
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
Linked List
Linked ListLinked List
Linked List
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 

Recently uploaded

Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
Kamal Acharya
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 

Recently uploaded (20)

Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
Laundry management system project report.pdf
Laundry management system project report.pdfLaundry management system project report.pdf
Laundry management system project report.pdf
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and ClusteringKIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
fundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projectionfundamentals of drawing and isometric and orthographic projection
fundamentals of drawing and isometric and orthographic projection
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
Halogenation process of chemical process industries
Halogenation process of chemical process industriesHalogenation process of chemical process industries
Halogenation process of chemical process industries
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
Natalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in KrakówNatalia Rutkowska - BIM School Course in Kraków
Natalia Rutkowska - BIM School Course in Kraków
 
Construction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptxConstruction method of steel structure space frame .pptx
Construction method of steel structure space frame .pptx
 

Binary Search Tree

  • 1. BINARY SEARCH TREES Technical Writing (CS 1305) Assignment Anya Chaturvedi 20146005 August 31, 2015 1 / 29
  • 2. What is a Binary Search Tree? PROPERTY 1: Each node can have upto two successor nodes. August 31, 2015 2 / 29
  • 3. What is a Binary Search Tree? PROPERTY 2: A unique path exists from the root to every node. This is not valid binary tree! August 31, 2015 3 / 29
  • 4. Some Basic Information The successor nodes of a node are called its children. The predecessor node of a node is called its parent. The first node is called the root. A node without a child is called a leaf. Nodes with the same parent are called siblings. August 31, 2015 4 / 29
  • 5. Basic Information(Cont.) Nodes are organized in levels.indexed from 0. Level(or depth) of a node is the number of edges in the path from the root to that node. Height of a tree (h):No. of levels (L) Full tree:Every node has exactly two children and all the leaves are on the same level. Complete tree:A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. Full Tree – Complete Tree August 31, 2015 5 / 29
  • 6. Binary Search Tree Property - 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 tree additionally satisfies thebinary search tree property, which states that the key in each node must be greater than all keys stored in the left sub-tree, and smaller than all keys in right sub-tree August 31, 2015 6 / 29
  • 7. Max No.of Nodes Max no. of nodes at a level l is 2l . Total no. of nodes N of a full tree with height h = 2h − 1. August 31, 2015 7 / 29
  • 8. Height h AND It’s Importance Since, N = 2h − 1 => 2h = N + 1 => h = log(N + 1) Hence, h -> O(log(N)) Tree operations (e.g., insert, delete, retrieve etc.)are typically expressed in terms of h. So, h determines running time! August 31, 2015 8 / 29
  • 9. Search In a Binary Tree Start at the root Search the tree level by level, until you find the element you are searching for or you reach a leaf. Is this better than searching a linked list? NO! -> O(N) August 31, 2015 9 / 29
  • 10. Search In A Binary Search Tree 1 Start at the root 2 Compare the value of the item you are searching for with the value stored at the root 3 If the values are equal, then item found; otherwise, if it is a leaf node, then not found 4 If it is less than the value stored at the root, then search the left subtree 5 If it is greater than the value stored at the root, then search the right subtree 6 Repeat steps 2-6 for the root of the subtree chosen in the previous step 4 or 5. August 31, 2015 10 / 29
  • 11. Search in BST(Cont.) Is this better than searching a linked list? YES-> O(log(N)) August 31, 2015 11 / 29
  • 12. Function for No. of Nodes Recursive implementation No. of nodes in a tree = No. of nodes in left subtree + No. of nodes in right subtree+1 What is the size factor? Number of nodes in the tree we are examining What is the base case? The tree is empty What is the general case? CountNodes(Left(tree)) + CountNodes(Right(tree)) +1 August 31, 2015 12 / 29
  • 13. Function RetrieveItem() What is the size of the problem? Number of nodes in the tree we are examining What is the base case(s)? 1 When the key is found 2 The tree is empty (key was not found) What is the general case? Search in the left or right subtrees August 31, 2015 13 / 29
  • 15. Function InsertItem() What is the size of the problem? Number of nodes in the tree we are examining What is the base case(s)? The tree is empty What is the general case? Choose the left or right subtree August 31, 2015 15 / 29
  • 16. InsertItem() (Cont.) Inserting 11 in the given BST O(h) August 31, 2015 16 / 29
  • 17. Function DeleteItem() First, find the item. Then, delete it. Binary search tree property must be preserved!! We need to consider three different cases 1 Deleting a leaf 2 Deleting a node with only one child 3 Deleting a node with two children August 31, 2015 17 / 29
  • 18. Deleting a Leaf August 31, 2015 18 / 29
  • 19. Deleting a node with only one child August 31, 2015 19 / 29
  • 20. Deleting a node with two children August 31, 2015 20 / 29
  • 21. Deleting a node with two children(Cont.) Find predecessor (i.e., rightmost node in the left subtree) Replace the data of the node to be deleted with predecessor’s data Delete predecessor node August 31, 2015 21 / 29
  • 22. Function DeleteItem() What is the size of the problem? Number of nodes in the tree we are examining What is the base case(s)? Key to be deleted was found What is the general case? Choose the left or right subtree August 31, 2015 22 / 29
  • 23. Traversals There are mainly three ways to traverse a tree: 1 Inorder Traversal 2 Postorder Traversal 3 Preorder Traversal August 31, 2015 23 / 29
  • 25. Print the Tree For printing the Binary Search Tree in sorted order we can use Inorder Traversal. Result: A D J M Q R T August 31, 2015 25 / 29
  • 26. Use Of A Binary Search Tree In comparison to other trees using a Binary Search Tree we can efficiently perform the following: Search for a particular element. Sorting of elements. Find Maximum/Minimum. Delete a particular element. August 31, 2015 26 / 29
  • 28. August 31, 2015 28 / 29
  • 29. August 31, 2015 29 / 29