SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (20)

Tree and Binary Search tree
Tree and Binary Search treeTree and Binary Search tree
Tree and Binary Search tree
 
Set data structure
Set data structure Set data structure
Set data structure
 
Binary tree
Binary tree Binary tree
Binary tree
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Linked list
Linked listLinked list
Linked list
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
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 traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
 
B tree
B treeB tree
B tree
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
B trees dbms
B trees dbmsB trees dbms
B trees dbms
 

Andere mochten auch (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
 
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
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
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)
 
Bca ii dfs u-3 tree and graph
Bca  ii dfs u-3 tree and graphBca  ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 

Ähnlich wie THREADED BINARY TREE AND BINARY SEARCH TREE

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

Mehr von Siddhi Shrivas

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

Kürzlich hochgeladen

An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxPurva Nikam
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
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
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 

Kürzlich hochgeladen (20)

An introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptxAn introduction to Semiconductor and its types.pptx
An introduction to Semiconductor and its types.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
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
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
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
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 

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)