SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

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

Was ist angesagt? (20)

Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
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
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
AVL Tree Data Structure
AVL Tree Data StructureAVL Tree Data Structure
AVL Tree Data Structure
 
AVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data StructureAVL tree ( Balanced Binary Search Tree)-Data Structure
AVL tree ( Balanced Binary Search Tree)-Data Structure
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
data structure(tree operations)
data structure(tree operations)data structure(tree operations)
data structure(tree operations)
 
Red black tree insertion
Red black tree insertionRed black tree insertion
Red black tree insertion
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Dijkstra
DijkstraDijkstra
Dijkstra
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Expression trees
Expression treesExpression trees
Expression trees
 
StructuresPointers.pptx
StructuresPointers.pptxStructuresPointers.pptx
StructuresPointers.pptx
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
 
Binary trees
Binary treesBinary trees
Binary trees
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Kürzlich hochgeladen

Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 

Kürzlich hochgeladen (20)

University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 

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