SlideShare a Scribd company logo
introduction,
searching,
insertion and
deletion
Binary Search Trees (BST)
A data structure for efficient searching, insertion
and deletion
Binary search tree property
For every node X
All the keys in its left
subtree are smaller than
the key value in X
All the keys in its right
subtree are larger than the
key value in X
Binary Search Trees
A binary search tree Not a binary search tree
Binary Search Trees
Average depth of a node is O(log N)
Maximum depth of a node is O(N)
The same set of keys may have different BSTs
Searching BST
If we are searching for root (15), then we are done.
If we are searching for a key < root , then we
should search in the left subtree.
If we are searching for a key > root, then we should
search in the right subtree.
Searching (Find)
FIND(info, left, right, root, item, loc, par)- finds the item in tree T with root is root and
info, left and right is three array represented in memory. This algorithm returns loc
i.e. location of item and par i.e. parent.
1. [Tree Empty??]
if root==NULL, then set LOC=NULL & PAR=NULL and return.
1. [Item root ??]
If item==INFO[ROOT], then LOC=ROOT & PAR=NULL and return.
1. [Initialize pointer ptr and save]
If item<INFO[ROOT]
then set PTR = LEFT[ROOT] and SAVE=ROOT
Else
set PTR = RIGHT[ROOT] and SAVE=ROOT
[End of if]
1. Repeat 5 and 6 while ptr!=NULL
2. [item found??]
If ITEM=INFO[PTR], then set LOC=PTR and PAR=SAVE, and return.
1. If ITEM<INFO[PTR], then SAVE=PTR and PTR=LEFT[PTR]
Else
Set SAVE=PTR and PTR=RIGHT[PTR]
1. [Search unsuccessful] Set, LOC=NULL and PAR = SAVE
2. Exit
 Time complexity: O(height of the tree)
Sorting: Inorder Traversal of BST
Inorder Traversal of BST prints out all the keys in
sorted order
Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
Insertion
Proceed down the tree as you would with a find
If X is found, do nothing (or update something)
Otherwise, insert X at the last spot on the path traversed
Time complexity = O(height of the tree)
Inserting (ADD node)INSBST(info, left, right, root, item, loc, avail)- insert the item in tree
T with root is root and info, left and right is three array
represented in memory. This algorithm returns loc i.e. location of
item or ADD item as new node in tree.
1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
2. If LOC!=NULL, then Exit.
3. [Copy ITEM into new node in AVAIL list]
a) If AVAIL==NULL, Print “OVER FLOW”;
b) Set NEW=AVAIL, AVAIL=LEFT[AVAIL] and
INFO[NEW]=ITEM.
c) Set LOC=NEW,LEFT[NEW]=RIGHT[NEW]=NULL
4. [ADD ITEM to TREE]
If PAR=NULL then, Set ROOT=NEW.
Else IF ITEM<INFO[PAR] , Set LEFT[PAR]=NEW
Else Set RIGHT[PAR]=NEW
1. Exit
Time complexity: O(height of the tree)
Deletion
When we delete a node, we need to consider how we
take care of the children of the deleted node.
This has to be done such that the property of the
search tree is maintained.
Deletion under Different Cases
Case 1: the node is a leaf
Delete it immediately
Case 2: the node has one child
Adjust a pointer from the parent to bypass that node
Deletion Case 3
Case 3: the node has 2 children
Replace the key of that node with the minimum element
at the right subtree
Delete that 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.
 Time complexity = O(height of the tree)
Deletion Algorithm
DEL(INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM)
A binary search tree T is in memory, and an ITEM of information is
given. This algorithm delete ITEM from the tree.
1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR)
2. If LOC=NULL, then write ITEM not in tree and Exit
3. If RIGHT[LOC]!=NULL and LEFT[LOC]!=NULL, then:
Call CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR)
Else:
Call CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR)
4. Set LEFT[LOC]:=AVAIL and AVAIL :=LOC.
5. Exit
CASEA: only one or, no child
CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR)-delete
the Node N at location LOC, where N doesn’t have two
Children. PAR is location of parent node or, PAR=NULL
i.e. ROOT node.
1. [initialize CHILD]
If LEFT[LOC]=NULL and RIGHT[LOC]=NULL, then
CHILD=NULL
Else if LEFT[LOC]!=NULL , then CHILD=LEFT[LOC]
Else CHILD=RIGHT[LOC]
1. If PAR != NULL then: (i.e. NOT A ROOT NODE)
If LOC=LEFT[PAR], then set LEFT[PAR]=CHILD
Else RIGHT[PAR]=CHILD
[End of IF]
Else set ROOT=CHILD.
[End of IF]
1. Exit
CASEB: has 2 children
 CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR)-delete the Node N at location
LOC, where N has two Children. PAR is location of parent node or, PAR=NULL i.e.
ROOT node. SUC gives location of inorder successor and PARSUC gives location
of parent of inorder successor .
1. [Find SUC and PARSUC]
a) Set PTR=RIGHT[LOC] and SAVE=LOC
b) Repeat while LEFT[PTR]!=NULL
Set, SAVE=PTR and PTR=LEFT[PTR]
[END OF LOOP]
a) Set SUC=PTR and PARSUC=SAVE.
2. [Delete SUC] Call CASEA(INFO, LEFT, RIGHT, ROOT, SUC,PARSUC)
3. [replace node N by SUC]
a) If PAR != NULL then: (i.e. NOT A ROOT NODE)
If LOC=LEFT[PAR], then set LEFT[PAR]=SUC
Else RIGHT[PAR]=SUC
[End of IF]
Else set ROOT=SUC.
[End of IF]
b) Set, LEFT[SUC]=LEFT[LOC] and
RIGHT[SUC]=RIGHT[LOC]
4. Exit
Data Structure and Algorithms Binary Search Tree

More Related Content

What's hot

AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
Vrushali Dhanokar
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
Janki Shah
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
pavankumarjakkepalli
 
Expression trees
Expression treesExpression trees
Expression trees
Salman Vadsarya
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
SHAKOOR AB
 
Linked list
Linked list Linked list
Linked list
Arbind Mandal
 
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
Gurukul Kangri Vishwavidyalaya - Faculty of Engineering and Technology
 
Linked lists
Linked listsLinked lists
Linked lists
SARITHA REDDY
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
chauhankapil
 
Binary tree
Binary  treeBinary  tree
Binary tree
Vanitha Chandru
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
Ashish Arun
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
Heap tree
Heap treeHeap tree
Heap tree
JananiJ19
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
Meghaj Mallick
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
Kousalya M
 
stack presentation
stack presentationstack presentation
Queue data structure
Queue data structureQueue data structure
Queue data structure
anooppjoseph
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
Himanshu Choudhary
 

What's hot (20)

AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
Expression trees
Expression treesExpression trees
Expression trees
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Linked list
Linked list Linked list
Linked list
 
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
 
Linked lists
Linked listsLinked lists
Linked lists
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Heap tree
Heap treeHeap tree
Heap tree
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Binary search tree
Binary search treeBinary search tree
Binary search tree
 
stack presentation
stack presentationstack presentation
stack presentation
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
B trees dbms
B trees dbmsB trees dbms
B trees dbms
 
Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++Types of Tree in Data Structure in C++
Types of Tree in Data Structure in C++
 

Similar to Data Structure and Algorithms Binary Search Tree

8.binry search tree
8.binry search tree8.binry search tree
8.binry search tree
Chandan Singh
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsAakash deep Singhal
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
Zafar Ayub
 
BST.pdf
BST.pdfBST.pdf
BST_algorithm.pptx
BST_algorithm.pptxBST_algorithm.pptx
BST_algorithm.pptx
MahdiHasan86
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
SSE_AndyLi
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
sowmya koneru
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
Saurabh Mishra
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treeszukun
 
Data Structures
Data StructuresData Structures
Data Structures
Rahul Jamwal
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation search
manojmanoj218596
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
kalyanineve
 
arrays
arraysarrays
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
RohitkumarYadav80
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
cinterviews
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
RedHeart11
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
ssuser034ce1
 

Similar to Data Structure and Algorithms Binary Search Tree (20)

8.binry search tree
8.binry search tree8.binry search tree
8.binry search tree
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
BST.pdf
BST.pdfBST.pdf
BST.pdf
 
BST_algorithm.pptx
BST_algorithm.pptxBST_algorithm.pptx
BST_algorithm.pptx
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Skiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure treesSkiena algorithm 2007 lecture05 dictionary data structure trees
Skiena algorithm 2007 lecture05 dictionary data structure trees
 
Data Structures
Data StructuresData Structures
Data Structures
 
Demonstrate interpolation search
Demonstrate interpolation searchDemonstrate interpolation search
Demonstrate interpolation search
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Ds notes
Ds notesDs notes
Ds notes
 
arrays
arraysarrays
arrays
 
Unit III Heaps.ppt
Unit III Heaps.pptUnit III Heaps.ppt
Unit III Heaps.ppt
 
Cinterviews Binarysearch Tree
Cinterviews Binarysearch TreeCinterviews Binarysearch Tree
Cinterviews Binarysearch Tree
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 

More from ManishPrajapati78

Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
Data Structure and Algorithms Queues
Data Structure and Algorithms QueuesData Structure and Algorithms Queues
Data Structure and Algorithms Queues
ManishPrajapati78
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiData Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of Hanoi
ManishPrajapati78
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
ManishPrajapati78
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Data Structure and Algorithms Sorting
Data Structure and Algorithms SortingData Structure and Algorithms Sorting
Data Structure and Algorithms Sorting
ManishPrajapati78
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
ManishPrajapati78
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
ManishPrajapati78
 
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalData Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms Graphs
ManishPrajapati78
 
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmData Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding Algorithm
ManishPrajapati78
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 

More from ManishPrajapati78 (15)

Data Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary TreeData Structure and Algorithms Binary Tree
Data Structure and Algorithms Binary Tree
 
Data Structure and Algorithms Queues
Data Structure and Algorithms QueuesData Structure and Algorithms Queues
Data Structure and Algorithms Queues
 
Data Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge SortData Structure and Algorithms Merge Sort
Data Structure and Algorithms Merge Sort
 
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiData Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of Hanoi
 
Data Structure and Algorithms Stacks
Data Structure and Algorithms StacksData Structure and Algorithms Stacks
Data Structure and Algorithms Stacks
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Data Structure and Algorithms Sorting
Data Structure and Algorithms SortingData Structure and Algorithms Sorting
Data Structure and Algorithms Sorting
 
Data Structure and Algorithms Arrays
Data Structure and Algorithms ArraysData Structure and Algorithms Arrays
Data Structure and Algorithms Arrays
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Data Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph TraversalData Structure and Algorithms Graph Traversal
Data Structure and Algorithms Graph Traversal
 
Data Structure and Algorithms Graphs
Data Structure and Algorithms GraphsData Structure and Algorithms Graphs
Data Structure and Algorithms Graphs
 
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmData Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding Algorithm
 
Data Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and TreesData Structure and Algorithms Heaps and Trees
Data Structure and Algorithms Heaps and Trees
 
Data Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL TreesData Structure and Algorithms AVL Trees
Data Structure and Algorithms AVL Trees
 

Recently uploaded

Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 

Recently uploaded (20)

Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 

Data Structure and Algorithms Binary Search Tree

  • 2. Binary Search Trees (BST) A data structure for efficient searching, insertion and deletion Binary search tree property For every node X All the keys in its left subtree are smaller than the key value in X All the keys in its right subtree are larger than the key value in X
  • 3. Binary Search Trees A binary search tree Not a binary search tree
  • 4. Binary Search Trees Average depth of a node is O(log N) Maximum depth of a node is O(N) The same set of keys may have different BSTs
  • 5. Searching BST If we are searching for root (15), then we are done. If we are searching for a key < root , then we should search in the left subtree. If we are searching for a key > root, then we should search in the right subtree.
  • 6.
  • 7. Searching (Find) FIND(info, left, right, root, item, loc, par)- finds the item in tree T with root is root and info, left and right is three array represented in memory. This algorithm returns loc i.e. location of item and par i.e. parent. 1. [Tree Empty??] if root==NULL, then set LOC=NULL & PAR=NULL and return. 1. [Item root ??] If item==INFO[ROOT], then LOC=ROOT & PAR=NULL and return. 1. [Initialize pointer ptr and save] If item<INFO[ROOT] then set PTR = LEFT[ROOT] and SAVE=ROOT Else set PTR = RIGHT[ROOT] and SAVE=ROOT [End of if] 1. Repeat 5 and 6 while ptr!=NULL 2. [item found??] If ITEM=INFO[PTR], then set LOC=PTR and PAR=SAVE, and return. 1. If ITEM<INFO[PTR], then SAVE=PTR and PTR=LEFT[PTR] Else Set SAVE=PTR and PTR=RIGHT[PTR] 1. [Search unsuccessful] Set, LOC=NULL and PAR = SAVE 2. Exit  Time complexity: O(height of the tree)
  • 8. Sorting: Inorder Traversal of BST Inorder Traversal of BST prints out all the keys in sorted order Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20
  • 9. Insertion Proceed down the tree as you would with a find If X is found, do nothing (or update something) Otherwise, insert X at the last spot on the path traversed Time complexity = O(height of the tree)
  • 10. Inserting (ADD node)INSBST(info, left, right, root, item, loc, avail)- insert the item in tree T with root is root and info, left and right is three array represented in memory. This algorithm returns loc i.e. location of item or ADD item as new node in tree. 1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR) 2. If LOC!=NULL, then Exit. 3. [Copy ITEM into new node in AVAIL list] a) If AVAIL==NULL, Print “OVER FLOW”; b) Set NEW=AVAIL, AVAIL=LEFT[AVAIL] and INFO[NEW]=ITEM. c) Set LOC=NEW,LEFT[NEW]=RIGHT[NEW]=NULL 4. [ADD ITEM to TREE] If PAR=NULL then, Set ROOT=NEW. Else IF ITEM<INFO[PAR] , Set LEFT[PAR]=NEW Else Set RIGHT[PAR]=NEW 1. Exit Time complexity: O(height of the tree)
  • 11. Deletion When we delete a node, we need to consider how we take care of the children of the deleted node. This has to be done such that the property of the search tree is maintained.
  • 12. Deletion under Different Cases Case 1: the node is a leaf Delete it immediately Case 2: the node has one child Adjust a pointer from the parent to bypass that node
  • 13. Deletion Case 3 Case 3: the node has 2 children Replace the key of that node with the minimum element at the right subtree Delete that 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.  Time complexity = O(height of the tree)
  • 14. Deletion Algorithm DEL(INFO, LEFT, RIGHT, ROOT, AVAIL, ITEM) A binary search tree T is in memory, and an ITEM of information is given. This algorithm delete ITEM from the tree. 1. Call FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC, PAR) 2. If LOC=NULL, then write ITEM not in tree and Exit 3. If RIGHT[LOC]!=NULL and LEFT[LOC]!=NULL, then: Call CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR) Else: Call CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR) 4. Set LEFT[LOC]:=AVAIL and AVAIL :=LOC. 5. Exit
  • 15. CASEA: only one or, no child CASEA(INFO, LEFT, RIGHT, ROOT, LOC, PAR)-delete the Node N at location LOC, where N doesn’t have two Children. PAR is location of parent node or, PAR=NULL i.e. ROOT node. 1. [initialize CHILD] If LEFT[LOC]=NULL and RIGHT[LOC]=NULL, then CHILD=NULL Else if LEFT[LOC]!=NULL , then CHILD=LEFT[LOC] Else CHILD=RIGHT[LOC] 1. If PAR != NULL then: (i.e. NOT A ROOT NODE) If LOC=LEFT[PAR], then set LEFT[PAR]=CHILD Else RIGHT[PAR]=CHILD [End of IF] Else set ROOT=CHILD. [End of IF] 1. Exit
  • 16. CASEB: has 2 children  CASEB(INFO, LEFT, RIGHT, ROOT, LOC, PAR)-delete the Node N at location LOC, where N has two Children. PAR is location of parent node or, PAR=NULL i.e. ROOT node. SUC gives location of inorder successor and PARSUC gives location of parent of inorder successor . 1. [Find SUC and PARSUC] a) Set PTR=RIGHT[LOC] and SAVE=LOC b) Repeat while LEFT[PTR]!=NULL Set, SAVE=PTR and PTR=LEFT[PTR] [END OF LOOP] a) Set SUC=PTR and PARSUC=SAVE. 2. [Delete SUC] Call CASEA(INFO, LEFT, RIGHT, ROOT, SUC,PARSUC) 3. [replace node N by SUC] a) If PAR != NULL then: (i.e. NOT A ROOT NODE) If LOC=LEFT[PAR], then set LEFT[PAR]=SUC Else RIGHT[PAR]=SUC [End of IF] Else set ROOT=SUC. [End of IF] b) Set, LEFT[SUC]=LEFT[LOC] and RIGHT[SUC]=RIGHT[LOC] 4. Exit