0% found this document useful (0 votes)
425 views

Lecture 3.1.3 Header Nodes Threads, Binary Search

Uploaded by

borab25865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
425 views

Lecture 3.1.3 Header Nodes Threads, Binary Search

Uploaded by

borab25865
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

UNIVERSITY INSTITUTE OF

ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE
AND ENGG.

Bachelor of Engineering (Computer Science & Engineering)


DATA STRUCTURES 21CSH-211

TREES DISCOVER . LEARN . EMPOWER


1
Threaded Binary Tree

• In the linked representation of binary trees, more than one half of the
link fields contain NULL values which results in wastage of storage
space. If a binary tree consists of n nodes then n+1 link fields contain
NULL values. So in order to effectively manage the space, a method
was devised by Perlis and Thornton in which the NULL links are
replaced with special links known as threads. Such binary trees with
threads are known as threaded binary trees. Each node in a threaded
binary tree either contains a link to its child node or thread to other
nodes in the tree.

2
3
Types of Threaded Binary Tree

• One-way threaded Binary Tree


• Two-way threaded Binary Tree

4
One-way threaded Binary trees
• In one-way threaded binary trees, a thread will appear either in the
right or left link field of a node. If it appears in the right link field of a
node then it will point to the next node that will appear on
performing in order traversal. Such trees are called Right threaded
binary trees. If thread appears in the left field of a node then it will
point to the nodes inorder predecessor. Such trees are called Left
threaded binary trees. Left threaded binary trees are used less often
as they don't yield the last advantages of right threaded binary trees.
In one-way threaded binary trees, the right link field of last node and
left link field of first node contains a NULL. In order to distinguish
threads from normal links they are represented by dotted lines.
5
The above figure shows the inorder traversal of this binary tree yields D, B, E, A, C, F.
When this tree is represented as a right threaded binary tree, the right link field of leaf
node D which contains a NULL value is replaced with a thread that points to node B
which is the inorder successor of a node D. In the same way other nodes containing
values in the right link field will contain NULL value.
6
Two-way threaded Binary Trees
• In two-way threaded Binary trees, the right link field of a node
containing NULL values is replaced by a thread that points to nodes
inorder successor and left field of a node containing NULL values is
replaced by a thread that points to nodes inorder predecessor.

7
The above figure shows the inorder traversal of this binary tree yields D, B, E, G, A, C, F. If we consider the
two-way threaded Binary tree, the node E whose left field contains NULL is replaced by a thread pointing to its
inorder predecessor i.e. node B. Similarly, for node G whose right and left linked fields contain NULL values
are replaced by threads such that right link field points to its inorder successor and left link field points to its
inorder predecessor. In the same way, other nodes containing NULL values in their link fields are filled with
threads.
8
Binary Search Trees (BST)
• A data structure for efficient searching, insertion and deletion
• Binary search tree property
• For every node X
• All the keys in its left
subtree are smaller than
the key value in X
• All the keys in its right
subtree are larger than the
key value in X

9
Binary Search Trees

A binary search tree Not a binary search tree

10
Binary Search Trees
The same set of keys may have different BSTs

• Average depth of a node is O(log N)


• Maximum depth of a node is O(N)
11
Algorithm for searching in a BST
FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR) : A binary search tree T is in memory and an ITEM of information is given. This procedure finds the
location LOC of ITEM in T and also location PAR of the parent of ITEM. There are 3 special cases:
(i) LOC=NULL and PAR=NULL will indicate that the tree is empty..
(ii) LOC≠NULL and PAR=NULL will indicate that the ITEM is the root of T.
(iii) LOC=NULL and PAR≠NULL will indicate that ITEM is not in T and can be added to T as a child of the node N with location PAR.

1. [Tree empty?]
If ROOT=NULL, then, Set LOC=NULL and PAR=NULL and return.
2. [ITEM at root?]
If ITEM=INFO[ROOT], then: Set LOC=ROOT and PAR=NULL and return.
3. [Initialize pointers PTR and SAVE]
If ITEM<INFO[ROOT], then:
Set PTR=LEFT[ROOT] and SAVE=ROOT
Else
Set PTR=RIGHT[ROOT] and SAVE=ROOT
[End of If structure]
4. Repeat steps 5 to 6 while PTR≠NULL:
5. [ITEM found?]
If ITEM=INFO[PTR], then: Set LOC=PTR and PAR=SAVE and return.
6. If ITEM<INFO[PTR], then:
Set SAVE=PTR and PTR=LEFT[PTR]
Else
Set SAVE=PTR and PTR=RIGHT[PTR]
[End of If structure]
[End of step4 loop]
7. [Search unsuccessful] Set LOC=NULL and PAR=SAVE
12
8. Exit
Searching BST
• If we are searching for 15, then we are done.
• If we are searching for a key < 15, then we should search in the left subtree.
• If we are searching for a key > 15, then we should search in the right subtree.

13
14
Inorder Traversal of BST
• Inorder traversal of BST prints out all the keys in sorted order

Inorder: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20

15
findMin/ findMax
• Goal: return the node containing the smallest (largest) key in the tree
• Algorithm: Start at the root and go left (right) as long as there is a left (right) child. The
stopping point is the smallest (largest) element

BinaryNode * BinarySearchTree::FindMin(BinaryNode *t) const


{
if (t == NULL)
return NULL;
if (t->left == NULL)
return t;
return FindMin(t->left);
}

• Time complexity = O(height of the tree)

16
Algorithm for inserting in a BST
INSBST(INFO,LEFT,RIGHT,ROOT,AVAIL,ITEM,LOC) : A binary search tree T is in memory and an ITEM
of information is given. This procedure finds the location LOC of ITEM in T or adds ITEM as a new
node in T at location LOC.
1. Call FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR)
2. If LOC≠NULL, then: Exit.
3. [Copy ITEM into new node in AVAIL list.]
(a)If AVAIL=NULL, then: Write: OVERFLOW & Exit.
(b)Set NEW=AVAIL, AVAIL=LEFT [AVAIL] & INFO [NEW] =ITEM.
(c)Set LOC=NEW, LEFT[NEW]=NULL & RIGHT[NEW]=NULL
4. [Add ITEM to tree]
If PAR=NULL, then:
Set ROOT=NEW [Make it root]
Else if ITEM<INFO [PAR], Then:
Set LEFT [PAR] =NEW
Else
Set RIGHT [PAR] =NEW
[End of if structure]
5. Exit

17
Insertion
• Proceed down the tree as you would with a find
• If X is found, do nothing (or update something)
• Otherwise, insert X at the last spot on the path traversed

• Time complexity = O(height of the tree)


18
Deletion
• When we delete a node, we need to consider how we take care of the children of the
deleted node.
• This has to be done such that the property of the search tree is maintained.

19
(1) Deleting a leaf
(2) Deleting a node with
only one child
(3) Deleting a node with two
children
(3) Deleting a node with two
children (cont.)
• Find predecessor (i.e., rightmost node in the left subtree)
• Replace the data of the node to be deleted with predecessor's data
• Delete predecessor node
Deleting in a Binary Search Tree
Algorithm:
1. If ROOT = NULL, then: Underflow and exit
2. If ROOT≠NULL , check:
If node to be deleted is a leaf node, then:
Directly delete leaf node.
else if node to be deleted has no left subtree, then:
Right child will take place of the parent node deleted.
else if node to be deleted has no right subtree, then:
left child will take place of the parent node deleted.
else if node to be deleted has left and right subtrees, then:
either of the 2 methods can be implemented:
(a) Find highest valued element among the descendants of left child & replace it
with deleted node.
(b) Find lowest valued element among the descendants of right child and replace it
with deleted node.
3. Exit

24
References
1. Li`pschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
2. Data structure and algorithm by Narasimha Karumanchi.
3. www.tutorialspoint.com
4. www.geeksforgeeks.com

25
Books Recommended
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and
Algorithms in C++”, Wiley Student Edition.
• Aho, Alfred V., Ullman, Jeffrey D., Hopcroft ,John E. “Data Structures and Algorithms”,
Addison Wesley
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall
of India
THANK YOU

27

You might also like