SlideShare a Scribd company logo
THREADED BINARY TREE AND BINARY SEARCH TREE
 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
Ad

Recommended

Tree Traversal
Tree Traversal
Md. Israil Fakir
 
Data structure - Graph
Data structure - Graph
Madhu Bala
 
Threaded Binary Tree
Threaded Binary Tree
khabbab_h
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Red black trees
Red black trees
mumairsadiq
 
Topological Sorting
Topological Sorting
ShahDhruv21
 
AVL Tree in Data Structure
AVL Tree in Data Structure
Vrushali Dhanokar
 
sparse matrix in data structure
sparse matrix in data structure
MAHALAKSHMI P
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptx
pavankumarjakkepalli
 
Linked list implementation of Queue
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
Binary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Binary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
Binary Search Tree
Binary Search Tree
Abhishek L.R
 
Tree in data structure
Tree in data structure
ghhgj jhgh
 
Binary Search Tree
Binary Search Tree
sagar yadav
 
Tree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 
Sum of subset problem.pptx
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
Depth first search [dfs]
Depth first search [dfs]
DEEPIKA T
 
Binary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
Bfs and Dfs
Bfs and Dfs
Masud Parvaze
 
Graph representation
Graph representation
Tech_MX
 
Red black tree
Red black tree
Rajendran
 
Tree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
Graph in data structure
Graph in data structure
Abrish06
 
Data structure tries
Data structure tries
Md. Naim khan
 
Binary tree
Binary tree
Rajendran
 
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
 
Threaded binarytree&heapsort
Threaded binarytree&heapsort
Ssankett Negi
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
 

More Related Content

What's hot (20)

Threaded Binary Tree.pptx
Threaded Binary Tree.pptx
pavankumarjakkepalli
 
Linked list implementation of Queue
Linked list implementation of Queue
Dr. Sindhia Lingaswamy
 
Binary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Binary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
Binary Search Tree
Binary Search Tree
Abhishek L.R
 
Tree in data structure
Tree in data structure
ghhgj jhgh
 
Binary Search Tree
Binary Search Tree
sagar yadav
 
Tree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 
Sum of subset problem.pptx
Sum of subset problem.pptx
V.V.Vanniaperumal College for Women
 
Depth first search [dfs]
Depth first search [dfs]
DEEPIKA T
 
Binary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
Bfs and Dfs
Bfs and Dfs
Masud Parvaze
 
Graph representation
Graph representation
Tech_MX
 
Red black tree
Red black tree
Rajendran
 
Tree and Binary Search tree
Tree and Binary Search tree
Muhazzab Chouhadry
 
Graph in data structure
Graph in data structure
Abrish06
 
Data structure tries
Data structure tries
Md. Naim khan
 
Binary tree
Binary tree
Rajendran
 
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
 

Viewers also liked (20)

Threaded binarytree&heapsort
Threaded binarytree&heapsort
Ssankett Negi
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
sumitbardhan
 
1.5 binary search tree
1.5 binary search tree
Krish_ver2
 
Tree
Tree
SHEETAL WAGHMARE
 
Trees data structure
Trees data structure
Sumit Gupta
 
TREE BST HEAP GRAPH
TREE BST HEAP GRAPH
Shahariar Rabby
 
Full threded binary tree
Full threded binary tree
Soham Kansodaria
 
Algorithm chapter 5
Algorithm chapter 5
chidabdu
 
Rfid
Rfid
Singavarapu Vijay Kumar
 
Programming in c++
Programming in c++
Christalin Nelson
 
Binary Search Tree and AVL
Binary Search Tree and AVL
Katang Isip
 
Red Black Tree
Red Black Tree
B.Sc in CSE, United International University - UIU, Dhaka
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
Aakash deep Singhal
 
1.1 binary tree
1.1 binary tree
Krish_ver2
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)
Taibah University, College of Computer Science & Engineering
 
Heaps & priority queues
Heaps & priority queues
Pedro Hugo Valencia Morales
 
Traversals | Data Structures
Traversals | Data Structures
Omair Imtiaz Ansari
 
(Binary tree)
(Binary tree)
almario1988
 
Bca ii dfs u-3 tree and graph
Bca ii dfs u-3 tree and graph
Rai University
 
Employee Time and Task Tracking System
Employee Time and Task Tracking System
Imran Javed Dar, MBA, PMP, 6σ, COBIT
 
Ad

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

Binary search Tree and avl tree , treee.ppt
Binary search Tree and avl tree , treee.ppt
sjain87654321
 
Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
DrBashirMSaad
 
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
sahadevbkbiet2023
 
Btree
Btree
sharmili priyadarsini
 
Lecture notes data structures tree
Lecture notes data structures tree
maamir farooq
 
Trees
Trees
9590133127
 
Data Structures
Data Structures
Nitesh Bichwani
 
TREES.pptx
TREES.pptx
19EBKCS055KarishmaVe
 
Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Binary Search Tree
Binary Search Tree
Zafar Ayub
 
Unit 3.ppt
Unit 3.ppt
JITTAYASHWANTHREDDY
 
Best for b trees
Best for b trees
DineshRaaja
 
VCE Unit 05.pptx
VCE Unit 05.pptx
skilljiolms
 
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
shesnasuneer
 
9. TREE Data Structure Non Linear Data Structure
9. TREE Data Structure Non Linear Data Structure
kejika1215
 
Algorithm and Data Structure - Binary Trees
Algorithm and Data Structure - Binary Trees
donotreply20
 
Binary Search Tree
Binary Search Tree
AdityaK92
 
Binary Search Tree
Binary Search Tree
VijayaLakshmi506
 
Introduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
Trees
Trees
Saurabh Mishra
 
Binary search Tree and avl tree , treee.ppt
Binary search Tree and avl tree , treee.ppt
sjain87654321
 
Lecture 7-BinarySearchTrees.ppt
Lecture 7-BinarySearchTrees.ppt
DrBashirMSaad
 
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
sahadevbkbiet2023
 
Lecture notes data structures tree
Lecture notes data structures tree
maamir farooq
 
Binary Search Tree
Binary Search Tree
Zafar Ayub
 
Best for b trees
Best for b trees
DineshRaaja
 
VCE Unit 05.pptx
VCE Unit 05.pptx
skilljiolms
 
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
shesnasuneer
 
9. TREE Data Structure Non Linear Data Structure
9. TREE Data Structure Non Linear Data Structure
kejika1215
 
Algorithm and Data Structure - Binary Trees
Algorithm and Data Structure - Binary Trees
donotreply20
 
Binary Search Tree
Binary Search Tree
AdityaK92
 
Introduction to data structure by anil dutt
Introduction to data structure by anil dutt
Anil Dutt
 
Ad

More from Siddhi Shrivas (14)

VECTOR FUNCTION
VECTOR FUNCTION
Siddhi Shrivas
 
Production management
Production management
Siddhi Shrivas
 
Team work
Team work
Siddhi Shrivas
 
Resonance in R-L-C circuit
Resonance in R-L-C circuit
Siddhi Shrivas
 
Exact & non differential equation
Exact & non differential equation
Siddhi Shrivas
 
Functions of process management
Functions of process management
Siddhi Shrivas
 
MULTIPLEXER
MULTIPLEXER
Siddhi Shrivas
 
DATABASE MANAGEMENT
DATABASE MANAGEMENT
Siddhi Shrivas
 
ENGAGE DEEPLY
ENGAGE DEEPLY
Siddhi Shrivas
 
KIRCHHOFF’S CURRENT LAW
KIRCHHOFF’S CURRENT LAW
Siddhi Shrivas
 
Communication
Communication
Siddhi Shrivas
 
Listening skills
Listening skills
Siddhi Shrivas
 
Laser
Laser
Siddhi Shrivas
 
Newtons law
Newtons law
Siddhi Shrivas
 

Recently uploaded (20)

Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
Mechanical Vibration_MIC 202_iit roorkee.pdf
Mechanical Vibration_MIC 202_iit roorkee.pdf
isahiliitr
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
How to Un-Obsolete Your Legacy Keypad Design
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Cadastral Maps
Cadastral Maps
Google
 
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
IJCNCJournal
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Tally.ERP 9 at a Glance.book - Tally Solutions .pdf
Shabista Imam
 
Unit III_One Dimensional Consolidation theory
Unit III_One Dimensional Consolidation theory
saravananr808639
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
最新版美国圣莫尼卡学院毕业证(SMC毕业证书)原版定制
Taqyea
 
Proposal for folders structure division in projects.pdf
Proposal for folders structure division in projects.pdf
Mohamed Ahmed
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
AI_Presentation (1). Artificial intelligence
AI_Presentation (1). Artificial intelligence
RoselynKaur8thD34
 
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
Deep Learning for Image Processing on 16 June 2025 MITS.pptx
resming1
 
Mechanical Vibration_MIC 202_iit roorkee.pdf
Mechanical Vibration_MIC 202_iit roorkee.pdf
isahiliitr
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Fundamentals of Digital Design_Class_12th April.pptx
Fundamentals of Digital Design_Class_12th April.pptx
drdebarshi1993
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Cadastral Maps
Cadastral Maps
Google
 
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
A Cluster-Based Trusted Secure Multipath Routing Protocol for Mobile Ad Hoc N...
IJCNCJournal
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 

THREADED BINARY TREE AND BINARY SEARCH TREE

  • 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)