SlideShare a Scribd company logo
BINARY SEARCH TREE
(BST)
OR
ORDERED BINARY TREE
OR
SORTED BINARY TREE
Binary Search Trees
• A binary search tree (BST) is a binary tree in which all nodes in the
left sub-tree have a value less than that of the root node and all
nodes in the right sub-tree have a value either equal to or greater
than the root node.
• The same rule is applicable to every sub-tree in the tree.
• Due to its efficiency in searching elements, BSTs are widely used in
dictionary problems where the code always inserts and searches the
elements that are indexed by some key value.
Binary Search Trees
Various operation can be performed on BST
• Insertion
• Deletion
• Search
• Find min
• Find max
Algorithm to Insert a Value in a BST
Insert (TREE, VAL)
Step 1: IF TREE = NULL, then
Allocate memory for TREE
SET TREE->DATA = VAL
SET TREE->LEFT = TREE ->RIGHT = NULL
ELSE
IF VAL < TREE->DATA
Insert(TREE->LEFT, VAL)
ELSE
Insert(TREE->RIGHT, VAL)
[END OF IF]
[END OF IF]
Step 2: End
Algorithm to Delete from a BST
Delete (TREE, VAL)
Step 1: IF TREE = NULL, then
Write “VAL not found in the tree”
ELSE IF VAL < TREE->DATA
Delete(TREE->LEFT, VAL)
ELSE IF VAL > TREE->DATA
Delete(TREE->RIGHT, VAL)
ELSE IF TREE->LEFT AND TREE->RIGHT
SET TEMP = findLargestNode(TREE->LEFT)
SET TREE->DATA = TEMP->DATA
Delete(TREE->LEFT, TEMP->DATA)
ELSE
SET TEMP = TREE
IF TREE->LEFT = NULL AND TREE ->RIGHT = NULL
SET TREE = NULL
ELSE IF TREE->LEFT != NULL
SET TREE = TREE->LEFT
ELSE
SET TREE = TREE->RIGHT
[END OF IF]
FREE TEMP
[END OF IF]
Step 2: End
searchElement (TREE, VAL)
Step 1: IF TREE->DATA = VAL OR TREE = NULL, then
Return TREE
ELSE
IF VAL < TREE->DATA
Return searchElement(TREE->LEFT, VAL)
ELSE
Return searchElement(TREE->RIGHT, VAL)
[END OF IF]
[END OF IF]
Step 2: End
Algorithm to Search a Value in a BST
Finding the Smallest Node in a BST
• The basic property of a BST states that the smaller value will occur
in the left sub-tree.
• If the left sub-tree is NULL, then the value of root node will be
smallest as compared with nodes in the right sub-tree.
• So, to find the node with the smallest value, we will find the value
of the leftmost node of the left sub-tree.
• However, if the left sub-tree is empty then we will find the value of
the root node.
findSmallestElement (TREE)
Step 1: IF TREE = NULL OR TREE->LEFT = NULL, then
Return TREE
ELSE
Return findSmallestElement(TREE->LEFT)
[END OF IF]
Step 2: End
Finding the Largest Node in a BST
• The basic property of a BST states that the larger value will occur
in the right sub-tree.
• If the right sub-tree is NULL, then the value of root node will be
largest as compared with nodes in the left sub-tree.
• So, to find the node with the largest value, we will find the value of
the rightmost node of the right sub-tree.
• However, if the right sub-tree is empty then we will find the value
of the root node.
findLargestElement (TREE)
Step 1: IF TREE = NULL OR TREE->RIGHT = NULL, then
Return TREE
ELSE
Return findLargestElement(TREE->RIGHT)
[END OF IF]
Step 2: End
Threaded Binary Trees
• A threaded binary tree is same as that of a binary tree but with a
difference in storing NULL pointers.
• In the linked representation of a BST, a number of nodes contain a
NULL pointer either in their left or right fields or in both. This space
that is wasted in storing a NULL pointer can be efficiently used to
store some other useful piece of information.
• The NULL entries can be replaced to store a pointer to the in-order
predecessor, or the in-order successor of the node. These special
pointers are called threads and binary trees containing threads are
called threaded trees.
• In the linked representation of a threaded binary tree, threads will
be denoted using dotted lines.
Threaded Binary Trees
• In one way threading, a thread will appear either in the right field or
the left field of the node.
• If the thread appears in the left field, then it points to the in-order
predecessor of the node. Such a one way threaded tree is called a left
threaded binary tree.
• If the thread appears in the right field, then it will point to the in-
order successor of the node. Such a one way threaded tree is called a
right threaded binary tree.
1
3
2
4
8
5 6 7
1
2
1
0
1
1
9
1
2 3
4 X 5 6 X 7
X 8 X 9 X 10 X 11 X 12 X
Binary tree with one way threading
Threaded Binary Trees
• In a two way threaded tree, also called a doubled threaded tree,
threads will appear in both the left and right fields of the node.
• While the left field will point to the in-order predecessor of the node,
the right field will point to its successor.
• A two way threaded binary tree is also called a fully threaded binary
tree.
1
3
2
4
8
5 6 7
1
2
1
0
1
1
9
1
2 3
4 5 6 7
X 8 9 10 11 12 X
Binary tree with two way threading
Threaded Binary Trees
ADVANTAGES
•Enables linear traversal of elements in the tree
•Linear traversal eliminates the use of stacks which in turn consume a
lot of memory space and computer time
•Enables to find the parent of given element without explicit use of
parent pointers
•Enable forward and backward traversal of the nodes
Ad

More Related Content

Similar to M.E - Computer Science and Engineering-Data structure-bst-and-threaded (20)

Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdfTreeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
timoemin50
 
learn tree, linked list, queue, stack, and other algo
learn tree, linked list, queue, stack, and other algolearn tree, linked list, queue, stack, and other algo
learn tree, linked list, queue, stack, and other algo
unknowwqer
 
4. Apply data structures such as arrays, linked lists, and trees as an abstra...
4. Apply data structures such as arrays, linked lists, and trees as an abstra...4. Apply data structures such as arrays, linked lists, and trees as an abstra...
4. Apply data structures such as arrays, linked lists, and trees as an abstra...
deivasigamani9
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
ER Punit Jain
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 
Binary tree
Binary treeBinary tree
Binary tree
MKalpanaDevi
 
Unit 3 - Part 1_Threaded Binary Tree.pptx
Unit 3 - Part 1_Threaded Binary Tree.pptxUnit 3 - Part 1_Threaded Binary Tree.pptx
Unit 3 - Part 1_Threaded Binary Tree.pptx
ssuser7319f8
 
Lec 10_Binary Search Tree in data structure and algorithm.pptx
Lec 10_Binary Search Tree in data structure and algorithm.pptxLec 10_Binary Search Tree in data structure and algorithm.pptx
Lec 10_Binary Search Tree in data structure and algorithm.pptx
hinamazhar6
 
Unit 3 trees
Unit 3   treesUnit 3   trees
Unit 3 trees
LavanyaJ28
 
Materi Searching
Materi Searching Materi Searching
Materi Searching
BintangWijaya5
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
TABISH HAMID
 
Tree
TreeTree
Tree
invertis university
 
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
BASIC TREE  AND TYPES OF DI CONCEPTS.pptxBASIC TREE  AND TYPES OF DI CONCEPTS.pptx
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
tpvvsreenivasarao
 
GEC_Soumik[1].pptx.interview of a person from JU.
GEC_Soumik[1].pptx.interview of a person from JU.GEC_Soumik[1].pptx.interview of a person from JU.
GEC_Soumik[1].pptx.interview of a person from JU.
Sritama Patra
 
Introduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptxIntroduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTINGSEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
mohanrajm63
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
Anusruti Mitra
 
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdfTreeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
Treeeeeeeeeeeeeeeeereeeeeeeeeeeeeeee.pdf
timoemin50
 
learn tree, linked list, queue, stack, and other algo
learn tree, linked list, queue, stack, and other algolearn tree, linked list, queue, stack, and other algo
learn tree, linked list, queue, stack, and other algo
unknowwqer
 
4. Apply data structures such as arrays, linked lists, and trees as an abstra...
4. Apply data structures such as arrays, linked lists, and trees as an abstra...4. Apply data structures such as arrays, linked lists, and trees as an abstra...
4. Apply data structures such as arrays, linked lists, and trees as an abstra...
deivasigamani9
 
tree-160731205832.pptx
tree-160731205832.pptxtree-160731205832.pptx
tree-160731205832.pptx
MouDhara1
 
Unit 3 - Part 1_Threaded Binary Tree.pptx
Unit 3 - Part 1_Threaded Binary Tree.pptxUnit 3 - Part 1_Threaded Binary Tree.pptx
Unit 3 - Part 1_Threaded Binary Tree.pptx
ssuser7319f8
 
Lec 10_Binary Search Tree in data structure and algorithm.pptx
Lec 10_Binary Search Tree in data structure and algorithm.pptxLec 10_Binary Search Tree in data structure and algorithm.pptx
Lec 10_Binary Search Tree in data structure and algorithm.pptx
hinamazhar6
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
TABISH HAMID
 
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
BASIC TREE  AND TYPES OF DI CONCEPTS.pptxBASIC TREE  AND TYPES OF DI CONCEPTS.pptx
BASIC TREE AND TYPES OF DI CONCEPTS.pptx
tpvvsreenivasarao
 
GEC_Soumik[1].pptx.interview of a person from JU.
GEC_Soumik[1].pptx.interview of a person from JU.GEC_Soumik[1].pptx.interview of a person from JU.
GEC_Soumik[1].pptx.interview of a person from JU.
Sritama Patra
 
Introduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptxIntroduction to Tree_Data Structure.pptx
Introduction to Tree_Data Structure.pptx
PoojariniMitra1
 
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTINGSEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
SEARCHING AND SORTING ALGORITHMS, TYPES OF SORTING
mohanrajm63
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
Krish_ver2
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
SeethaDinesh
 

More from poonkodiraja2806 (7)

Computer Network NFV Management and Orchestration.pdf
Computer Network NFV Management and Orchestration.pdfComputer Network NFV Management and Orchestration.pdf
Computer Network NFV Management and Orchestration.pdf
poonkodiraja2806
 
Principle of programming language -M.E-CSE
Principle of programming language -M.E-CSEPrinciple of programming language -M.E-CSE
Principle of programming language -M.E-CSE
poonkodiraja2806
 
Virtual Local area Network and Virtual Private area Network
Virtual Local area Network and Virtual Private area NetworkVirtual Local area Network and Virtual Private area Network
Virtual Local area Network and Virtual Private area Network
poonkodiraja2806
 
Computer Network unit-5 -SDN and NFV topics
Computer Network unit-5 -SDN and NFV topicsComputer Network unit-5 -SDN and NFV topics
Computer Network unit-5 -SDN and NFV topics
poonkodiraja2806
 
M.E - Computer Science and Engineering-Data structure B tree
M.E - Computer Science and Engineering-Data structure B treeM.E - Computer Science and Engineering-Data structure B tree
M.E - Computer Science and Engineering-Data structure B tree
poonkodiraja2806
 
M.E - Computer Science and Engineering-Data structure avl-tree
M.E - Computer Science and Engineering-Data structure avl-treeM.E - Computer Science and Engineering-Data structure avl-tree
M.E - Computer Science and Engineering-Data structure avl-tree
poonkodiraja2806
 
M.E - Computer Science and Engineering-Data structure unit-2 heaps
M.E - Computer Science and Engineering-Data structure unit-2  heapsM.E - Computer Science and Engineering-Data structure unit-2  heaps
M.E - Computer Science and Engineering-Data structure unit-2 heaps
poonkodiraja2806
 
Computer Network NFV Management and Orchestration.pdf
Computer Network NFV Management and Orchestration.pdfComputer Network NFV Management and Orchestration.pdf
Computer Network NFV Management and Orchestration.pdf
poonkodiraja2806
 
Principle of programming language -M.E-CSE
Principle of programming language -M.E-CSEPrinciple of programming language -M.E-CSE
Principle of programming language -M.E-CSE
poonkodiraja2806
 
Virtual Local area Network and Virtual Private area Network
Virtual Local area Network and Virtual Private area NetworkVirtual Local area Network and Virtual Private area Network
Virtual Local area Network and Virtual Private area Network
poonkodiraja2806
 
Computer Network unit-5 -SDN and NFV topics
Computer Network unit-5 -SDN and NFV topicsComputer Network unit-5 -SDN and NFV topics
Computer Network unit-5 -SDN and NFV topics
poonkodiraja2806
 
M.E - Computer Science and Engineering-Data structure B tree
M.E - Computer Science and Engineering-Data structure B treeM.E - Computer Science and Engineering-Data structure B tree
M.E - Computer Science and Engineering-Data structure B tree
poonkodiraja2806
 
M.E - Computer Science and Engineering-Data structure avl-tree
M.E - Computer Science and Engineering-Data structure avl-treeM.E - Computer Science and Engineering-Data structure avl-tree
M.E - Computer Science and Engineering-Data structure avl-tree
poonkodiraja2806
 
M.E - Computer Science and Engineering-Data structure unit-2 heaps
M.E - Computer Science and Engineering-Data structure unit-2  heapsM.E - Computer Science and Engineering-Data structure unit-2  heaps
M.E - Computer Science and Engineering-Data structure unit-2 heaps
poonkodiraja2806
 
Ad

Recently uploaded (20)

Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdfDavid Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry
 
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
JRR Tolkien’s Lord of the Rings: Was It Influenced by Nordic Mythology, Homer...
Reflections on Morality, Philosophy, and History
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Journal of Soft Computing in Civil Engineering
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Modelling of Concrete Compressive Strength Admixed with GGBFS Using Gene Expr...
Journal of Soft Computing in Civil Engineering
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Nanometer Metal-Organic-Framework Literature Comparison
Nanometer Metal-Organic-Framework  Literature ComparisonNanometer Metal-Organic-Framework  Literature Comparison
Nanometer Metal-Organic-Framework Literature Comparison
Chris Harding
 
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdfDavid Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
Uses of drones in civil construction.pdf
Uses of drones in civil construction.pdfUses of drones in civil construction.pdf
Uses of drones in civil construction.pdf
surajsen1729
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
Empowering Electric Vehicle Charging Infrastructure with Renewable Energy Int...
AI Publications
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
Frontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend EngineersFrontend Architecture Diagram/Guide For Frontend Engineers
Frontend Architecture Diagram/Guide For Frontend Engineers
Michael Hertzberg
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Ad

M.E - Computer Science and Engineering-Data structure-bst-and-threaded

  • 1. BINARY SEARCH TREE (BST) OR ORDERED BINARY TREE OR SORTED BINARY TREE
  • 2. Binary Search Trees • A binary search tree (BST) is a binary tree in which all nodes in the left sub-tree have a value less than that of the root node and all nodes in the right sub-tree have a value either equal to or greater than the root node. • The same rule is applicable to every sub-tree in the tree. • Due to its efficiency in searching elements, BSTs are widely used in dictionary problems where the code always inserts and searches the elements that are indexed by some key value.
  • 3. Binary Search Trees Various operation can be performed on BST • Insertion • Deletion • Search • Find min • Find max
  • 4. Algorithm to Insert a Value in a BST Insert (TREE, VAL) Step 1: IF TREE = NULL, then Allocate memory for TREE SET TREE->DATA = VAL SET TREE->LEFT = TREE ->RIGHT = NULL ELSE IF VAL < TREE->DATA Insert(TREE->LEFT, VAL) ELSE Insert(TREE->RIGHT, VAL) [END OF IF] [END OF IF] Step 2: End
  • 5. Algorithm to Delete from a BST Delete (TREE, VAL) Step 1: IF TREE = NULL, then Write “VAL not found in the tree” ELSE IF VAL < TREE->DATA Delete(TREE->LEFT, VAL) ELSE IF VAL > TREE->DATA Delete(TREE->RIGHT, VAL) ELSE IF TREE->LEFT AND TREE->RIGHT SET TEMP = findLargestNode(TREE->LEFT) SET TREE->DATA = TEMP->DATA Delete(TREE->LEFT, TEMP->DATA) ELSE SET TEMP = TREE IF TREE->LEFT = NULL AND TREE ->RIGHT = NULL SET TREE = NULL ELSE IF TREE->LEFT != NULL SET TREE = TREE->LEFT ELSE SET TREE = TREE->RIGHT [END OF IF] FREE TEMP [END OF IF] Step 2: End
  • 6. searchElement (TREE, VAL) Step 1: IF TREE->DATA = VAL OR TREE = NULL, then Return TREE ELSE IF VAL < TREE->DATA Return searchElement(TREE->LEFT, VAL) ELSE Return searchElement(TREE->RIGHT, VAL) [END OF IF] [END OF IF] Step 2: End Algorithm to Search a Value in a BST
  • 7. Finding the Smallest Node in a BST • The basic property of a BST states that the smaller value will occur in the left sub-tree. • If the left sub-tree is NULL, then the value of root node will be smallest as compared with nodes in the right sub-tree. • So, to find the node with the smallest value, we will find the value of the leftmost node of the left sub-tree. • However, if the left sub-tree is empty then we will find the value of the root node. findSmallestElement (TREE) Step 1: IF TREE = NULL OR TREE->LEFT = NULL, then Return TREE ELSE Return findSmallestElement(TREE->LEFT) [END OF IF] Step 2: End
  • 8. Finding the Largest Node in a BST • The basic property of a BST states that the larger value will occur in the right sub-tree. • If the right sub-tree is NULL, then the value of root node will be largest as compared with nodes in the left sub-tree. • So, to find the node with the largest value, we will find the value of the rightmost node of the right sub-tree. • However, if the right sub-tree is empty then we will find the value of the root node. findLargestElement (TREE) Step 1: IF TREE = NULL OR TREE->RIGHT = NULL, then Return TREE ELSE Return findLargestElement(TREE->RIGHT) [END OF IF] Step 2: End
  • 9. Threaded Binary Trees • A threaded binary tree is same as that of a binary tree but with a difference in storing NULL pointers. • In the linked representation of a BST, a number of nodes contain a NULL pointer either in their left or right fields or in both. This space that is wasted in storing a NULL pointer can be efficiently used to store some other useful piece of information. • The NULL entries can be replaced to store a pointer to the in-order predecessor, or the in-order successor of the node. These special pointers are called threads and binary trees containing threads are called threaded trees. • In the linked representation of a threaded binary tree, threads will be denoted using dotted lines.
  • 10. Threaded Binary Trees • In one way threading, a thread will appear either in the right field or the left field of the node. • If the thread appears in the left field, then it points to the in-order predecessor of the node. Such a one way threaded tree is called a left threaded binary tree. • If the thread appears in the right field, then it will point to the in- order successor of the node. Such a one way threaded tree is called a right threaded binary tree. 1 3 2 4 8 5 6 7 1 2 1 0 1 1 9 1 2 3 4 X 5 6 X 7 X 8 X 9 X 10 X 11 X 12 X Binary tree with one way threading
  • 11. Threaded Binary Trees • In a two way threaded tree, also called a doubled threaded tree, threads will appear in both the left and right fields of the node. • While the left field will point to the in-order predecessor of the node, the right field will point to its successor. • A two way threaded binary tree is also called a fully threaded binary tree. 1 3 2 4 8 5 6 7 1 2 1 0 1 1 9 1 2 3 4 5 6 7 X 8 9 10 11 12 X Binary tree with two way threading
  • 12. Threaded Binary Trees ADVANTAGES •Enables linear traversal of elements in the tree •Linear traversal eliminates the use of stacks which in turn consume a lot of memory space and computer time •Enables to find the parent of given element without explicit use of parent pointers •Enable forward and backward traversal of the nodes
  翻译: