SlideShare a Scribd company logo
1 of 38
 Definition: A tree is a finite set of one or
more nodes such that:
 There is a specially designated node called the
root.
 The remaining nodes are partitioned into n ≥ 0
disjoint sets T1, …, Tn, where each of these sets
is a tree. We call T1, …, Tn the subtrees of the
root.
 In a linked representation of binary tree,
there are more null links can be replaced by
pointers, called threads to other nodes. A
left null link of the node is replaced with the
address of its inorder predecessor , similarly
a right null link of node is replaced with the
address of its inorder successor.
 Binary trees have a lot of wasted space: the
leaf nodes each have 2 null pointers
 We can use these pointers to help us in
inorder traversals
 We have the pointers reference the next
node in an inorder traversal; called threads
 We need to know if a pointer is an actual link
or a thread, so we keep a boolean for each
pointer
 Threading Rules
 A 0 Right Child field at node p is replaced by a
pointer to the node that would be visited after
p when traversing the tree in inorder. That is,
it is replaced by the inorder successor of p.
 A 0 Left Child link at node p is replaced by a
pointer to the node that immediately precedes
node p in inorder (i.e., it is replaced by the
inorder predecessor of p).
A
H I
B
D E
C
GF
Inorder sequence: H, D, I, B, E, A, F, C, G
 To distinguish between normal pointers and
threads, two boolean fields, LeftThread and
RightThread, are added to the record in
memory representation.
 t->LeftChild = TRUE
=> t->LeftChild is a thread
 t->LeftChild = FALSE
=> t->LeftChild is a pointer to the left child.
 To avoid dangling threads, a head node is
used in representing a binary tree.
 The original tree becomes the left subtree of
the head node.
 Empty Binary Tree
TRUE FALSE
LeftThread LeftChild RightChild RightThreaddata
f - f
f A f
f B f
f D f
t H t t I t
t E t
f B f
f D f t E t
 Inserting a node r as the right child of a node s.
 If s has an empty right subtree, then the insertion is
simple and diagram in Figure 5.23(a).
 If the right subtree of s is not empty, the this right
subtree is made the right subtree of r after insertion.
When thisis done, r becomes the inorder predecessor of
a node that has a LdeftThread==TRUE field, and
consequently there is an thread which has to be updated
to point to r. The node containing this thread was
previously the inorder successor of s. Figure 5.23(b)
illustrates the insertion for this case.
s
r
s
r
before after
8
75
3
11
13
1
6
9
 We start at the leftmost node in the tree,
print it, and follow its right thread
 If we follow a thread to the right, we output
the node and continue to its right
 If we follow a link to the right, we go to the
leftmost node, print it, and continue
8
75
3
11
13
1
6
9
Start at leftmost node, print it
Output
1
8
75
3
11
13
1
6
9
Follow thread to right, print
node
Output
1
3
8
75
3
11
13
1
6
9
Follow link to right, go to
leftmost node and print
Output
1
3
5
8
75
3
11
13
1
6
9
Follow thread to right, print
node
Output
1
3
5
6
8
75
3
11
13
1
6
9
Follow link to right, go to
leftmost node and print
Output
1
3
5
6
7
8
75
3
11
13
1
6
9
Follow thread to right, print
node
Output
1
3
5
6
7
8
8
75
3
11
13
1
6
9
Follow thread to right, print
node
Output
1
3
5
6
7
8
8
75
3
11
13
1
6
9
Follow link to right, go to
leftmost node and print
Output
1
3
5
6
7
8
9
11
13
8
75
3
11
13
1
6
9
Follow thread to right, print
node
Output
1
3
5
6
7
8
9
11
 We’re still wasting pointers, since half of our
leafs’ pointers are still null
 We can add threads to the previous node in
an inorder traversal as well, which we can
use to traverse the tree backwards or even
to do postorder traversals
8
75
3
11
13
1
6
9
 Advantages of threaded binary tree.
 Non-recursive preorder traversal can be
implemented without a stack.
 Non-recursive inorder traversal can be
implemented without a stack.
 Non-recursive postorder traversal can be
implemented without a stack.
 Binary search tree
 Every element has a unique key.
 The keys in a nonempty left sub tree (right sub tree)
are smaller (larger) than the key in the root of
subtree.
 The left and right subtrees are also binary search
trees.
 Heap needs O(n) to perform deletion of a non-priority
queue. This may not be the best solution.
 Binary search tree provide a better performance for search,
insertion, and deletion.
 Definition: A binary serach tree is a binary tree. It may be
empty. If it is not empty then it satisfies the following
properties:
 Every element has a key and no two elements have the same
key (i.e., the keys are distinct)
 The keys (if any) in the left subtree are smaller than the key in
the root.
 The keys (if any) in the right subtree are larger than the key in
the root.
 The left and right subtrees are also binary search trees.
20
15 25
14 10 22
30
5 40
2
60
70
8065
Not binary
search tree
Binary search
trees
 If the root is 0, then this is an empty tree.
No search is needed.
 If the root is not 0, compare the x with
the key of root.
 If x equals to the key of the root, then it’s
done.
 If x is less than the key of the root, then no
elements in the right subtree can have key
value x. We only need to search the left tree.
 If x larger than the key of the root, only the
right subtree is to be searched.
 To search a binary search tree by the
ranks of the elements in the tree, we
need additional field “LeftSize”.
 LeftSize is the number of the elements in
the left subtree of a node plus one.
 It is obvious that a binary search tree of
height h can be searched by key as well as
by rank in O(h) time.
template <class Type>
BstNode <Type>* BST<Type>::Search(int k)
// Search the binary search tree for the kth smallest element
{
BstNode<Type> *t = root;
while(t)
{
if (k == t->LeftSize) return n;
if (k < t->LeftSize) t = t->LeftChild;
else {
k -= LeftSize;
t = t->RightChild;
}
}
return 0;
}
 Before insertion is performed, a search
must be done to make sure that the value
to be inserted is not already in the tree.
 If the search fails, then we know the
value is not in the tree. So it can be
inserted into the tree.
 It takes O(h) to insert a node to a binary
search tree.
30
5 40
2 80
30
5 40
2 8035
Template <class Type>
Boolean BST<Type>::Insert(const Element<Type>& x)
// insert x into the binary search tree
{
// Search for x.key, q is the parent of p
BstNode<Type> *p = root; BstNode<Type> *q = 0;
while(p) {
q = p;
if (x.key == p->data.key) return FALSE; // x.key is already in tree
if (x.key < p->data.key) p = p->LeftChild;
else p = p->RightChild;
}
// Perform insertion
p = new BstNode<Type>;
p->LeftChild = p->RightChild = 0; p->data = x;
if (!root) root = p;
else if (x.key < q->data.key) q->LeftChild = p;
else q->RightChild = p;
return TRUE;
}
 Delete a leaf node
 A leaf node which is a right child of its parent
 A leaf node which is a left child of its parent
 Delete a non-leaf node
 A node that has one child
 A node that has two children
 Replaced by the largest element in its left subtree, or
 Replaced by the smallest element in its right subtree
 Again, the delete function has complexity of O(h)
25
30
40
2 80
30
5 40
2 8035
30
25
30
40
2 80
30
25
30
40
2 80
5
22
30
40
80
5
THANK YOU

More Related Content

What's hot

Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.pptTirthika Bandi
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
Sparse matrix
Sparse matrixSparse matrix
Sparse matrixdincyjain
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree Divya Ks
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree Krish_ver2
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAbhishek L.R
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm03446940736
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of joinraj upadhyay
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 

What's hot (20)

Circular link list.ppt
Circular link list.pptCircular link list.ppt
Circular link list.ppt
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary tree
Binary tree Binary tree
Binary tree
 
Sparse matrix
Sparse matrixSparse matrix
Sparse matrix
 
Lecture optimal binary search tree
Lecture optimal binary search tree Lecture optimal binary search tree
Lecture optimal binary search tree
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
1.8 splay tree
1.8 splay tree 1.8 splay tree
1.8 splay tree
 
stack & queue
stack & queuestack & queue
stack & queue
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 
AVL Tree in Data Structure
AVL Tree in Data Structure AVL Tree in Data Structure
AVL Tree in Data Structure
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
 
Relational Algebra,Types of join
Relational Algebra,Types of joinRelational Algebra,Types of join
Relational Algebra,Types of join
 
Abstract Data Types
Abstract Data TypesAbstract Data Types
Abstract Data Types
 
Arrays
ArraysArrays
Arrays
 
Hashing
HashingHashing
Hashing
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Red black tree
Red black treeRed black tree
Red black tree
 

Viewers also liked

Viewers also liked (20)

Threaded binarytree&heapsort
Threaded binarytree&heapsortThreaded binarytree&heapsort
Threaded binarytree&heapsort
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Tree
TreeTree
Tree
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Trees data structure
Trees data structureTrees data structure
Trees data structure
 
TREE BST HEAP GRAPH
TREE BST HEAP GRAPHTREE BST HEAP GRAPH
TREE BST HEAP GRAPH
 
Full threded binary tree
Full threded binary treeFull threded binary tree
Full threded binary tree
 
Algorithm chapter 5
Algorithm chapter 5Algorithm chapter 5
Algorithm chapter 5
 
Rfid
RfidRfid
Rfid
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
Binary Search Tree and AVL
Binary Search Tree and AVLBinary Search Tree and AVL
Binary Search Tree and AVL
 
Red Black Tree
Red Black TreeRed Black Tree
Red Black Tree
 
Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary 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
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Traversals | Data Structures
Traversals | Data StructuresTraversals | Data Structures
Traversals | Data Structures
 
(Binary tree)
(Binary tree)(Binary tree)
(Binary tree)
 

Similar to THREADED BINARY TREE AND BINARY SEARCH TREE

Similar to THREADED BINARY TREE AND BINARY SEARCH TREE (20)

Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.pptLecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
 
Btree
BtreeBtree
Btree
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Trees
TreesTrees
Trees
 
Data Structures
Data StructuresData Structures
Data Structures
 
TREES.pptx
TREES.pptxTREES.pptx
TREES.pptx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Unit 3.ppt
Unit 3.pptUnit 3.ppt
Unit 3.ppt
 
Best for b trees
Best for b treesBest for b trees
Best for b trees
 
VCE Unit 05.pptx
VCE Unit 05.pptxVCE Unit 05.pptx
VCE Unit 05.pptx
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
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
 
Trees
TreesTrees
Trees
 
presentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashingpresentation on b tress. heap trees.hashing
presentation on b tress. heap trees.hashing
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
8.binry search tree
8.binry search tree8.binry search tree
8.binry search tree
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Binary tree
Binary treeBinary tree
Binary tree
 
unit06-3-Trees.pdf
unit06-3-Trees.pdfunit06-3-Trees.pdf
unit06-3-Trees.pdf
 

More from Siddhi Shrivas

More from Siddhi Shrivas (14)

VECTOR FUNCTION
VECTOR FUNCTION VECTOR FUNCTION
VECTOR FUNCTION
 
Production management
Production managementProduction management
Production management
 
Team work
Team workTeam work
Team work
 
Resonance in R-L-C circuit
Resonance in R-L-C circuitResonance in R-L-C circuit
Resonance in R-L-C circuit
 
Exact & non differential equation
Exact & non differential equationExact & non differential equation
Exact & non differential equation
 
Functions of process management
Functions of process managementFunctions of process management
Functions of process management
 
MULTIPLEXER
MULTIPLEXERMULTIPLEXER
MULTIPLEXER
 
DATABASE MANAGEMENT
DATABASE MANAGEMENTDATABASE MANAGEMENT
DATABASE MANAGEMENT
 
ENGAGE DEEPLY
ENGAGE DEEPLYENGAGE DEEPLY
ENGAGE DEEPLY
 
KIRCHHOFF’S CURRENT LAW
KIRCHHOFF’S CURRENT LAWKIRCHHOFF’S CURRENT LAW
KIRCHHOFF’S CURRENT LAW
 
Communication
CommunicationCommunication
Communication
 
Listening skills
Listening skillsListening skills
Listening skills
 
Laser
LaserLaser
Laser
 
Newtons law
Newtons lawNewtons law
Newtons law
 

Recently uploaded

US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxsomshekarkn64
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 

Recently uploaded (20)

US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptx
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 

THREADED BINARY TREE AND BINARY SEARCH TREE

  • 1.
  • 2.  Definition: A tree is a finite set of one or more nodes such that:  There is a specially designated node called the root.  The remaining nodes are partitioned into n ≥ 0 disjoint sets T1, …, Tn, where each of these sets is a tree. We call T1, …, Tn the subtrees of the root.
  • 3.  In a linked representation of binary tree, there are more null links can be replaced by pointers, called threads to other nodes. A left null link of the node is replaced with the address of its inorder predecessor , similarly a right null link of node is replaced with the address of its inorder successor.
  • 4.  Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers  We can use these pointers to help us in inorder traversals  We have the pointers reference the next node in an inorder traversal; called threads  We need to know if a pointer is an actual link or a thread, so we keep a boolean for each pointer
  • 5.  Threading Rules  A 0 Right Child field at node p is replaced by a pointer to the node that would be visited after p when traversing the tree in inorder. That is, it is replaced by the inorder successor of p.  A 0 Left Child link at node p is replaced by a pointer to the node that immediately precedes node p in inorder (i.e., it is replaced by the inorder predecessor of p).
  • 6. A H I B D E C GF Inorder sequence: H, D, I, B, E, A, F, C, G
  • 7.  To distinguish between normal pointers and threads, two boolean fields, LeftThread and RightThread, are added to the record in memory representation.  t->LeftChild = TRUE => t->LeftChild is a thread  t->LeftChild = FALSE => t->LeftChild is a pointer to the left child.
  • 8.  To avoid dangling threads, a head node is used in representing a binary tree.  The original tree becomes the left subtree of the head node.  Empty Binary Tree TRUE FALSE LeftThread LeftChild RightChild RightThreaddata
  • 9. f - f f A f f B f f D f t H t t I t t E t f B f f D f t E t
  • 10.  Inserting a node r as the right child of a node s.  If s has an empty right subtree, then the insertion is simple and diagram in Figure 5.23(a).  If the right subtree of s is not empty, the this right subtree is made the right subtree of r after insertion. When thisis done, r becomes the inorder predecessor of a node that has a LdeftThread==TRUE field, and consequently there is an thread which has to be updated to point to r. The node containing this thread was previously the inorder successor of s. Figure 5.23(b) illustrates the insertion for this case.
  • 13.  We start at the leftmost node in the tree, print it, and follow its right thread  If we follow a thread to the right, we output the node and continue to its right  If we follow a link to the right, we go to the leftmost node, print it, and continue
  • 14. 8 75 3 11 13 1 6 9 Start at leftmost node, print it Output 1
  • 15. 8 75 3 11 13 1 6 9 Follow thread to right, print node Output 1 3
  • 16. 8 75 3 11 13 1 6 9 Follow link to right, go to leftmost node and print Output 1 3 5
  • 17. 8 75 3 11 13 1 6 9 Follow thread to right, print node Output 1 3 5 6
  • 18. 8 75 3 11 13 1 6 9 Follow link to right, go to leftmost node and print Output 1 3 5 6 7
  • 19. 8 75 3 11 13 1 6 9 Follow thread to right, print node Output 1 3 5 6 7 8
  • 20. 8 75 3 11 13 1 6 9 Follow thread to right, print node Output 1 3 5 6 7 8
  • 21. 8 75 3 11 13 1 6 9 Follow link to right, go to leftmost node and print Output 1 3 5 6 7 8 9 11 13
  • 22. 8 75 3 11 13 1 6 9 Follow thread to right, print node Output 1 3 5 6 7 8 9 11
  • 23.  We’re still wasting pointers, since half of our leafs’ pointers are still null  We can add threads to the previous node in an inorder traversal as well, which we can use to traverse the tree backwards or even to do postorder traversals
  • 25.  Advantages of threaded binary tree.  Non-recursive preorder traversal can be implemented without a stack.  Non-recursive inorder traversal can be implemented without a stack.  Non-recursive postorder traversal can be implemented without a stack.
  • 26.  Binary search tree  Every element has a unique key.  The keys in a nonempty left sub tree (right sub tree) are smaller (larger) than the key in the root of subtree.  The left and right subtrees are also binary search trees.
  • 27.  Heap needs O(n) to perform deletion of a non-priority queue. This may not be the best solution.  Binary search tree provide a better performance for search, insertion, and deletion.  Definition: A binary serach tree is a binary tree. It may be empty. If it is not empty then it satisfies the following properties:  Every element has a key and no two elements have the same key (i.e., the keys are distinct)  The keys (if any) in the left subtree are smaller than the key in the root.  The keys (if any) in the right subtree are larger than the key in the root.  The left and right subtrees are also binary search trees.
  • 28. 20 15 25 14 10 22 30 5 40 2 60 70 8065 Not binary search tree Binary search trees
  • 29.  If the root is 0, then this is an empty tree. No search is needed.  If the root is not 0, compare the x with the key of root.  If x equals to the key of the root, then it’s done.  If x is less than the key of the root, then no elements in the right subtree can have key value x. We only need to search the left tree.  If x larger than the key of the root, only the right subtree is to be searched.
  • 30.  To search a binary search tree by the ranks of the elements in the tree, we need additional field “LeftSize”.  LeftSize is the number of the elements in the left subtree of a node plus one.  It is obvious that a binary search tree of height h can be searched by key as well as by rank in O(h) time.
  • 31. template <class Type> BstNode <Type>* BST<Type>::Search(int k) // Search the binary search tree for the kth smallest element { BstNode<Type> *t = root; while(t) { if (k == t->LeftSize) return n; if (k < t->LeftSize) t = t->LeftChild; else { k -= LeftSize; t = t->RightChild; } } return 0; }
  • 32.  Before insertion is performed, a search must be done to make sure that the value to be inserted is not already in the tree.  If the search fails, then we know the value is not in the tree. So it can be inserted into the tree.  It takes O(h) to insert a node to a binary search tree.
  • 33. 30 5 40 2 80 30 5 40 2 8035
  • 34. Template <class Type> Boolean BST<Type>::Insert(const Element<Type>& x) // insert x into the binary search tree { // Search for x.key, q is the parent of p BstNode<Type> *p = root; BstNode<Type> *q = 0; while(p) { q = p; if (x.key == p->data.key) return FALSE; // x.key is already in tree if (x.key < p->data.key) p = p->LeftChild; else p = p->RightChild; } // Perform insertion p = new BstNode<Type>; p->LeftChild = p->RightChild = 0; p->data = x; if (!root) root = p; else if (x.key < q->data.key) q->LeftChild = p; else q->RightChild = p; return TRUE; }
  • 35.  Delete a leaf node  A leaf node which is a right child of its parent  A leaf node which is a left child of its parent  Delete a non-leaf node  A node that has one child  A node that has two children  Replaced by the largest element in its left subtree, or  Replaced by the smallest element in its right subtree  Again, the delete function has complexity of O(h)